-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathDevProxyConfigOptions.cs
More file actions
215 lines (188 loc) · 7.04 KB
/
DevProxyConfigOptions.cs
File metadata and controls
215 lines (188 loc) · 7.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
using DevProxy.Abstractions.Proxy;
using DevProxy.Abstractions.Utils;
using System.CommandLine;
using System.CommandLine.Parsing;
namespace DevProxy.Commands;
sealed class DevProxyConfigOptions : RootCommand
{
public string? ConfigFile
{
get
{
var configFile = _parseResult?.GetValueOrDefault<string?>(DevProxyCommand.ConfigFileOptionName);
return configFile is not null ?
Path.GetFullPath(ProxyUtils.ReplacePathTokens(configFile)) :
null;
}
}
public int? ApiPort => _parseResult?.GetValueOrDefault<int?>(DevProxyCommand.ApiPortOptionName);
public bool? AsSystemProxy => _parseResult?.GetValueOrDefault<bool?>(DevProxyCommand.AsSystemProxyOptionName);
public int? Port => _parseResult?.GetValueOrDefault<int?>(DevProxyCommand.PortOptionName);
public bool Discover => _parseResult?.GetValueOrDefault<bool?>(DevProxyCommand.DiscoverOptionName) ?? false;
public string? IPAddress => _parseResult?.GetValueOrDefault<string?>(DevProxyCommand.IpAddressOptionName);
public bool IsStdioMode => _parseResult?.CommandResult.Command.Name == "stdio";
public OutputFormat? Output => _parseResult?.GetValueOrDefault<OutputFormat?>(DevProxyCommand.OutputOptionName);
public LogLevel? LogLevel => _parseResult?.GetValueOrDefault<LogLevel?>(DevProxyCommand.LogLevelOptionName);
public List<string>? UrlsToWatch
{
get
{
var value = _parseResult?.GetValueOrDefault<List<string>?>(DevProxyCommand.UrlsToWatchOptionName);
if (value is null || value.Count == 0)
{
return null;
}
return value;
}
}
private ParseResult? _parseResult;
public DevProxyConfigOptions()
{
// here we only use custom parsers for the initial options
// general parsing and error handling is done in the DevProxyCommand
var configFileOption = new Option<string?>(DevProxyCommand.ConfigFileOptionName, "-c")
{
CustomParser = result =>
{
if (!result.Tokens.Any())
{
return null;
}
var value = result.Tokens[0].Value;
if (string.IsNullOrEmpty(value))
{
return null;
}
// Replace path tokens in the configuration file path
value = ProxyUtils.ReplacePathTokens(value);
return value;
}
};
var ipAddressOption = new Option<string?>(DevProxyCommand.IpAddressOptionName)
{
CustomParser = result =>
{
if (!result.Tokens.Any())
{
return null;
}
var value = result.Tokens[0].Value;
if (string.IsNullOrEmpty(value))
{
return null;
}
if (System.Net.IPAddress.TryParse(value, out _))
{
return value;
}
else
{
return null;
}
}
};
var urlsToWatchOption = new Option<List<string>?>(DevProxyCommand.UrlsToWatchOptionName, "-u")
{
AllowMultipleArgumentsPerToken = true,
Arity = ArgumentArity.ZeroOrMore
};
var logLevelOption = new Option<LogLevel?>(DevProxyCommand.LogLevelOptionName)
{
CustomParser = result =>
{
if (!result.Tokens.Any())
{
return null;
}
if (Enum.TryParse<LogLevel>(result.Tokens[0].Value, true, out var logLevel))
{
return logLevel;
}
else
{
return null;
}
}
};
var outputOption = new Option<OutputFormat?>(DevProxyCommand.OutputOptionName)
{
CustomParser = result =>
{
if (!result.Tokens.Any())
{
return null;
}
if (Enum.TryParse<OutputFormat>(result.Tokens[0].Value, true, out var output))
{
return output;
}
return null;
}
};
var apiPortOption = new Option<int?>(DevProxyCommand.ApiPortOptionName);
var asSystemProxyOption = new Option<bool?>(DevProxyCommand.AsSystemProxyOptionName);
var portOption = new Option<int?>(DevProxyCommand.PortOptionName, "-p");
var discoverOption = new Option<bool>(DevProxyCommand.DiscoverOptionName, "--discover")
{
Arity = ArgumentArity.Zero
};
var noColorOption = new Option<bool>(DevProxyCommand.NoColorOptionName)
{
Arity = ArgumentArity.Zero
};
var options = new List<Option>
{
apiPortOption,
asSystemProxyOption,
ipAddressOption,
configFileOption,
portOption,
urlsToWatchOption,
logLevelOption,
outputOption,
discoverOption,
noColorOption
};
this.AddOptions(options.OrderByName());
// Add stdio subcommand with config-file option so it can be parsed early
var stdioConfigFileOption = new Option<string?>(DevProxyCommand.ConfigFileOptionName, "-c")
{
CustomParser = result =>
{
if (!result.Tokens.Any())
{
return null;
}
var value = result.Tokens[0].Value;
if (string.IsNullOrEmpty(value))
{
return null;
}
value = ProxyUtils.ReplacePathTokens(value);
return value;
}
};
var stdioCommand = new Command("stdio", """
Proxy stdin/stdout/stderr of local executables.
Dev Proxy intercepts and processes the stdio streams of the specified command,
applying configured plugins (mocking, error simulation, etc.) to the traffic.
Logs are written to a timestamped file (devproxy-stdio-YYYYMMDD-HHmmss.log)
to avoid interfering with the proxied streams.
Usage errors and exceptions are written to stderr.
Examples:
devproxy stdio npx -y @devproxy/mcp Proxy MCP server
devproxy stdio node server.js Proxy Node.js app
devproxy stdio -c myconfig.json node app.js With custom config
""")
{
stdioConfigFileOption,
// Add a catch-all argument to consume remaining args (command to execute)
new Argument<string[]>("command") { Arity = ArgumentArity.ZeroOrMore }
};
Add(stdioCommand);
}
public void ParseOptions(string[] args)
{
_parseResult = Parse(args);
}
}