Skip to content
This repository has been archived by the owner on Nov 20, 2023. It is now read-only.

Commit

Permalink
add dashboard option to auto launch (#350)
Browse files Browse the repository at this point in the history
  • Loading branch information
spboyer committed Apr 11, 2020
1 parent 10b3dc2 commit a2dd987
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/Microsoft.Tye.Hosting/TyeHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -98,6 +101,11 @@ public async Task<WebApplication> StartAsync()

await _processor.StartAsync(_application);

if (_args.Contains("--dashboard"))
{
OpenDashboard(app.Addresses.First());
}

return app;
}

Expand Down Expand Up @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions src/tye/Program.RunCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IConsole, FileInfo, string[]>(async (console, path, debug) =>
{
// Workaround for https://github.com/dotnet/command-line-api/issues/723#issuecomment-593062654
Expand Down

0 comments on commit a2dd987

Please sign in to comment.