7
7
8
8
namespace Microsoft . DevProxy ;
9
9
10
- internal class ProxyHost {
10
+ internal class ProxyHost
11
+ {
11
12
private Option < int ? > _portOption ;
12
13
private Option < string ? > _ipAddressOption ;
13
14
private Option < LogLevel ? > _logLevelOption ;
@@ -19,37 +20,61 @@ internal class ProxyHost {
19
20
20
21
private static bool _configFileResolved = false ;
21
22
private static string _configFile = "devproxyrc.json" ;
22
- public static string ConfigFile {
23
- get {
24
- if ( _configFileResolved ) {
23
+ public static string ConfigFile
24
+ {
25
+ get
26
+ {
27
+ if ( _configFileResolved )
28
+ {
25
29
return _configFile ;
26
30
}
27
-
28
- if ( _configFileOption is null ) {
31
+
32
+ if ( _configFileOption is null )
33
+ {
29
34
_configFileOption = new Option < string ? > ( "--config-file" , "The path to the configuration file" ) ;
30
35
_configFileOption . AddAlias ( "-c" ) ;
31
36
_configFileOption . ArgumentHelpName = "configFile" ;
32
- _configFileOption . AddValidator ( input => {
37
+ _configFileOption . AddValidator ( input =>
38
+ {
33
39
var filePath = ProxyUtils . ReplacePathTokens ( input . Tokens . First ( ) . Value ) ;
34
- if ( String . IsNullOrEmpty ( filePath ) ) {
40
+ if ( string . IsNullOrEmpty ( filePath ) )
41
+ {
35
42
return ;
36
43
}
37
44
38
- if ( ! File . Exists ( filePath ) ) {
39
- input . ErrorMessage = $ "File { filePath } does not exist";
45
+ if ( ! File . Exists ( filePath ) )
46
+ {
47
+ input . ErrorMessage = $ "Configuration file { filePath } does not exist";
40
48
}
41
49
} ) ;
42
50
}
43
51
44
52
var result = _configFileOption . Parse ( Environment . GetCommandLineArgs ( ) ) ;
45
- var configFile = result . GetValueForOption < string ? > ( _configFileOption ) ;
46
- if ( configFile is not null ) {
53
+ // since we're parsing all args, and other options are not instantiated yet
54
+ // we're getting here a bunch of other errors, so we only need to look for
55
+ // errors related to the config file option
56
+ var error = result . Errors . Where ( e => e . SymbolResult ? . Symbol == _configFileOption ) . FirstOrDefault ( ) ;
57
+ if ( error is not null )
58
+ {
59
+ // Logger is not available here yet so we need to fallback to Console
60
+ var color = Console . ForegroundColor ;
61
+ Console . ForegroundColor = ConsoleColor . Red ;
62
+ Console . Error . WriteLine ( error . Message ) ;
63
+ Console . ForegroundColor = color ;
64
+ Environment . Exit ( 1 ) ;
65
+ }
66
+
67
+ var configFile = result . GetValueForOption ( _configFileOption ) ;
68
+ if ( configFile is not null )
69
+ {
47
70
_configFile = configFile ;
48
71
}
49
- else {
72
+ else
73
+ {
50
74
// if there's no config file in the current working folder
51
75
// fall back to the default config file in the app folder
52
- if ( ! File . Exists ( _configFile ) ) {
76
+ if ( ! File . Exists ( _configFile ) )
77
+ {
53
78
_configFile = "~appFolder/devproxyrc.json" ;
54
79
}
55
80
}
@@ -62,7 +87,8 @@ public static string ConfigFile {
62
87
}
63
88
}
64
89
65
- public ProxyHost ( ) {
90
+ public ProxyHost ( )
91
+ {
66
92
_portOption = new Option < int ? > ( "--port" , "The port for the proxy to listen on" ) ;
67
93
_portOption . AddAlias ( "-p" ) ;
68
94
_portOption . ArgumentHelpName = "port" ;
@@ -71,16 +97,20 @@ public ProxyHost() {
71
97
{
72
98
ArgumentHelpName = "ipAddress"
73
99
} ;
74
- _ipAddressOption . AddValidator ( input => {
75
- if ( ! IPAddress . TryParse ( input . Tokens . First ( ) . Value , out var ipAddress ) ) {
100
+ _ipAddressOption . AddValidator ( input =>
101
+ {
102
+ if ( ! IPAddress . TryParse ( input . Tokens . First ( ) . Value , out var ipAddress ) )
103
+ {
76
104
input . ErrorMessage = $ "{ input . Tokens . First ( ) . Value } is not a valid IP address";
77
105
}
78
106
} ) ;
79
107
80
108
_logLevelOption = new Option < LogLevel ? > ( "--log-level" , $ "Level of messages to log. Allowed values: { string . Join ( ", " , Enum . GetNames ( typeof ( LogLevel ) ) ) } ") ;
81
109
_logLevelOption . ArgumentHelpName = "logLevel" ;
82
- _logLevelOption . AddValidator ( input => {
83
- if ( ! Enum . TryParse < LogLevel > ( input . Tokens . First ( ) . Value , true , out var logLevel ) ) {
110
+ _logLevelOption . AddValidator ( input =>
111
+ {
112
+ if ( ! Enum . TryParse < LogLevel > ( input . Tokens . First ( ) . Value , true , out var logLevel ) )
113
+ {
84
114
input . ErrorMessage = $ "{ input . Tokens . First ( ) . Value } is not a valid log level. Allowed values are: { string . Join ( ", " , Enum . GetNames ( typeof ( LogLevel ) ) ) } ";
85
115
}
86
116
} ) ;
@@ -98,17 +128,20 @@ public ProxyHost() {
98
128
_rateOption = new Option < int ? > ( "--failure-rate" , "The percentage of chance that a request will fail" ) ;
99
129
_rateOption . AddAlias ( "-f" ) ;
100
130
_rateOption . ArgumentHelpName = "failure rate" ;
101
- _rateOption . AddValidator ( ( input ) => {
131
+ _rateOption . AddValidator ( ( input ) =>
132
+ {
102
133
int ? value = input . GetValueForOption ( _rateOption ) ;
103
- if ( value . HasValue && ( value < 0 || value > 100 ) ) {
134
+ if ( value . HasValue && ( value < 0 || value > 100 ) )
135
+ {
104
136
input . ErrorMessage = $ "{ value } is not a valid failure rate. Specify a number between 0 and 100";
105
137
}
106
138
} ) ;
107
139
108
140
ProxyCommandHandler . Configuration . ConfigFile = ConfigFile ;
109
141
}
110
142
111
- public RootCommand GetRootCommand ( ILogger logger ) {
143
+ public RootCommand GetRootCommand ( ILogger logger )
144
+ {
112
145
var command = new RootCommand {
113
146
_portOption ,
114
147
_ipAddressOption ,
0 commit comments