Skip to content

Commit

Permalink
chore: switch to system.commandline
Browse files Browse the repository at this point in the history
  • Loading branch information
vandycknick committed Feb 20, 2020
1 parent 6607959 commit 50fe438
Show file tree
Hide file tree
Showing 15 changed files with 445 additions and 698 deletions.
6 changes: 3 additions & 3 deletions src/WebTty.Hosting/Client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
"devDependencies": {
"@nvd/tsconfig": "1.0.0",
"@types/jest": "25.1.2",
"@types/jest": "25.1.3",
"@types/lodash": "4.14.149",
"@types/react-redux": "7.1.7",
"@types/uuid": "3.4.7",
Expand All @@ -49,10 +49,10 @@
"lodash": "4.17.15",
"prettier": "1.19.1",
"style-loader": "1.1.3",
"ts-jest": "25.2.0",
"ts-jest": "25.2.1",
"ts-loader": "6.2.1",
"tsconfig-paths-webpack-plugin": "3.2.0",
"typescript": "3.7.5",
"typescript": "3.8.2",
"webpack": "4.41.6",
"webpack-cli": "3.3.11",
"webpack-dev-server": "3.10.3"
Expand Down
28 changes: 14 additions & 14 deletions src/WebTty.Hosting/Client/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -576,10 +576,10 @@
"@types/istanbul-lib-coverage" "*"
"@types/istanbul-lib-report" "*"

"@types/[email protected].2":
version "25.1.2"
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-25.1.2.tgz#1c4c8770c27906c7d8def5d2033df9dbd39f60da"
integrity sha512-EsPIgEsonlXmYV7GzUqcvORsSS9Gqxw/OvkGwHfAdpjduNRxMlhsav0O5Kb0zijc/eXSO/uW6SJt9nwull8AUQ==
"@types/[email protected].3":
version "25.1.3"
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-25.1.3.tgz#9b0b5addebccfb631175870be8ba62182f1bc35a"
integrity sha512-jqargqzyJWgWAJCXX96LBGR/Ei7wQcZBvRv0PLEu9ZByMfcs23keUJrKv9FMR6YZf9YCbfqDqgmY+JUBsnqhrg==
dependencies:
jest-diff "^25.1.0"
pretty-format "^25.1.0"
Expand Down Expand Up @@ -7194,10 +7194,10 @@ trim-newlines@^2.0.0:
resolved "https://registry.yarnpkg.com/trim-newlines/-/trim-newlines-2.0.0.tgz#b403d0b91be50c331dfc4b82eeceb22c3de16d20"
integrity sha1-tAPQuRvlDDMd/EuC7s6yLD3hbSA=

[email protected].0:
version "25.2.0"
resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-25.2.0.tgz#dfd87c2b71ef4867f5a0a44f40cb9c67e02991ac"
integrity sha512-VaRdb0da46eorLfuHEFf0G3d+jeREcV+Wb/SvW71S4y9Oe8SHWU+m1WY/3RaMknrBsnvmVH0/rRjT8dkgeffNQ==
[email protected].1:
version "25.2.1"
resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-25.2.1.tgz#49bf05da26a8b7fbfbc36b4ae2fcdc2fef35c85d"
integrity sha512-TnntkEEjuXq/Gxpw7xToarmHbAafgCaAzOpnajnFC6jI7oo1trMzAHA04eWpc3MhV6+yvhE8uUBAmN+teRJh0A==
dependencies:
bs-logger "0.x"
buffer-from "1.x"
Expand All @@ -7208,7 +7208,7 @@ [email protected]:
mkdirp "0.x"
resolve "1.x"
semver "^5.5"
yargs-parser "10.x"
yargs-parser "^16.1.0"

[email protected]:
version "6.2.1"
Expand Down Expand Up @@ -7306,10 +7306,10 @@ typedarray@^0.0.6:
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=

typescript@3.7.5:
version "3.7.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.7.5.tgz#0692e21f65fd4108b9330238aac11dd2e177a1ae"
integrity sha512-/P5lkRXkWHNAbcJIiHPfRoKqyd7bsyCma1hZNUGfn20qm64T6ZBlrzprymeu918H+mB/0rIg2gGK/BXkhhYgBw==
typescript@3.8.2:
version "3.8.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.8.2.tgz#91d6868aaead7da74f493c553aeff76c0c0b1d5a"
integrity sha512-EgOVgL/4xfVrCMbhYKUQTdF37SQn4Iw73H5BgCrF1Abdun7Kwy/QZsE/ssAy0y4LxBbvua3PIbFsbRczWWnDdQ==

[email protected]:
version "3.4.10"
Expand Down Expand Up @@ -7819,7 +7819,7 @@ yallist@^3.0.2:
resolved "https://registry.yarnpkg.com/yallist/-/yallist-3.1.1.tgz#dbb7daf9bfd8bac9ab45ebf602b8cbad0d5d08fd"
integrity sha512-a4UGQaWPH59mOXUYnAG2ewncQS4i4F43Tv3JoAM+s2VDAmS9NsK8GpDMLrCHPksFT7h3K6TOoUNn2pb7RoXx4g==

yargs-parser@10.x, yargs-parser@^10.0.0:
yargs-parser@^10.0.0:
version "10.1.0"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-10.1.0.tgz#7202265b89f7e9e9f2e5765e0fe735a905edbaa8"
integrity sha512-VCIyR1wJoEBZUqk5PA+oOBF6ypbwh5aNB3I50guxAL/quggdfs4TtNHQrSazFA3fYZ+tEqfs0zIGlv0c/rgjbQ==
Expand Down
28 changes: 24 additions & 4 deletions src/WebTty.Hosting/WebTtyHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,40 @@ namespace WebTty.Hosting
{
public static class WebTtyHost
{
public static IHostBuilder CreateHostBuilder()
public static IHostBuilder CreateHostBuilder() => CreateHostBuilder(new WebTtyHostOptions());

public static IHostBuilder CreateHostBuilder(WebTtyHostOptions options)
{
return new HostBuilder()
.ConfigureWebHost(webhost =>
webhost
.ConfigureAppConfiguration(appConfig =>
appConfig.AddInMemoryCollection(options.ToDictionary()))
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder
.UseStaticWebAssets()
.PreferHostingUrls(false)
.SuppressStatusMessages(true)
.ConfigureServices(ConfigureServices)
.Configure(Configure));
.Configure(Configure)
.UseKestrel(kestrel =>
{
kestrel.Listen(options.Address, options.Port);

if (!string.IsNullOrEmpty(options.UnixSocket))
{
kestrel.ListenUnixSocket(options.UnixSocket);
}
});
})
.UseSerilog();
}

private static void ConfigureServices(WebHostBuilderContext context, IServiceCollection services)
{
services.Configure<ConsoleLifetimeOptions>(opts => opts.SuppressStatusMessages = true);
services.AddOptions<StaticFileOptions>()
.Configure(options => options.FileProvider = new ManifestEmbeddedFileProvider(typeof(WebTtyHost).Assembly, "wwwroot"));

services.AddPty();
services.AddResponseCompression();
services.AddRazorPages();
Expand Down
30 changes: 30 additions & 0 deletions src/WebTty.Hosting/WebTtyHostOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Collections.Generic;
using System.Net;

namespace WebTty.Hosting
{
public class WebTtyHostOptions
{
public IPAddress Address { get; set; } = IPAddress.Loopback;
public string UnixSocket { get; set; }
public int Port { get; set; } = 5000;
public string Path { get; set; } = "/pty";
public string Theme { get; set; } = "default";
public string Command { get; set; }
public List<string> Args { get; set; } = new List<string>();

internal IDictionary<string, string> ToDictionary()
{
return new Dictionary<string, string>
{
{ nameof(Address), Address.ToString() },
{ "UnixSocket", UnixSocket },
{ "Port", Port.ToString() },
{ "Path", Path },
{ "Theme", Theme },
{ nameof(Command), Command },
{ nameof(Args), string.Join(' ', Args) },
};
}
}
}
62 changes: 62 additions & 0 deletions src/WebTty/ArgumentExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using System.CommandLine;
using System.CommandLine.Parsing;
using System.Linq;
using System.Net;

namespace WebTty
{
public static class ArgumentExtensions
{
public static bool TryConvertIPAddress(SymbolResult result, out IPAddress address)
{
var token = result.Tokens.FirstOrDefault();

if (string.Equals("localhost", token.Value, StringComparison.OrdinalIgnoreCase))
{
address = IPAddress.Loopback;
return true;
}

if (string.Equals("any", token.Value, StringComparison.OrdinalIgnoreCase))
{
address = IPAddress.Any;
return true;
}

return IPAddress.TryParse(token.Value, out address);
}

public static Argument<string> StartsWith(this Argument<string> arg, char character)
{
arg.AddValidator(result => {
var value = result.GetValueOrDefault<string>();
return value.StartsWith(character) ?
string.Empty : $"Argument {result.Argument.Name} should start with a {character}.";
});

return arg;
}

public static Argument<int> Between(this Argument<int> args, int min, int max)
{
args.AddValidator(result => {
int port;

try
{
port = result.GetValueOrDefault<int>();
}
catch
{
return $"Argument {result.Argument.Name} should be an integer.";
}

return port > min && port < max ?
string.Empty : $"Argument {result.Argument.Name} should be a value between {min} and {max}.";
});

return args;
}
}
}
158 changes: 0 additions & 158 deletions src/WebTty/CommandLineOptions.cs

This file was deleted.

Loading

0 comments on commit 50fe438

Please sign in to comment.