Skip to content

Commit b7bd98a

Browse files
authored
Implement the help and version options for redis-cli (#2)
1 parent 3550951 commit b7bd98a

File tree

4 files changed

+180
-6
lines changed

4 files changed

+180
-6
lines changed

redis-cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ license = "Apache-2.0"
77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

99
[dependencies]
10+
redis-core = { path = "../redis-core" }

redis-cli/src/help.rs

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
use std::process::exit;
2+
use redis_core::version;
3+
4+
pub(crate) fn print_usage(code: i32) {
5+
const REDIS_CLI_AUTH_ENV: &str = "REDISCLI_AUTH";
6+
const REDIS_CLI_DEFAULT_PIPE_TIMEOUT: i32 = 30;
7+
const USE_OPENSSL: bool = false;
8+
const TLS1_3_VERSION: bool = false;
9+
let redis_cli_version = version::redis_cli_version();
10+
11+
let mut usage = format!("{redis_cli_version}
12+
13+
Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]
14+
-h <hostname> Server hostname (default: 127.0.0.1).
15+
-p <port> Server port (default: 6379).
16+
-s <socket> Server socket (overrides hostname and port).
17+
-a <password> Password to use when connecting to the server.
18+
You can also use the {REDIS_CLI_AUTH_ENV} environment
19+
variable to pass this password more safely
20+
(if both are used, this argument takes precedence).
21+
--user <username> Used to send ACL style 'AUTH username pass'. Needs -a.
22+
--pass <password> Alias of -a for consistency with the new --user option.
23+
--askpass Force user to input password with mask from STDIN.
24+
If this argument is used, '-a' and {REDIS_CLI_AUTH_ENV}
25+
environment variable will be ignored.
26+
-u <uri> Server URI.
27+
-r <repeat> Execute specified command N times.
28+
-i <interval> When -r is used, waits <interval> seconds per command.
29+
It is possible to specify sub-second times like -i 0.1.
30+
This interval is also used in --scan and --stat per cycle.
31+
and in --bigkeys, --memkeys, and --hotkeys per 100 cycles.
32+
-n <db> Database number.
33+
-2 Start session in RESP2 protocol mode.
34+
-3 Start session in RESP3 protocol mode.
35+
-x Read last argument from STDIN (see example below).
36+
-X Read <tag> argument from STDIN (see example below).
37+
-d <delimiter> Delimiter between response bulks for raw formatting (default: \\n).
38+
-D <delimiter> Delimiter between responses for raw formatting (default: \\n).
39+
-c Enable cluster mode (follow -ASK and -MOVED redirections).
40+
-e Return exit error code when command execution fails.");
41+
if USE_OPENSSL {
42+
usage += "
43+
--tls Establish a secure TLS connection.
44+
--sni <host> Server name indication for TLS.
45+
--cacert <file> CA Certificate file to verify with.
46+
--cacertdir <dir> Directory where trusted CA certificates are stored.
47+
If neither cacert nor cacertdir are specified, the default
48+
system-wide trusted root certs configuration will apply.
49+
--insecure Allow insecure TLS connection by skipping cert validation.
50+
--cert <file> Client certificate to authenticate with.
51+
--key <file> Private key file to authenticate with.
52+
--tls-ciphers <list> Sets the list of preferred ciphers (TLSv1.2 and below)
53+
in order of preference from highest to lowest separated by colon (\":\").
54+
See the ciphers(1ssl) manpage for more information about the syntax of this string.";
55+
56+
if TLS1_3_VERSION {
57+
usage += "
58+
--tls-ciphersuites <list> Sets the list of preferred ciphersuites (TLSv1.3)
59+
in order of preference from highest to lowest separated by colon (\":\").
60+
See the ciphers(1ssl) manpage for more information about the syntax of this string,
61+
and specifically for TLSv1.3 ciphersuites."
62+
}
63+
}
64+
65+
usage.push_str(format!("
66+
--raw Use raw formatting for replies (default when STDOUT is
67+
not a tty).
68+
--no-raw Force formatted output even when STDOUT is not a tty.
69+
--quoted-input Force input to be handled as quoted strings.
70+
--csv Output in CSV format.
71+
--json Output in JSON format (default RESP3, use -2 if you want to use with RESP2).
72+
--quoted-json Same as --json, but produce ASCII-safe quoted strings, not Unicode.
73+
--show-pushes <yn> Whether to print RESP3 PUSH messages. Enabled by default when
74+
STDOUT is a tty but can be overridden with --show-pushes no.
75+
--stat Print rolling stats about server: mem, clients, ...
76+
--latency Enter a special mode continuously sampling latency.
77+
If you use this mode in an interactive session it runs
78+
forever displaying real-time stats. Otherwise if --raw or
79+
--csv is specified, or if you redirect the output to a non
80+
TTY, it samples the latency for 1 second (you can use
81+
-i to change the interval), then produces a single output
82+
and exits.
83+
--latency-history Like --latency but tracking latency changes over time.
84+
Default time interval is 15 sec. Change it using -i.
85+
--latency-dist Shows latency as a spectrum, requires xterm 256 colors.
86+
Default time interval is 1 sec. Change it using -i.
87+
--lru-test <keys> Simulate a cache workload with an 80-20 distribution.
88+
--replica Simulate a replica showing commands received from the master.
89+
--rdb <filename> Transfer an RDB dump from remote server to local file.
90+
Use filename of \"-\" to write to stdout.
91+
--functions-rdb <filename> Like --rdb but only get the functions (not the keys)
92+
when getting the RDB dump file.
93+
--pipe Transfer raw Redis protocol from stdin to server.
94+
--pipe-timeout <n> In --pipe mode, abort with error if after sending all data.
95+
no reply is received within <n> seconds.
96+
Default timeout: {REDIS_CLI_DEFAULT_PIPE_TIMEOUT}. Use 0 to wait forever.
97+
--bigkeys Sample Redis keys looking for keys with many elements (complexity).
98+
--memkeys Sample Redis keys looking for keys consuming a lot of memory.
99+
--memkeys-samples <n> Sample Redis keys looking for keys consuming a lot of memory.
100+
And define number of key elements to sample
101+
--hotkeys Sample Redis keys looking for hot keys.
102+
only works when maxmemory-policy is *lfu.
103+
--scan List all keys using the SCAN command.
104+
--pattern <pat> Keys pattern when using the --scan, --bigkeys or --hotkeys
105+
options (default: *).
106+
--quoted-pattern <pat> Same as --pattern, but the specified string can be
107+
quoted, in order to pass an otherwise non binary-safe string.
108+
--intrinsic-latency <sec> Run a test to measure intrinsic system latency.
109+
The test will run for the specified amount of seconds.
110+
--eval <file> Send an EVAL command using the Lua script at <file>.
111+
--ldb Used with --eval enable the Redis Lua debugger.
112+
--ldb-sync-mode Like --ldb but uses the synchronous Lua debugger, in
113+
this mode the server is blocked and script changes are
114+
not rolled back from the server memory.
115+
--cluster <command> [args...] [opts...]
116+
Cluster Manager command and arguments (see below).
117+
--verbose Verbose mode.
118+
--no-auth-warning Don't show warning message when using password on command
119+
line interface.
120+
--help Output this help and exit.
121+
--version Output version and exit.
122+
").as_str());
123+
124+
/* Using another fprintf call to avoid -Woverlength-strings compile warning */
125+
usage += "
126+
Cluster Manager Commands:
127+
Use --cluster help to list all available cluster manager commands.
128+
129+
Examples:
130+
cat /etc/passwd | redis-cli -x set mypasswd
131+
redis-cli -D \"\" --raw dump key > key.dump && redis-cli -X dump_tag restore key2 0 dump_tag replace < key.dump
132+
redis-cli -r 100 lpush mylist x
133+
redis-cli -r 100 -i 1 info | grep used_memory_human:
134+
redis-cli --quoted-input set '\"null-\\x00-separated\"' value
135+
redis-cli --eval myscript.lua key1 key2 , arg1 arg2 arg3
136+
redis-cli --scan --pattern '*:12345*'
137+
138+
(Note: when using --eval the comma separates KEYS[] from ARGV[] items)
139+
140+
When no command is given, redis-cli starts in interactive mode.
141+
Type \"help\" in interactive mode for information on available commands
142+
and settings.
143+
";
144+
145+
if code == 0 {
146+
println!("{}", usage);
147+
} else {
148+
eprint!("{}", usage);
149+
}
150+
exit(code);
151+
}

redis-cli/src/main.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
mod help;
2+
3+
use std::env;
4+
use std::process;
5+
use redis_core::version;
6+
17
fn main() {
2-
println!("Hello, redis-cli!");
3-
}
8+
let args: Vec<String> = env::args().collect();
9+
if args.len() == 1 {
10+
help::print_usage(0);
11+
}
12+
let arg1 = &args[1];
13+
if arg1 == "-v" || arg1 == "--version" {
14+
println!("{}", version::redis_cli_version());
15+
process::exit(0);
16+
}
17+
}

redis-core/src/version.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
const REDIS_VERSION: &str = "7.0.5";
2-
const REDIS_GIT_SHA1: &str = "";
3-
const REDIS_GIT_DIRTY: u8 = 0;
1+
const REDIS_VERSION: &str = "7.0.11";
2+
const REDIS_GIT_SHA1: &str = "bdb1e6cc";
3+
const REDIS_GIT_DIRTY: u8 = 1;
44

55
pub fn check_rdb_version() -> String {
6-
let mut version = format!("redis-check-rdb {REDIS_VERSION}");
6+
version("redis-check-rdb")
7+
}
8+
9+
pub fn redis_cli_version() -> String {
10+
version("redis-cli")
11+
}
12+
13+
fn version(name: &str) -> String {
14+
let mut version = format!("{name} {REDIS_VERSION}");
715
if !REDIS_GIT_SHA1.trim().is_empty() {
816
version = format!("{version} (git:{REDIS_GIT_SHA1}");
917
if REDIS_GIT_DIRTY > 0 {

0 commit comments

Comments
 (0)