@@ -102,11 +102,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
102102 // Validate the --threshold-* options.
103103 $ thresholdUp = $ input ->getOption ('threshold-up ' );
104104 if ($ thresholdUp !== null ) {
105- $ thresholdUp = $ this ->validateThreshold ($ thresholdUp );
105+ $ thresholdUp = $ this ->validateThreshold ($ thresholdUp, ' threshold-up ' );
106106 }
107107 $ thresholdDown = $ input ->getOption ('threshold-down ' );
108108 if ($ thresholdDown !== null ) {
109- $ thresholdDown = $ this ->validateThreshold ($ thresholdDown );
109+ $ thresholdDown = $ this ->validateThreshold ($ thresholdDown, ' threshold-down ' );
110110 }
111111
112112 // Validate the --duration-* options.
@@ -133,11 +133,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
133133 $ instanceLimit = $ defaults ['instances ' ]['max ' ];
134134 $ instancesMin = $ input ->getOption ('instances-min ' );
135135 if ($ instancesMin !== null ) {
136- $ instancesMin = $ this ->validateInstanceCount ($ instancesMin , $ instanceLimit );
136+ $ instancesMin = $ this ->validateInstanceCount ($ instancesMin , $ instanceLimit, ' instances-min ' );
137137 }
138138 $ instancesMax = $ input ->getOption ('instances-max ' );
139139 if ($ instancesMax !== null ) {
140- $ instancesMax = $ this ->validateInstanceCount ($ instancesMax , $ instanceLimit );
140+ $ instancesMax = $ this ->validateInstanceCount ($ instancesMax , $ instanceLimit, ' instances-max ' );
141141 }
142142
143143 $ this ->stdErr ->writeln ('' );
@@ -173,7 +173,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
173173 $ app = $ serviceNames [$ selectedService ];
174174 }
175175 // Configure the selected service
176- $ service = $ services [$ app ];
176+ $ serviceName = $ app ;
177+ $ service = $ services [$ serviceName ];
177178
178179 $ this ->stdErr ->writeln ('' );
179180 $ this ->stdErr ->writeln ('<options=bold> ' . ucfirst ($ this ->typeName ($ service )) . ': </><options=bold,underscore> ' . $ serviceName . '</> ' );
@@ -191,7 +192,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
191192 if ($ thresholdUp === null ) {
192193 // Ask for scaling up threshold
193194 $ default = $ defaults ['triggers ' ][$ metric ]['up ' ]['threshold ' ];
194- $ thresholdUp = $ questionHelper ->askInput ('Enter the threshold for scaling up ' , $ default , [], $ this ->validateThreshold );
195+ $ value = $ questionHelper ->askInput ('Enter the threshold for scaling up ' , $ default );
196+ $ thresholdUp = $ this ->validateThreshold ($ value , 'threshold-up ' );
195197 $ this ->stdErr ->writeln ('' );
196198 }
197199 $ updates [$ serviceName ]['threshold-up ' ] = $ thresholdUp ;
@@ -203,15 +205,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
203205 $ default = array_search ($ this ->formatDuration ($ defaults ['triggers ' ][$ metric ]['up ' ]['duration ' ]), $ choices );
204206 $ text = 'Enter the duration for scaling up evaluation: ' . "\n" . 'Default: <question> ' . $ default . '</question> ' ;
205207 $ choice = $ questionHelper ->choose ($ choices , $ text , $ default );
206- $ durationUp = $ this ->validateDuration ($ choices [$ choice ]);
208+ $ durationUp = $ this ->validateDuration ($ choices [$ choice ], ' duration-up ' );
207209 }
208210 $ updates [$ serviceName ]['duration-up ' ] = $ durationUp ;
209211
210-
211212 if ($ thresholdDown === null ) {
212213 // Ask for scaling down threshold
213- $ value = $ questionHelper ->askInput ('Enter the threshold for scaling down ' , $ defaults ['triggers ' ][$ metric ]['down ' ]['threshold ' ]);
214- $ thresholdDown = $ this ->validateThreshold ($ value );
214+ $ default = $ defaults ['triggers ' ][$ metric ]['down ' ]['threshold ' ];
215+ $ value = $ questionHelper ->askInput ('Enter the threshold for scaling down ' , $ default );
216+ $ thresholdDown = $ this ->validateThreshold ($ value , 'threshold-down ' );
215217 $ this ->stdErr ->writeln ('' );
216218 }
217219 $ updates [$ serviceName ]['threshold-down ' ] = $ thresholdDown ;
@@ -222,7 +224,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
222224 $ default = array_search ($ this ->formatDuration ($ defaults ['triggers ' ][$ metric ]['down ' ]['duration ' ]), $ choices );
223225 $ text = 'Enter the duration for scaling down evaluation: ' . "\n" . 'Default: <question> ' . $ default . '</question> ' ;
224226 $ choice = $ questionHelper ->choose ($ choices , $ text , $ default );
225- $ durationDown = $ this ->validateDuration ($ choices [$ choice ]);
227+ $ durationDown = $ this ->validateDuration ($ choices [$ choice ], ' duration-down ' );
226228 }
227229 $ updates [$ serviceName ]['duration-down ' ] = $ durationDown ;
228230
@@ -236,8 +238,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
236238
237239 if ($ instancesMin === null ) {
238240 // Ask for instance count limits
239- $ value = $ questionHelper ->askInput ('Enter the minimum number of instances ' , 1 );
240- $ instancesMin = $ this ->validateInstanceCount ($ value , $ instanceLimit );
241+ $ instancesMin = $ questionHelper ->askInput ('Enter the minimum number of instances ' , 1 , [], [ $ this , ' validateInstanceCount ' , $ instanceLimit ] );
242+ // $instancesMin = $this->validateInstanceCount($value, $instanceLimit);
241243 $ this ->stdErr ->writeln ('' );
242244 }
243245 $ updates [$ serviceName ]['instances-min ' ] = $ instancesMin ;
@@ -255,7 +257,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
255257 $ default = array_search ($ this ->formatDuration ($ defaults ['scale_cooldown ' ]['up ' ]), $ choices );
256258 $ text = 'Enter the duration of the cool-down period for scaling up: ' . "\n" . 'Default: <question> ' . $ default . '</question> ' ;
257259 $ choice = $ questionHelper ->choose ($ choices , $ text , $ default );
258- $ cooldownUp = $ this ->validateDuration ($ choices [$ choice ]);
260+ $ cooldownUp = $ this ->validateDuration ($ choices [$ choice ], ' cooldown-up ' );
259261 }
260262 $ updates [$ serviceName ]['cooldown-up ' ] = $ cooldownUp ;
261263
@@ -264,7 +266,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
264266 $ default = array_search ($ this ->formatDuration ($ defaults ['scale_cooldown ' ]['down ' ]), $ choices );
265267 $ text = 'Enter the duration of the cool-down period for scaling down: ' . "\n" . 'Default: <question> ' . $ default . '</question> ' ;
266268 $ choice = $ questionHelper ->choose ($ choices , $ text , $ default );
267- $ cooldownDown = $ this ->validateDuration ($ choices [$ choice ]);
269+ $ cooldownDown = $ this ->validateDuration ($ choices [$ choice ], ' cooldown-down ' );
268270 }
269271 $ updates [$ serviceName ]['cooldown-down ' ] = $ cooldownDown ;
270272
@@ -278,7 +280,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
278280 $ serviceNames = array_keys ($ services );
279281
280282 if ($ app === null ) {
281- // TODO: abort
283+ $ this -> stdErr -> writeln ( ' <error>The --app options is required when not running interactively.</error> ' );
282284 return 1 ;
283285 }
284286 // Configure the selected service
@@ -534,7 +536,8 @@ protected function validateService($value, $services)
534536 if (array_key_exists ($ value , $ services )) {
535537 return $ value ;
536538 }
537- throw new InvalidArgumentException (sprintf ('Invalid service name <error>%s</error>: must be one of %s ' , $ value , implode (', ' , $ services )));
539+ $ serviceNames = array_keys ($ services );
540+ throw new InvalidArgumentException (sprintf ('Invalid service name <error>%s</error>. Available services: %s ' , $ value , implode (', ' , $ serviceNames )));
538541 }
539542
540543 /**
@@ -551,7 +554,8 @@ protected function validateMetric($value, $metrics)
551554 if (array_key_exists ($ value , $ metrics )) {
552555 return $ value ;
553556 }
554- throw new InvalidArgumentException (sprintf ('Invalid metric name <error>%s</error>: must be one of %s ' , $ value , implode (', ' , array_keys ($ metrics ))));
557+ $ metricNames = array_keys ($ metrics );
558+ throw new InvalidArgumentException (sprintf ('Invalid metric name <error>%s</error>. Available metrics: %s ' , $ value , implode (', ' , $ metricNames )));
555559 }
556560
557561 /**
@@ -586,14 +590,22 @@ protected function validateBoolean($value)
586590 *
587591 * @return float
588592 */
589- protected function validateThreshold ($ value )
593+ protected function validateThreshold ($ value, string $ context = '' )
590594 {
591595 $ threshold = (float ) $ value ;
592596 if ($ threshold <= 0 ) {
593- throw new InvalidArgumentException (sprintf ('Invalid threshold <error>%s</error>: must be greater than 0. ' , $ value ));
597+ $ message = sprintf ('Invalid threshold <error>%s</error>: must be greater than 0 ' , $ value );
598+ if ($ context ) {
599+ $ message .= sprintf (' for %s ' , $ context );
600+ }
601+ throw new InvalidArgumentException ($ message );
594602 }
595603 if ($ threshold > 100 ) {
596- throw new InvalidArgumentException (sprintf ('Invalid threshold <error>%s</error>: must be smaller than 100. ' , $ value ));
604+ $ message = sprintf ('Invalid threshold <error>%s</error>: must be 100 or less ' , $ value );
605+ if ($ context ) {
606+ $ message .= sprintf (' for %s ' , $ context );
607+ }
608+ throw new InvalidArgumentException ($ message );
597609 }
598610 return $ threshold ;
599611 }
@@ -616,10 +628,15 @@ protected function validateThreshold($value)
616628 *
617629 * @return int
618630 */
619- protected function validateDuration ($ value )
631+ protected function validateDuration ($ value, string $ context = '' )
620632 {
621633 if (!isset (self ::$ validDurations [$ value ])) {
622- throw new InvalidArgumentException (sprintf ('Invalid duration <error>%s</error>: must be one of %s ' , $ value , implode (', ' , array_keys (self ::$ validDurations ))));
634+ $ durations = array_keys (self ::$ validDurations );
635+ $ message = sprintf ('Invalid duration <error>%s</error>: must be one of %s ' , $ value , implode (', ' ,$ durations ));
636+ if ($ context ) {
637+ $ message .= sprintf (' for %s ' , $ context );
638+ }
639+ throw new InvalidArgumentException ($ message );
623640 }
624641 return self ::$ validDurations [$ value ];
625642 }
@@ -652,14 +669,22 @@ protected function typeName($service)
652669 *
653670 * @return int
654671 */
655- protected function validateInstanceCount ($ value , $ limit )
672+ protected function validateInstanceCount ($ value , $ limit, string $ context = '' )
656673 {
657674 $ count = (int ) $ value ;
658675 if ($ count != $ value || $ value <= 0 ) {
659- throw new InvalidArgumentException (sprintf ('Invalid instance count <error>%s</error>: it must be an integer greater than 0. ' , $ value ));
676+ $ message = sprintf ('Invalid instance count <error>%s</error>: it must be an integer greater than 0 ' , $ value );
677+ if ($ context ) {
678+ $ message .= sprintf (' for %s ' , $ context );
679+ }
680+ throw new InvalidArgumentException ($ message );
660681 }
661682 if ($ limit !== null && $ count > $ limit ) {
662- throw new InvalidArgumentException (sprintf ('The instance count <error>%d</error> exceeds the limit %d. ' , $ count , $ limit ));
683+ $ message = sprintf ('The instance count <error>%d</error> exceeds the limit %d ' , $ count , $ limit );
684+ if ($ context ) {
685+ $ message .= sprintf (' for %s ' , $ context );
686+ }
687+ throw new InvalidArgumentException ($ message );
663688 }
664689 return $ count ;
665690 }
0 commit comments