forked from genuinetools/img
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
72 lines (57 loc) · 1.74 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"context"
"flag"
"fmt"
"os"
"text/tabwriter"
"time"
"github.com/containerd/containerd/namespaces"
units "github.com/docker/go-units"
"github.com/genuinetools/img/client"
"github.com/moby/buildkit/identity"
"github.com/moby/buildkit/session"
)
const listHelp = `List images and digests.`
func (cmd *listCommand) Name() string { return "ls" }
func (cmd *listCommand) Args() string { return "[OPTIONS]" }
func (cmd *listCommand) ShortHelp() string { return listHelp }
func (cmd *listCommand) LongHelp() string { return listHelp }
func (cmd *listCommand) Hidden() bool { return false }
func (cmd *listCommand) Register(fs *flag.FlagSet) {
fs.Var(&cmd.filters, "f", "Filter output based on conditions provided")
fs.Var(&cmd.filters, "filter", "Filter output based on conditions provided")
}
type listCommand struct {
filters stringSlice
}
func (cmd *listCommand) Run(ctx context.Context, args []string) (err error) {
reexec()
// Create the context.
id := identity.NewID()
ctx = session.NewContext(ctx, id)
ctx = namespaces.WithNamespace(ctx, "buildkit")
// Create the client.
c, err := client.New(stateDir, backend, nil)
if err != nil {
return err
}
defer c.Close()
images, err := c.ListImages(ctx, cmd.filters...)
if err != nil {
return err
}
tw := tabwriter.NewWriter(os.Stdout, 1, 8, 1, '\t', 0)
fmt.Fprintln(tw, "NAME\tSIZE\tCREATED AT\tUPDATED AT\tDIGEST")
for _, image := range images {
fmt.Fprintf(tw, "%s\t%s\t%s\t%s\t%s\n",
image.Name,
units.BytesSize(float64(image.ContentSize)),
units.HumanDuration(time.Now().UTC().Sub(image.CreatedAt))+" ago",
units.HumanDuration(time.Now().UTC().Sub(image.UpdatedAt))+" ago",
image.Target.Digest,
)
}
tw.Flush()
return nil
}