How to use a shell script to stop CLI if one arg matches

yoursweater :

Basically, I'm trying to disable a CLI's "delete" function in order to prevent myself (and others) from inadvertently deleting our environments. I want to basically intercept amplify delete (including subsequent arguments) and echo something like you are not supposed to do this.

 amplify() {
   local -a args=( )
   for arg; do
     # if arg == delete
     echo "This command is disabled."
   done
 }

The catch is that I still want the rest of the CLI arguments to work. So if I type in amplify status, that should run. Just not amplify delete or amplify delete --arg2 --arg3 --etc

Inian :

You can write a wrapper over the amplify() function like you had and check if the argument is set to delete

amplify() {
   [ "$1" = "delete" ] && { >&2 printf '%s\n' "This command is disabled."; return; }
   command amplify "$@"
}

The command before amplify ensures this time, the binary is searched int the PATH variable for execution.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=6613&siteId=1