From be03176ed635f738831a24b211f3e9284e180044 Mon Sep 17 00:00:00 2001 From: Ilia Rostovtsev Date: Fri, 28 May 2021 14:16:31 +0300 Subject: [PATCH] Check if subcommand exists --- bin/webmin | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/bin/webmin b/bin/webmin index 069953aef..7ddb48112 100755 --- a/bin/webmin +++ b/bin/webmin @@ -62,6 +62,12 @@ sub run_command { # Figure out the Webmin root directory my $root = root($optref->{'config'}); + my (@commands) = list_commands($optref); + if (! grep( /^$subcmd$/, @commands ) ) { + say RED, "Error: ", RESET, "Command \`$subcmd\` doesn't exist", RESET; + exit 1; + } + my $command_path = get_command_path($root, $subcmd); # Merge the options @@ -74,7 +80,8 @@ sub run_command { # Try to exit with the passed through exit code (rarely used, but # why not?) if ($? == -1) { - die RED, "Failed to execute $command_path: $!", RESET; + say RED, "Error: ", RESET, "Failed to execute \`$command_path\`: $!"; + exit 1; } else { exit $? >> 8; } @@ -126,6 +133,8 @@ sub list_commands { my ($optref) = @_; my $root = root($optref->{'config'}); + my @commands; + # Find and list global commands for my $command (glob ("$root/bin/*")) { my ($bin, $path) = fileparse($command); @@ -140,10 +149,15 @@ sub list_commands { -input => $command, -exitval => "NOEXIT"); } else { - # Just list the names - say "$bin"; + if (wantarray) { + push(@commands, $bin); + } else { + # Just list the names + say "$bin"; + } } - } + } + my @modules; # Find all module directories with something in bin for my $command (glob ("$root/*/bin/*")) { @@ -157,10 +171,18 @@ sub list_commands { -input => $command, -exitval => "NOEXIT"); } else { - # Just list the names - say "$module-$bin"; + if (wantarray) { + push(@modules, "$module-$bin"); + } else { + # Just list the names + say "$module-$bin"; + } } } + + if (wantarray) { + return (@commands, @modules); + } } # Display either a short usage message (--help) or a full manual (--man)