From e5a2bd58ecb6cbb9dc0edb722ab2e5dce5d4b802 Mon Sep 17 00:00:00 2001 From: Anthony Romano Date: Mon, 11 Apr 2016 15:16:11 -0700 Subject: [PATCH] etcdctl: respect --write-out Support got clobbered about a month ago. --- e2e/ctl_v3_test.go | 28 ++++++++++++++++++++++++++++ etcdctl/ctlv3/command/global.go | 12 ++++++++++++ 2 files changed, 40 insertions(+) diff --git a/e2e/ctl_v3_test.go b/e2e/ctl_v3_test.go index 2a9a60660..5ac18ae05 100644 --- a/e2e/ctl_v3_test.go +++ b/e2e/ctl_v3_test.go @@ -38,6 +38,8 @@ func TestCtlV3GetPeerTLS(t *testing.T) { testCtl(t, getTest, withCfg(configPee func TestCtlV3GetTimeout(t *testing.T) { testCtl(t, getTest, withDialTimeout(0)) } func TestCtlV3GetQuorum(t *testing.T) { testCtl(t, getTest, withQuorum()) } +func TestCtlV3GetFormat(t *testing.T) { testCtl(t, getFormatTest) } + func TestCtlV3Del(t *testing.T) { testCtl(t, delTest) } func TestCtlV3DelNoTLS(t *testing.T) { testCtl(t, delTest, withCfg(configNoTLS)) } func TestCtlV3DelClientTLS(t *testing.T) { testCtl(t, delTest, withCfg(configClientTLS)) } @@ -218,6 +220,32 @@ func getTest(cx ctlCtx) { } } +func getFormatTest(cx ctlCtx) { + defer close(cx.errc) + if err := ctlV3Put(cx, "abc", "123", ""); err != nil { + cx.t.Fatal(err) + } + + tests := []struct { + format string + + wstr string + }{ + {"simple", "abc"}, + {"json", "\"key\":\"YWJj\""}, + {"protobuf", "\x17\b\x93\xe7\xf6\x93\xd4ņ\xe14\x10\xed"}, + } + + for i, tt := range tests { + cmdArgs := append(ctlV3PrefixArgs(cx.epc, cx.dialTimeout), "get") + cmdArgs = append(cmdArgs, "--write-out="+tt.format) + cmdArgs = append(cmdArgs, "abc") + if err := spawnWithExpect(cmdArgs, tt.wstr); err != nil { + cx.t.Errorf("#%d: error (%v), wanted %v", i, err, tt.wstr) + } + } +} + func delTest(cx ctlCtx) { defer close(cx.errc) diff --git a/etcdctl/ctlv3/command/global.go b/etcdctl/ctlv3/command/global.go index 13a4531a6..bc38b92c7 100644 --- a/etcdctl/ctlv3/command/global.go +++ b/etcdctl/ctlv3/command/global.go @@ -63,6 +63,18 @@ func mustClientFromCmd(cmd *cobra.Command) *clientv3.Client { dialTimeout := dialTimeoutFromCmd(cmd) sec := secureCfgFromCmd(cmd) + isHex, err := cmd.Flags().GetBool("hex") + if err != nil { + ExitWithError(ExitError, err) + } + outputType, err := cmd.Flags().GetString("write-out") + if err != nil { + ExitWithError(ExitError, err) + } + if display = NewPrinter(outputType, isHex); display == nil { + ExitWithError(ExitBadFeature, errors.New("unsupported output format")) + } + return mustClient(endpoints, dialTimeout, sec) }