snapshot: support JSON marshalling for Info
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
This commit is contained in:
parent
fef7f3addc
commit
525bffd194
@ -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.
|
||||||
|
Loading…
Reference in New Issue
Block a user