diff --git a/src/Microsoft.Tye.Hosting/TyeHost.cs b/src/Microsoft.Tye.Hosting/TyeHost.cs index a56e8c3c7..f69efb3b9 100644 --- a/src/Microsoft.Tye.Hosting/TyeHost.cs +++ b/src/Microsoft.Tye.Hosting/TyeHost.cs @@ -4,10 +4,13 @@ using System; using System.Collections.Generic; +using System.CommandLine.Invocation; +using System.Diagnostics; using System.Globalization; using System.Linq; using System.Net; using System.Net.Sockets; +using System.Runtime.InteropServices; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Configuration; @@ -98,6 +101,11 @@ public async Task StartAsync() await _processor.StartAsync(_application); + if (_args.Contains("--dashboard")) + { + OpenDashboard(app.Addresses.First()); + } + return app; } @@ -298,6 +306,31 @@ private async Task StopAsync() _processor = null; } + private void OpenDashboard(string url) + { + try + { + // https://github.com/dotnet/corefx/issues/10361 + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + url = url.Replace("&", "^&"); + System.Diagnostics.Process.Start(new ProcessStartInfo("cmd", $"/c start {url}") { CreateNoWindow = true }); + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + System.Diagnostics.Process.Start("xdg-open", url); + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + System.Diagnostics.Process.Start("open", url); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Error launching dashboard."); + } + } + public async ValueTask DisposeAsync() { await StopAsync(); diff --git a/src/tye/Program.RunCommand.cs b/src/tye/Program.RunCommand.cs index 2aa2818a3..3739ebd98 100644 --- a/src/tye/Program.RunCommand.cs +++ b/src/tye/Program.RunCommand.cs @@ -64,6 +64,12 @@ private static Command CreateRunCommand(string[] args) Required = false }); + command.AddOption(new Option("--dashboard") + { + Description = "Launch dashboard on run.", + Required = false + }); + command.Handler = CommandHandler.Create(async (console, path, debug) => { // Workaround for https://github.com/dotnet/command-line-api/issues/723#issuecomment-593062654