77
88namespace Microsoft . DevProxy ;
99
10- internal class ProxyHost {
10+ internal class ProxyHost
11+ {
1112 private Option < int ? > _portOption ;
1213 private Option < string ? > _ipAddressOption ;
1314 private Option < LogLevel ? > _logLevelOption ;
@@ -19,37 +20,61 @@ internal class ProxyHost {
1920
2021 private static bool _configFileResolved = false ;
2122 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+ {
2529 return _configFile ;
2630 }
27-
28- if ( _configFileOption is null ) {
31+
32+ if ( _configFileOption is null )
33+ {
2934 _configFileOption = new Option < string ? > ( "--config-file" , "The path to the configuration file" ) ;
3035 _configFileOption . AddAlias ( "-c" ) ;
3136 _configFileOption . ArgumentHelpName = "configFile" ;
32- _configFileOption . AddValidator ( input => {
37+ _configFileOption . AddValidator ( input =>
38+ {
3339 var filePath = ProxyUtils . ReplacePathTokens ( input . Tokens . First ( ) . Value ) ;
34- if ( String . IsNullOrEmpty ( filePath ) ) {
40+ if ( string . IsNullOrEmpty ( filePath ) )
41+ {
3542 return ;
3643 }
3744
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";
4048 }
4149 } ) ;
4250 }
4351
4452 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+ {
4770 _configFile = configFile ;
4871 }
49- else {
72+ else
73+ {
5074 // if there's no config file in the current working folder
5175 // fall back to the default config file in the app folder
52- if ( ! File . Exists ( _configFile ) ) {
76+ if ( ! File . Exists ( _configFile ) )
77+ {
5378 _configFile = "~appFolder/devproxyrc.json" ;
5479 }
5580 }
@@ -62,7 +87,8 @@ public static string ConfigFile {
6287 }
6388 }
6489
65- public ProxyHost ( ) {
90+ public ProxyHost ( )
91+ {
6692 _portOption = new Option < int ? > ( "--port" , "The port for the proxy to listen on" ) ;
6793 _portOption . AddAlias ( "-p" ) ;
6894 _portOption . ArgumentHelpName = "port" ;
@@ -71,16 +97,20 @@ public ProxyHost() {
7197 {
7298 ArgumentHelpName = "ipAddress"
7399 } ;
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+ {
76104 input . ErrorMessage = $ "{ input . Tokens . First ( ) . Value } is not a valid IP address";
77105 }
78106 } ) ;
79107
80108 _logLevelOption = new Option < LogLevel ? > ( "--log-level" , $ "Level of messages to log. Allowed values: { string . Join ( ", " , Enum . GetNames ( typeof ( LogLevel ) ) ) } ") ;
81109 _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+ {
84114 input . ErrorMessage = $ "{ input . Tokens . First ( ) . Value } is not a valid log level. Allowed values are: { string . Join ( ", " , Enum . GetNames ( typeof ( LogLevel ) ) ) } ";
85115 }
86116 } ) ;
@@ -98,17 +128,20 @@ public ProxyHost() {
98128 _rateOption = new Option < int ? > ( "--failure-rate" , "The percentage of chance that a request will fail" ) ;
99129 _rateOption . AddAlias ( "-f" ) ;
100130 _rateOption . ArgumentHelpName = "failure rate" ;
101- _rateOption . AddValidator ( ( input ) => {
131+ _rateOption . AddValidator ( ( input ) =>
132+ {
102133 int ? value = input . GetValueForOption ( _rateOption ) ;
103- if ( value . HasValue && ( value < 0 || value > 100 ) ) {
134+ if ( value . HasValue && ( value < 0 || value > 100 ) )
135+ {
104136 input . ErrorMessage = $ "{ value } is not a valid failure rate. Specify a number between 0 and 100";
105137 }
106138 } ) ;
107139
108140 ProxyCommandHandler . Configuration . ConfigFile = ConfigFile ;
109141 }
110142
111- public RootCommand GetRootCommand ( ILogger logger ) {
143+ public RootCommand GetRootCommand ( ILogger logger )
144+ {
112145 var command = new RootCommand {
113146 _portOption ,
114147 _ipAddressOption ,
0 commit comments