From fef7f3addc593d1315e1067c1932909cf2e7a5b6 Mon Sep 17 00:00:00 2001 From: Akihiro Suda Date: Tue, 15 Aug 2017 06:24:43 +0000 Subject: [PATCH 1/2] ctr: add `ctr snapshot info ` Signed-off-by: Akihiro Suda --- cmd/ctr/info.go | 12 +++--------- cmd/ctr/shim.go | 13 +------------ cmd/ctr/snapshot.go | 31 +++++++++++++++++++++++++++++++ cmd/ctr/utils.go | 8 ++++++++ 4 files changed, 43 insertions(+), 21 deletions(-) diff --git a/cmd/ctr/info.go b/cmd/ctr/info.go index fcbc28e3a..c2a9f9395 100644 --- a/cmd/ctr/info.go +++ b/cmd/ctr/info.go @@ -1,10 +1,6 @@ package main import ( - "encoding/json" - - "fmt" - "github.com/pkg/errors" "github.com/urfave/cli" ) @@ -31,11 +27,9 @@ var containerInfoCommand = cli.Command{ if err != nil { return err } - cjson, err := json.MarshalIndent(container.Info(), "", " ") - if err != nil { - return err - } - fmt.Println(string(cjson)) + + printAsJSON(container.Info()) + return nil }, } diff --git a/cmd/ctr/shim.go b/cmd/ctr/shim.go index 3fbbed179..c5625f35a 100644 --- a/cmd/ctr/shim.go +++ b/cmd/ctr/shim.go @@ -3,12 +3,9 @@ package main import ( - "bytes" - "encoding/json" "fmt" "io/ioutil" "net" - "os" "time" gocontext "context" @@ -185,15 +182,7 @@ var shimStateCommand = cli.Command{ if err != nil { return err } - data, err := json.Marshal(r) - if err != nil { - return err - } - buf := bytes.NewBuffer(nil) - if err := json.Indent(buf, data, " ", " "); err != nil { - return err - } - buf.WriteTo(os.Stdout) + printAsJSON(r) return nil }, } diff --git a/cmd/ctr/snapshot.go b/cmd/ctr/snapshot.go index ad07c4c1e..c48a35a94 100644 --- a/cmd/ctr/snapshot.go +++ b/cmd/ctr/snapshot.go @@ -27,6 +27,7 @@ var snapshotCommand = cli.Command{ treeSnapshotCommand, mountSnapshotCommand, commitSnapshotCommand, + infoSnapshotCommand, }, } @@ -312,6 +313,36 @@ var treeSnapshotCommand = cli.Command{ }, } +var infoSnapshotCommand = cli.Command{ + Name: "info", + Usage: "get info about a snapshot", + ArgsUsage: "", + Action: func(clicontext *cli.Context) error { + ctx, cancel := appContext(clicontext) + defer cancel() + + if clicontext.NArg() != 1 { + return cli.ShowSubcommandHelp(clicontext) + } + + key := clicontext.Args().Get(0) + + snapshotter, err := getSnapshotter(clicontext) + if err != nil { + return err + } + + info, err := snapshotter.Stat(ctx, key) + if err != nil { + return err + } + + printAsJSON(info) + + return nil + }, +} + type snapshotTreeNode struct { Name string Parent string diff --git a/cmd/ctr/utils.go b/cmd/ctr/utils.go index 2f9d99fe9..32e8f3327 100644 --- a/cmd/ctr/utils.go +++ b/cmd/ctr/utils.go @@ -417,3 +417,11 @@ func labelArgs(labelStrings []string) map[string]string { return labels } + +func printAsJSON(x interface{}) { + b, err := json.MarshalIndent(x, "", " ") + if err != nil { + fmt.Fprintf(os.Stderr, "can't marshal %+v as a JSON string: %v\n", x, err) + } + fmt.Println(string(b)) +} From 525bffd194fe8ec3595811dcde9b69c9f8e01782 Mon Sep 17 00:00:00 2001 From: Akihiro Suda Date: Wed, 16 Aug 2017 03:10:24 +0000 Subject: [PATCH 2/2] snapshot: support JSON marshalling for Info Signed-off-by: Akihiro Suda --- snapshot/snapshotter.go | 51 ++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/snapshot/snapshotter.go b/snapshot/snapshotter.go index 35d645000..a75a70c51 100644 --- a/snapshot/snapshotter.go +++ b/snapshot/snapshotter.go @@ -2,6 +2,8 @@ package snapshot import ( "context" + "encoding/json" + "strings" "time" "github.com/containerd/containerd/mount" @@ -17,27 +19,48 @@ const ( KindCommitted ) -func (k Kind) String() string { - switch k { - case KindView: - return "View" - case KindActive: - return "Active" - case KindCommitted: - return "Committed" - default: - return "Unknown" +var ( + kindStrings = map[Kind]string{ + KindView: "view", + KindActive: "active", + KindCommitted: "committed", } +) + +func (k Kind) String() string { + if s, ok := kindStrings[k]; ok { + return s + } + return "unknown" +} + +func (k Kind) MarshalJSON() ([]byte, error) { + return json.Marshal(k.String()) +} + +func (k *Kind) UnmarshalJSON(b []byte) error { + var s string + if err := json.Unmarshal(b, &s); err != nil { + return err + } + for kk, ks := range kindStrings { + if strings.EqualFold(s, ks) { + *k = kk + return nil + } + } + return nil } // Info provides information about a particular snapshot. +// JSON marshallability is supported for interactive with tools like ctr, type Info struct { Kind Kind // active or committed snapshot Name string // name or key of snapshot - Parent string // name of parent snapshot - Labels map[string]string // Labels for snapshot - Created time.Time // Created time - Updated time.Time // Last update time + Parent string `json:",omitempty"` // name of parent snapshot + Labels map[string]string `json:",omitempty"` // Labels for snapshot + Created time.Time `json:",omitempty"` // Created time + Updated time.Time `json:",omitempty"` // Last update time } // Usage defines statistics for disk resources consumed by the snapshot.