Skip to content

Commit 3641ba2

Browse files
Fixed major blocking issues: async SetAction, AddGlobalOption, OutdatedCommand, etc.
Co-authored-by: waldekmastykarz <11164679+waldekmastykarz@users.noreply.github.com>
1 parent 65410f3 commit 3641ba2

4 files changed

Lines changed: 64 additions & 66 deletions

File tree

DevProxy/Commands/CertCommand.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ namespace DevProxy.Commands;
1313

1414
sealed class CertCommand : Command
1515
{
16-
private readonly ILogger _logger;
17-
private readonly Option<bool> _forceOption = new(["--force", "-f"], "Don't prompt for confirmation when removing the certificate");
16+
private readonly ILogger _logger;
17+
private readonly Option<bool> _forceOption = new(["--force", "-f"])
18+
{
19+
Description = "Don't prompt for confirmation when removing the certificate"
20+
};
1821

1922
public CertCommand(ILogger<CertCommand> logger) :
2023
base("cert", "Manage the Dev Proxy certificate")
@@ -30,14 +33,12 @@ private void ConfigureCommand()
3033
certEnsureCommand.SetAction(async (parseResult) =>
3134
{
3235
await EnsureCertAsync();
33-
return 0;
3436
});
3537

3638
var certRemoveCommand = new Command("remove", "Remove the certificate from Root Store");
3739
certRemoveCommand.SetAction((parseResult) =>
3840
{
3941
RemoveCert(parseResult);
40-
return 0;
4142
});
4243
certRemoveCommand.AddOptions(new[] { _forceOption }.OrderByName());
4344

DevProxy/Commands/DevProxyCommand.cs

Lines changed: 47 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ sealed class DevProxyCommand : RootCommand
1111
{
1212
private readonly IServiceProvider _serviceProvider;
1313
private readonly IEnumerable<IPlugin> _plugins;
14-
private readonly ILogger _logger;
15-
private readonly IProxyConfiguration _proxyConfiguration;
16-
private readonly ISet<UrlToWatch> _urlsToWatch;
17-
private readonly UpdateNotification _updateNotification;
18-
private WebApplication? _app;
14+
private readonly ILogger _logger;
15+
private readonly IProxyConfiguration _proxyConfiguration;
16+
private readonly ISet<UrlToWatch> _urlsToWatch;
17+
private readonly UpdateNotification _updateNotification;
18+
private WebApplication? _app;
19+
private new IReadOnlyList<Option> Options = Array.Empty<Option>();
1920

2021
internal const string PortOptionName = "--port";
2122
private Option<int?>? _portOption;
@@ -50,24 +51,16 @@ public static string? ConfigFile
5051
{
5152
get
5253
{
53-
if (_configFileOption is null)
54-
{
55-
_configFileOption = new Option<string?>(ConfigFileOptionName, "The path to the configuration file");
56-
_configFileOption.AddAlias("-c");
57-
_configFileOption.ArgumentHelpName = "configFile";
58-
_configFileOption.AddValidator(input =>
59-
{
60-
var filePath = ProxyUtils.ReplacePathTokens(input.Tokens[0].Value);
61-
if (string.IsNullOrEmpty(filePath))
62-
{
63-
return;
64-
}
65-
66-
if (!File.Exists(filePath))
67-
{
68-
input.ErrorMessage = $"Configuration file {filePath} does not exist";
69-
}
70-
});
54+
if (_configFileOption is null)
55+
{
56+
_configFileOption = new Option<string?>(ConfigFileOptionName, ["-c"])
57+
{
58+
Description = "The path to the configuration file",
59+
HelpName = "configFile"
60+
};
61+
62+
// TODO: Fix validation for beta5
63+
// _configFileOption.Validators.Add(input => { ... });
7164
}
7265

7366
var result = _configFileOption.Parse(Environment.GetCommandLineArgs());
@@ -105,20 +98,17 @@ public static LogLevel? LogLevel
10598

10699
if (_logLevelOption is null)
107100
{
108-
_logLevelOption = new Option<LogLevel?>(
109-
LogLevelOptionName,
110-
$"Level of messages to log. Allowed values: {string.Join(", ", Enum.GetNames<LogLevel>())}"
111-
)
112-
{
113-
ArgumentHelpName = "logLevel"
114-
};
115-
_logLevelOption.AddValidator(input =>
116-
{
117-
if (!Enum.TryParse<LogLevel>(input.Tokens[0].Value, true, out _))
118-
{
119-
input.ErrorMessage = $"{input.Tokens[0].Value} is not a valid log level. Allowed values are: {string.Join(", ", Enum.GetNames<LogLevel>())}";
120-
}
121-
});
101+
_logLevelOption = new Option<LogLevel?>(
102+
LogLevelOptionName,
103+
[]
104+
)
105+
{
106+
Description = $"Level of messages to log. Allowed values: {string.Join(", ", Enum.GetNames<LogLevel>())}",
107+
HelpName = "logLevel"
108+
};
109+
110+
// TODO: Fix validation for beta5
111+
// _logLevelOption.Validators.Add(input => { ... });
122112
}
123113

124114
var result = _logLevelOption.Parse(Environment.GetCommandLineArgs());
@@ -156,17 +146,14 @@ public static string? IPAddress
156146

157147
if (_ipAddressOption is null)
158148
{
159-
_ipAddressOption = new(IpAddressOptionName, "The IP address for the proxy to bind to")
160-
{
161-
ArgumentHelpName = "ipAddress"
162-
};
163-
_ipAddressOption.AddValidator(input =>
164-
{
165-
if (!System.Net.IPAddress.TryParse(input.Tokens[0].Value, out _))
166-
{
167-
input.ErrorMessage = $"{input.Tokens[0].Value} is not a valid IP address";
168-
}
169-
});
149+
_ipAddressOption = new(IpAddressOptionName, [])
150+
{
151+
Description = "The IP address for the proxy to bind to",
152+
HelpName = "ipAddress"
153+
};
154+
155+
// TODO: Fix validation for beta5
156+
// _ipAddressOption.Validators.Add(input => { ... });
170157
}
171158

172159
var result = _ipAddressOption.Parse(Environment.GetCommandLineArgs());
@@ -429,14 +416,18 @@ private void ConfigureCommand()
429416
_discoverOption,
430417
_envOption
431418
};
432-
options.AddRange(_plugins
433-
.SelectMany(p => p.GetOptions())
434-
// remove duplicates by comparing the option names
435-
.GroupBy(o => o.Name)
436-
.Select(g => g.First()));
437-
this.AddOptions(options.OrderByName());
438-
439-
AddGlobalOption(_logLevelOption!);
419+
options.AddRange(_plugins
420+
.SelectMany(p => p.GetOptions())
421+
// remove duplicates by comparing the option names
422+
.GroupBy(o => o.Name)
423+
.Select(g => g.First()));
424+
this.AddOptions(options.OrderByName());
425+
426+
// Store options for use in parsing methods
427+
Options = options.AsReadOnly();
428+
429+
// Add log level as a regular option instead of global option
430+
// In beta5, global options are handled by adding to the root command
440431

441432
var commands = new List<Command>
442433
{

DevProxy/Commands/MsGraphDbCommand.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ private void ConfigureCommand()
2323
this.SetAction(async (parseResult) =>
2424
{
2525
await GenerateMsGraphDbAsync();
26-
return 0;
2726
});
2827
}
2928

DevProxy/Commands/OutdatedCommand.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,18 @@ public OutdatedCommand(
2626
ConfigureCommand();
2727
}
2828

29-
private void ConfigureCommand()
30-
{
31-
var outdatedShortOption = new Option<bool>("--short", "Return version only");
32-
AddOption(outdatedShortOption);
33-
this.SetHandler(CheckVersionAsync, outdatedShortOption);
29+
private void ConfigureCommand()
30+
{
31+
var outdatedShortOption = new Option<bool>("--short")
32+
{
33+
Description = "Return version only"
34+
};
35+
this.Add(outdatedShortOption);
36+
this.SetAction((parseResult) =>
37+
{
38+
var versionOnly = parseResult.GetValue(outdatedShortOption);
39+
CheckVersionAsync(versionOnly);
40+
});
3441
}
3542

3643
private async Task CheckVersionAsync(bool versionOnly)

0 commit comments

Comments
 (0)