Bladeren bron

Merge pull request #5069 from heyitsanthony/fix-snapshot-status-json

etcdctl: respect -write-out=json for snapshot status
Anthony Romano 9 jaren geleden
bovenliggende
commit
22812badc2
3 gewijzigde bestanden met toevoegingen van 27 en 22 verwijderingen
  1. 14 10
      etcdctl/ctlv3/command/global.go
  2. 4 4
      etcdctl/ctlv3/command/printer.go
  3. 9 8
      etcdctl/ctlv3/command/snapshot_command.go

+ 14 - 10
etcdctl/ctlv3/command/global.go

@@ -53,16 +53,7 @@ type secureCfg struct {
 
 var display printer = &simplePrinter{}
 
-func mustClientFromCmd(cmd *cobra.Command) *clientv3.Client {
-	flags.SetPflagsFromEnv("ETCDCTL", cmd.InheritedFlags())
-
-	endpoints, err := cmd.Flags().GetStringSlice("endpoints")
-	if err != nil {
-		ExitWithError(ExitError, err)
-	}
-	dialTimeout := dialTimeoutFromCmd(cmd)
-	sec := secureCfgFromCmd(cmd)
-
+func initDisplayFromCmd(cmd *cobra.Command) {
 	isHex, err := cmd.Flags().GetBool("hex")
 	if err != nil {
 		ExitWithError(ExitError, err)
@@ -74,6 +65,19 @@ func mustClientFromCmd(cmd *cobra.Command) *clientv3.Client {
 	if display = NewPrinter(outputType, isHex); display == nil {
 		ExitWithError(ExitBadFeature, errors.New("unsupported output format"))
 	}
+}
+
+func mustClientFromCmd(cmd *cobra.Command) *clientv3.Client {
+	flags.SetPflagsFromEnv("ETCDCTL", cmd.InheritedFlags())
+
+	endpoints, err := cmd.Flags().GetStringSlice("endpoints")
+	if err != nil {
+		ExitWithError(ExitError, err)
+	}
+	dialTimeout := dialTimeoutFromCmd(cmd)
+	sec := secureCfgFromCmd(cmd)
+
+	initDisplayFromCmd(cmd)
 
 	return mustClient(endpoints, dialTimeout, sec)
 }

+ 4 - 4
etcdctl/ctlv3/command/printer.go

@@ -153,10 +153,10 @@ func (s *simplePrinter) DBStatus(ds dbstatus) {
 	table.SetHeader([]string{"hash", "revision", "total keys", "total size"})
 
 	table.Append([]string{
-		fmt.Sprintf("%x", ds.hash),
-		fmt.Sprint(ds.revision),
-		fmt.Sprint(ds.totalKey),
-		humanize.Bytes(uint64(ds.totalSize)),
+		fmt.Sprintf("%x", ds.Hash),
+		fmt.Sprint(ds.Revision),
+		fmt.Sprint(ds.TotalKey),
+		humanize.Bytes(uint64(ds.TotalSize)),
 	})
 
 	table.Render()

+ 9 - 8
etcdctl/ctlv3/command/snapshot_command.go

@@ -134,6 +134,7 @@ func snapshotStatusCommandFunc(cmd *cobra.Command, args []string) {
 		err := fmt.Errorf("snapshot status requires exactly one argument")
 		ExitWithError(ExitBadArgs, err)
 	}
+	initDisplayFromCmd(cmd)
 	ds := dbStatus(args[0])
 	display.DBStatus(ds)
 }
@@ -289,10 +290,10 @@ func makeDB(snapdir, dbfile string) {
 }
 
 type dbstatus struct {
-	hash      uint32
-	revision  int64
-	totalKey  int
-	totalSize int64
+	Hash      uint32 `json:"hash"`
+	Revision  int64  `json:"revision"`
+	TotalKey  int    `json:"totalKey"`
+	TotalSize int64  `json:"totalSize"`
 }
 
 func dbStatus(p string) dbstatus {
@@ -306,7 +307,7 @@ func dbStatus(p string) dbstatus {
 	h := crc32.New(crc32.MakeTable(crc32.Castagnoli))
 
 	err = db.View(func(tx *bolt.Tx) error {
-		ds.totalSize = tx.Size()
+		ds.TotalSize = tx.Size()
 		c := tx.Cursor()
 		for next, _ := c.First(); next != nil; next, _ = c.Next() {
 			b := tx.Bucket(next)
@@ -320,9 +321,9 @@ func dbStatus(p string) dbstatus {
 				h.Write(v)
 				if iskeyb {
 					rev := bytesToRev(k)
-					ds.revision = rev.main
+					ds.Revision = rev.main
 				}
-				ds.totalKey++
+				ds.TotalKey++
 				return nil
 			})
 		}
@@ -333,7 +334,7 @@ func dbStatus(p string) dbstatus {
 		ExitWithError(ExitError, err)
 	}
 
-	ds.hash = h.Sum32()
+	ds.Hash = h.Sum32()
 	return ds
 }