Skip to content

Commit

Permalink
feature: allow providing a path to --open-browser (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
natemcmaster committed Jun 25, 2023
1 parent 6f65405 commit 2741f03
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 13 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ Usage: dotnet serve [options]
Options:
--version Show version information.
-d|--directory <DIR> The root directory to serve. [Current directory]
-o|--open-browser Open a web browser when the server starts. [false]
-o|--open-browser[:<PATH>] Open a web browser when the server starts. [Default false]
You can also provide the subpath to launch by using -o:<PATH>.
Example: -o:/path/index.html
-p|--port <PORT> Port to use [8080]. Use 0 for a dynamic port.
-a|--address <ADDRESS> Address to use. [Default = localhost].
Accepts IP addresses,
Expand Down
4 changes: 2 additions & 2 deletions src/dotnet-serve/CommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ internal class CommandLineOptions
[DirectoryExists]
public string Directory { get; internal set; }

[Option(Description = "Open a web browser when the server starts. [false]")]
public bool? OpenBrowser { get; internal set; }
[Option("-o|--open-browser[:<PATH>]", CommandOptionType.SingleOrNoValue, Description = "Open a web browser when the server starts. [Default = false]\nYou can also provide the subpath to launch by using -o:<PATH>.\nExample: -o:/path/index.html")]
public (bool hasValue, string path) OpenBrowser { get; internal set; }

[Option(Description = "Port to use [8080]. Use 0 for a dynamic port.")]
[Range(0, 65535, ErrorMessage = "Invalid port. Ports must be in the range of 0 to 65535.")]
Expand Down
31 changes: 28 additions & 3 deletions src/dotnet-serve/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,25 @@ private static void ReadConfig(ConfigSection config, CommandLineOptions model)
{
model.Port ??= (int?)config.GetNumber("port");
model.Directory ??= config.GetString("directory");
model.OpenBrowser ??= config.GetBoolean("open-browser");
if (!model.OpenBrowser.hasValue)
{
try
{
if (config.TryGetBoolean("open-browser", out var openBrowser) && openBrowser)
{
model.OpenBrowser = (hasValue: true, null);
}
}
catch (FormatException)
{
// Raised when the value is not a boolean
var openBrowserPath = config.GetString("open-browser");
if (!string.IsNullOrEmpty(openBrowserPath))
{
model.OpenBrowser = (hasValue: true, openBrowserPath);
}
}
}
model.Quiet ??= config.GetBoolean("quiet");
model.Verbose ??= config.GetBoolean("verbose");
model.CertPemPath ??= config.GetNormalizedPath("cert");
Expand Down Expand Up @@ -146,9 +164,16 @@ private static void WriteConfig(ConfigSection config, CommandLineOptions model)
{
config.SetString("directory", model.Directory);
}
if (model.OpenBrowser != null)
if (model.OpenBrowser.hasValue)
{
config.SetBoolean("open-browser", model.OpenBrowser.Value);
if (!string.IsNullOrEmpty(model.OpenBrowser.path))
{
config.SetString("open-browser", model.OpenBrowser.path);
}
else
{
config.SetBoolean("open-browser", true);
}
}
if (model.Quiet != null)
{
Expand Down
14 changes: 9 additions & 5 deletions src/dotnet-serve/SimpleServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,20 @@ private void AfterServerStart(IWebHost host, ILogger<SimpleServer> logger)
_console.WriteLine("");
_console.WriteLine("Press CTRL+C to exit");

if (_options.OpenBrowser == true)
if (_options.OpenBrowser.hasValue)
{
var url = NormalizeToLoopbackAddress(addresses.Addresses.First());
var uri = new Uri(NormalizeToLoopbackAddress(addresses.Addresses.First()));

if (!string.IsNullOrEmpty(pathBase))
if (!string.IsNullOrWhiteSpace(_options.OpenBrowser.path))
{
url += pathBase;
uri = new Uri(uri, _options.OpenBrowser.path);
}
else if (!string.IsNullOrEmpty(pathBase))
{
uri = new Uri(uri, pathBase);
}

LaunchBrowser(url);
LaunchBrowser(uri.ToString());
}

static string GetListeningAddressText(IServerAddressesFeature addresses)
Expand Down
4 changes: 2 additions & 2 deletions test/dotnet-serve.Tests/ConfigTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void ConfigurationProvidesDefaultOptions()

Assert.Equal(4242, options.Port);
Assert.Equal(Path.GetTempPath(), options.Directory);
Assert.True(options.OpenBrowser);
Assert.True(options.OpenBrowser.hasValue);
Assert.True(options.Quiet);
Assert.True(options.Verbose);
Assert.Equal(certFile, options.CertPemPath);
Expand Down Expand Up @@ -123,7 +123,7 @@ public void CommandLineOverridesConfiguration()

Assert.Equal(4242, options.Port);
Assert.Equal(Path.GetTempPath(), options.Directory);
Assert.True(options.OpenBrowser);
Assert.True(options.OpenBrowser.hasValue);
Assert.True(options.Quiet);
Assert.True(options.Verbose);
Assert.Equal(certFile, options.CertPemPath);
Expand Down

0 comments on commit 2741f03

Please sign in to comment.