Merge pull request #1362 from AkihiroSuda/ctr-snapshot-info

ctr: add `ctr snapshot info <key>`
This commit is contained in:
Stephen Day 2017-09-01 14:25:25 -07:00 committed by GitHub
commit 378e3343fe
5 changed files with 80 additions and 35 deletions

View File

@ -1,10 +1,6 @@
package main package main
import ( import (
"encoding/json"
"fmt"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -31,11 +27,9 @@ var containerInfoCommand = cli.Command{
if err != nil { if err != nil {
return err return err
} }
cjson, err := json.MarshalIndent(container.Info(), "", " ")
if err != nil { printAsJSON(container.Info())
return err
}
fmt.Println(string(cjson))
return nil return nil
}, },
} }

View File

@ -3,12 +3,9 @@
package main package main
import ( import (
"bytes"
"encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net" "net"
"os"
"time" "time"
gocontext "context" gocontext "context"
@ -185,15 +182,7 @@ var shimStateCommand = cli.Command{
if err != nil { if err != nil {
return err return err
} }
data, err := json.Marshal(r) printAsJSON(r)
if err != nil {
return err
}
buf := bytes.NewBuffer(nil)
if err := json.Indent(buf, data, " ", " "); err != nil {
return err
}
buf.WriteTo(os.Stdout)
return nil return nil
}, },
} }

View File

@ -27,6 +27,7 @@ var snapshotCommand = cli.Command{
treeSnapshotCommand, treeSnapshotCommand,
mountSnapshotCommand, mountSnapshotCommand,
commitSnapshotCommand, commitSnapshotCommand,
infoSnapshotCommand,
}, },
} }
@ -312,6 +313,36 @@ var treeSnapshotCommand = cli.Command{
}, },
} }
var infoSnapshotCommand = cli.Command{
Name: "info",
Usage: "get info about a snapshot",
ArgsUsage: "<key>",
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 { type snapshotTreeNode struct {
Name string Name string
Parent string Parent string

View File

@ -417,3 +417,11 @@ func labelArgs(labelStrings []string) map[string]string {
return labels 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))
}

View File

@ -2,6 +2,8 @@ package snapshot
import ( import (
"context" "context"
"encoding/json"
"strings"
"time" "time"
"github.com/containerd/containerd/mount" "github.com/containerd/containerd/mount"
@ -17,27 +19,48 @@ const (
KindCommitted KindCommitted
) )
func (k Kind) String() string { var (
switch k { kindStrings = map[Kind]string{
case KindView: KindView: "view",
return "View" KindActive: "active",
case KindActive: KindCommitted: "committed",
return "Active"
case KindCommitted:
return "Committed"
default:
return "Unknown"
} }
)
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. // Info provides information about a particular snapshot.
// JSON marshallability is supported for interactive with tools like ctr,
type Info struct { type Info struct {
Kind Kind // active or committed snapshot Kind Kind // active or committed snapshot
Name string // name or key of snapshot Name string // name or key of snapshot
Parent string // name of parent snapshot Parent string `json:",omitempty"` // name of parent snapshot
Labels map[string]string // Labels for snapshot Labels map[string]string `json:",omitempty"` // Labels for snapshot
Created time.Time // Created time Created time.Time `json:",omitempty"` // Created time
Updated time.Time // Last update time Updated time.Time `json:",omitempty"` // Last update time
} }
// Usage defines statistics for disk resources consumed by the snapshot. // Usage defines statistics for disk resources consumed by the snapshot.