From 3a4931b0e467e3b64a2637b96d7a3cab9add1c80 Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Mon, 17 Jul 2017 09:38:52 -0400 Subject: [PATCH] ceph: allow '-' with -i and -o for stdin/stdout Signed-off-by: Sage Weil --- qa/workunits/cephtool/test.sh | 7 +++++++ src/ceph.in | 18 ++++++++++++------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/qa/workunits/cephtool/test.sh b/qa/workunits/cephtool/test.sh index 2395f1baef7..9586d81c0df 100755 --- a/qa/workunits/cephtool/test.sh +++ b/qa/workunits/cephtool/test.sh @@ -2326,6 +2326,12 @@ function test_mon_tell_help_command() expect_false ceph tell mon.zzz help } +function test_mon_stdin_stdout() +{ + echo foo | ceph config-key put test_key -i - + ceph config-key get test_key -o - | grep -c foo | grep -q 1 +} + function test_osd_tell_help_command() { ceph tell osd.1 help @@ -2411,6 +2417,7 @@ MON_TESTS+=" mon_deprecated_commands" MON_TESTS+=" mon_caps" MON_TESTS+=" mon_cephdf_commands" MON_TESTS+=" mon_tell_help_command" +MON_TESTS+=" mon_stdin_stdout" OSD_TESTS+=" osd_bench" OSD_TESTS+=" osd_negative_filestore_merge_threshold" diff --git a/src/ceph.in b/src/ceph.in index 6a6672a8592..311ec64e65c 100755 --- a/src/ceph.in +++ b/src/ceph.in @@ -264,9 +264,9 @@ def parse_cmdargs(args=None, target=''): parser.add_argument('-c', '--conf', dest='cephconf', help='ceph configuration file') parser.add_argument('-i', '--in-file', dest='input_file', - help='input file') + help='input file, or "-" for stdin') parser.add_argument('-o', '--out-file', dest='output_file', - help='output file') + help='output file, or "-" for stdout') parser.add_argument('--id', '--user', dest='client_id', help='client id for authentication') @@ -964,8 +964,11 @@ def main(): inbuf = b'' if parsed_args.input_file: try: - with open(parsed_args.input_file, 'rb') as f: - inbuf = f.read() + if parsed_args.input_file == '-': + inbuf = sys.stdin.read() + else: + with open(parsed_args.input_file, 'rb') as f: + inbuf = f.read() except Exception as e: print('Can\'t open input file {0}: {1}'.format(parsed_args.input_file, e), file=sys.stderr) return 1 @@ -973,7 +976,10 @@ def main(): # prepare output file, if any if parsed_args.output_file: try: - outf = open(parsed_args.output_file, 'wb') + if parsed_args.output_file == '-': + outf = sys.stdout + else: + outf = open(parsed_args.output_file, 'wb') except Exception as e: print('Can\'t open output file {0}: {1}'.format(parsed_args.output_file, e), file=sys.stderr) return 1 @@ -1099,7 +1105,7 @@ def main(): sys.stdout.flush() - if parsed_args.output_file: + if parsed_args.output_file and parsed_args.output_file != '-': outf.close() if final_ret: -- 2.47.3