Sfoglia il codice sorgente

Merge pull request #5039 from heyitsanthony/fix-write-out

etcdctl: respect --write-out
Anthony Romano 9 anni fa
parent
commit
195100a769
2 ha cambiato i file con 40 aggiunte e 0 eliminazioni
  1. 28 0
      e2e/ctl_v3_test.go
  2. 12 0
      etcdctl/ctlv3/command/global.go

+ 28 - 0
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)
 

+ 12 - 0
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)
 }