Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save shadyabhi/7c3c77a068fba7b58eb24463f62c6a50 to your computer and use it in GitHub Desktop.

Select an option

Save shadyabhi/7c3c77a068fba7b58eb24463f62c6a50 to your computer and use it in GitHub Desktop.
0001-MINOR-cli-print-parsed-command-when-not-found.patch
From 5bced85b9d68ef2972f4b649bba4358a0fcf5dca Mon Sep 17 00:00:00 2001
From: Abhijeet Rastogi <[email protected]>
Date: Thu, 17 Nov 2022 04:42:38 -0800
Subject: [PATCH] MINOR: cli: print parsed command when not found
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
It is useful because when we're passing data to runtime API,
specially via code, we can mistakenly send newlines leading to some
lines being wrongly interpretted as commands.
This is analogous to how its done in a shell, example bash.
```
$ not_found arg1
bash: not_found: command not found...
$
```
Real world example: Following the official docs to add a cert:-
```
echo -e "set ssl cert ./cert.pem <<\n$(cat ./cert.pem)\n" | socat stdio tcp4-connect:127.0.0.1:9999
```
Note, how the payload is sent via `<<\n$(cat ./cert.pem)\n`. If
`cert.pem` contains a newline between various PEM blocks, which is valid, the above
command would generate a flood of `Unknown command` messages for every
line sent after the first newline. As a new user, this detail is not clearly visible as
socket API doesn't say what exactly what was `unknown` about it. The cli
interface should be obvious around guiding user on "what do do next".
This commit changes that by printing the parsed cmd in output like `Unknown command: "<cmd>"`
so the user gets clear "next steps", like bash, regarding what indeed was the wrong
command that HAproxy couldn't interpret.
Previously:-
```
➤ echo -e "show version\nhelpp"| socat ./haproxy.sock - | head -n4
2.7-dev6
Unknown command, but maybe one of the following ones is a better match:
add map [@<ver>] <map> <key> <val> : add a map entry (payload supported instead of key/val)
```
Now:-
```
➤ echo -e "show version\nhelpp"| socat ./haproxy.sock - | head -n4
2.7-dev8-737bb7-156
Unknown command: 'helpp', but maybe one of the following ones is a better match:
add map [@<ver>] <map> <key> <val> : add a map entry (payload supported instead of key/val)
```
---
src/cli.c | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)
diff --git a/src/cli.c b/src/cli.c
index 4e7ae1496..2b3ea3a3c 100644
--- a/src/cli.c
+++ b/src/cli.c
@@ -170,10 +170,17 @@ static char *cli_gen_usage_msg(struct appctx *appctx, char * const *args)
chunk_reset(tmp);
if (ishelp) // this is the help message.
chunk_strcat(tmp, "The following commands are valid at this level:\n");
- else if (!length && (!args || !*args || !**args)) // no match
- chunk_strcat(tmp, "Unknown command. Please enter one of the following commands only:\n");
- else // partial match
- chunk_strcat(tmp, "Unknown command, but maybe one of the following ones is a better match:\n");
+ else {
+ chunk_strcat(tmp, "Unknown command: '");
+ if (args && *args)
+ chunk_strcat(tmp, *args);
+ chunk_strcat(tmp, "'");
+
+ if (!length && (!args || !*args || !**args)) // no match
+ chunk_strcat(tmp, ". Please enter one of the following commands only:\n");
+ else // partial match
+ chunk_strcat(tmp, ", but maybe one of the following ones is a better match:\n");
+ }
for (idx = 0; idx < CLI_MAX_MATCHES; idx++) {
matches[idx].kw = NULL;
--
2.27.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment