valkey icon indicating copy to clipboard operation
valkey copied to clipboard

[BUG] valkey-cli --csv / --json do not produce correct output

Open utdrmac opened this issue 7 months ago • 3 comments

Describe the bug The valkey-cli has options for --csv and --json to produce output formatted in such a way. This does not work in all cases.

To reproduce

$ valkey-cli --version
valkey-cli 8.1.1

$ valkey-cli --csv -u redis://app:[email protected] info stats | head -n5
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Stats
total_connections_received:3
total_commands_processed:5
instantaneous_ops_per_sec:0
total_net_input_bytes:195
...

Expected behavior

$ valkey-cli --csv -u redis://app:[email protected] info stats | head -n5
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Stats
"total_connections_received","3"
"total_commands_processed","5"
"instantaneous_ops_per_sec","0"
"total_net_input_bytes","195"
...

A description of what you expected to happen.

I expect --csv to produce actual csv results. Same goes for --json.

utdrmac avatar May 05 '25 22:05 utdrmac

Documentation: https://valkey.io/topics/cli/#csv-output

Note that the --csv flag will only work on a single command, not the entirety of a DB as an export.

Seems like it works on LRANGE and similar commands and not with info

(25-05-06 20:57:49) <0> [~/Projects/valkey/src]% ./valkey-cli LPUSH mylist a b c d     
(integer) 8

(25-05-06 20:57:55) <0> [~/Projects/valkey/src] % ./valkey-cli --json LRANGE mylist 0 -1
["d","c","b","a","d","c","b","a"]

(25-05-06 20:58:00) <0> [~/Projects/valkey/src]  % ./valkey-cli --csv LRANGE mylist 0 -1
"d","c","b","a","d","c","b","a"

(25-05-06 20:58:07) <0> [~/Projects/valkey/src] % ./valkey-cli LRANGE mylist 0 -1 
1) "d"
2) "c"
3) "b"
4) "a"
5) "d"
6) "c"
7) "b"
8) "a"

roshkhatri avatar May 06 '25 21:05 roshkhatri

Your output for LPUSH, and LRANGE are acceptable. The results for CONFIG are incorrect. This should produce 1 line per config setting, and not clobber them all together into a single line.

$ valkey-cli -u redis://app:[email protected] CONFIG GET '*'
client-query-buffer-limit
1073741824
cluster-migration-barrier
1
slave-announce-port
0
zset-max-listpack-value
64
...

$ valkey-cli --csv -u redis://app:[email protected] CONFIG GET '*'
"client-query-buffer-limit","1073741824","cluster-migration-barrier","1","slave-announce-port","0","zset-max-listpack-value","64","replicaof","","set-max-intset-entries","512","tls-auth-clients","yes","aof_rewrite_cpulist","",...

Expected result:

$ valkey-cli --csv -u redis://app:[email protected] CONFIG GET '*'
"client-query-buffer-limit","1073741824"
"cluster-migration-barrier","1"
"slave-announce-port","0"
"zset-max-listpack-value","64"
"replicaof",""
"set-max-intset-entries","512"
...

utdrmac avatar May 06 '25 21:05 utdrmac

I think these are in the array format where --csv just converts the array into csv format where it is not aware of the structure of the array.

I think we would need to add a new flag/ option like --smart-csv which does that so that we do not cause a breaking change

Although --json option works well for CONFIG GET '*'

 % ./src/valkey-cli --json config get "*" | jq .
{
  "sanitize-dump-payload": "no",
  "slave-announce-port": "0",
  "list-compress-depth": "0",
  "aof-timestamp-enabled": "no",
  "ignore-warnings": "",
  "repl-disable-tcp-nodelay": "no",
  "hash-max-listpack-value": "64",
  "list-max-ziplist-size": "-2",
.
.
.
.
.
  "supervised": "no",
  "active-defrag-cycle-max": "25",
  "maxmemory-eviction-tenacity": "10",
  "socket-mark-id": "0",
  "slave-read-only": "yes"
}

roshkhatri avatar May 06 '25 22:05 roshkhatri

It would be very useful for commamd like cluster info to get json output. Very usefull in scripting.

Tchirana avatar Jun 22 '25 17:06 Tchirana