Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
123a10f7
Commit
123a10f7
authored
Aug 04, 2010
by
Pieter Noordhuis
Browse files
Let the output mode depend on having a tty or not
parent
07242c0c
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/redis-cli.c
View file @
123a10f7
...
@@ -60,6 +60,7 @@ static struct config {
...
@@ -60,6 +60,7 @@ static struct config {
int
monitor_mode
;
int
monitor_mode
;
int
pubsub_mode
;
int
pubsub_mode
;
int
raw_output
;
/* output mode per command */
int
raw_output
;
/* output mode per command */
int
tty
;
/* flag for default output format */
char
*
auth
;
char
*
auth
;
char
*
historyfile
;
char
*
historyfile
;
}
config
;
}
config
;
...
@@ -152,7 +153,7 @@ static int cliReadBulkReply(int fd) {
...
@@ -152,7 +153,7 @@ static int cliReadBulkReply(int fd) {
reply
=
zmalloc
(
bulklen
);
reply
=
zmalloc
(
bulklen
);
anetRead
(
fd
,
reply
,
bulklen
);
anetRead
(
fd
,
reply
,
bulklen
);
anetRead
(
fd
,
crlf
,
2
);
anetRead
(
fd
,
crlf
,
2
);
if
(
config
.
raw_output
)
{
if
(
config
.
raw_output
||
!
config
.
tty
)
{
if
(
bulklen
&&
fwrite
(
reply
,
bulklen
,
1
,
stdout
)
==
0
)
{
if
(
bulklen
&&
fwrite
(
reply
,
bulklen
,
1
,
stdout
)
==
0
)
{
zfree
(
reply
);
zfree
(
reply
);
return
1
;
return
1
;
...
@@ -491,6 +492,7 @@ int main(int argc, char **argv) {
...
@@ -491,6 +492,7 @@ int main(int argc, char **argv) {
config
.
raw_output
=
0
;
config
.
raw_output
=
0
;
config
.
auth
=
NULL
;
config
.
auth
=
NULL
;
config
.
historyfile
=
NULL
;
config
.
historyfile
=
NULL
;
config
.
tty
=
1
;
if
(
getenv
(
"HOME"
)
!=
NULL
)
{
if
(
getenv
(
"HOME"
)
!=
NULL
)
{
config
.
historyfile
=
malloc
(
256
);
config
.
historyfile
=
malloc
(
256
);
...
@@ -515,6 +517,7 @@ int main(int argc, char **argv) {
...
@@ -515,6 +517,7 @@ int main(int argc, char **argv) {
repl
();
repl
();
}
}
config
.
tty
=
isatty
(
stdout
)
||
(
getenv
(
"FAKETTY"
)
!=
NULL
);
argvcopy
=
convertToSds
(
argc
+
1
,
argv
);
argvcopy
=
convertToSds
(
argc
+
1
,
argv
);
if
(
config
.
argn_from_stdin
)
{
if
(
config
.
argn_from_stdin
)
{
sds
lastarg
=
readArgFromStdin
();
sds
lastarg
=
readArgFromStdin
();
...
...
tests/integration/redis-cli.tcl
View file @
123a10f7
...
@@ -41,7 +41,7 @@ start_server {tags {"cli"}} {
...
@@ -41,7 +41,7 @@ start_server {tags {"cli"}} {
close_cli $fd
close_cli $fd
}
}
proc run_cli
{
args
}
{
proc run_
nontty_
cli
{
args
}
{
set fd
[
open
[
format
"|src/redis-cli -p %d -n 9
$args
"
[
srv port
]]
"r"
]
set fd
[
open
[
format
"|src/redis-cli -p %d -n 9
$args
"
[
srv port
]]
"r"
]
fconfigure $fd -buffering none
fconfigure $fd -buffering none
fconfigure $fd -translation binary
fconfigure $fd -translation binary
...
@@ -50,8 +50,19 @@ start_server {tags {"cli"}} {
...
@@ -50,8 +50,19 @@ start_server {tags {"cli"}} {
set _ $resp
set _ $resp
}
}
proc test_noninteractive_cli
{
name code
}
{
proc test_nontty_cli
{
name code
}
{
test
"Non-interactive CLI:
$name
"
$code
test
"Non-interactive non-TTY CLI:
$name
"
$code
}
proc run_tty_cli
{
args
}
{
set ::env
(
FAKETTY
)
1
set resp
[
run_nontty_cli
{*}
$args
]
unset ::env
(
FAKETTY
)
set _ $resp
}
proc test_tty_cli
{
name code
}
{
test
"Non-interactive TTY CLI:
$name
"
$code
}
}
test_interactive_cli
"INFO response should be printed raw"
{
test_interactive_cli
"INFO response should be printed raw"
{
...
@@ -99,25 +110,25 @@ start_server {tags {"cli"}} {
...
@@ -99,25 +110,25 @@ start_server {tags {"cli"}} {
assert_equal
"bar"
[
r get key
]
assert_equal
"bar"
[
r get key
]
}
}
test_
noninteractive
_cli
"Status reply"
{
test_
tty
_cli
"Status reply"
{
assert_equal
"OK
\n
"
[
run_cli set key bar
]
assert_equal
"OK
\n
"
[
run_
tty_
cli set key bar
]
assert_equal
"bar"
[
r get key
]
assert_equal
"bar"
[
r get key
]
}
}
test_
noninteractive
_cli
"Integer reply"
{
test_
tty
_cli
"Integer reply"
{
r del counter
r del counter
assert_equal
"(integer) 1
\n
"
[
run_cli incr counter
]
assert_equal
"(integer) 1
\n
"
[
run_
tty_
cli incr counter
]
}
}
test_
noninteractive
_cli
"Bulk reply"
{
test_
tty
_cli
"Bulk reply"
{
r set key
"tab
\t
newline
\n
"
r set key
"tab
\t
newline
\n
"
assert_equal
"
\"
tab
\\
tnewline
\\
n
\"\n
"
[
run_cli get key
]
assert_equal
"
\"
tab
\\
tnewline
\\
n
\"\n
"
[
run_
tty_
cli get key
]
}
}
test_
noninteractive
_cli
"Multi-bulk reply"
{
test_
tty
_cli
"Multi-bulk reply"
{
r del list
r del list
r rpush list foo
r rpush list foo
r rpush list bar
r rpush list bar
assert_equal
"1.
\"
foo
\"\n
2.
\"
bar
\"\n
"
[
run_cli lrange list 0 -1
]
assert_equal
"1.
\"
foo
\"\n
2.
\"
bar
\"\n
"
[
run_
tty_
cli lrange list 0 -1
]
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment