forked from jessfraz/dockfmt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.go
52 lines (39 loc) · 1.14 KB
/
base.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
package main
import (
"context"
"flag"
"fmt"
"os"
"text/tabwriter"
"github.com/moby/buildkit/frontend/dockerfile/parser"
)
const baseHelp = `List the base image used in the Dockerfile(s).`
func (cmd *baseCommand) Name() string { return "base" }
func (cmd *baseCommand) Args() string { return "[OPTIONS] DOCKERFILE [DOCKERFILE...]" }
func (cmd *baseCommand) ShortHelp() string { return baseHelp }
func (cmd *baseCommand) LongHelp() string { return baseHelp }
func (cmd *baseCommand) Hidden() bool { return false }
func (cmd *baseCommand) Register(fs *flag.FlagSet) {}
type baseCommand struct{}
func (cmd *baseCommand) Run(ctx context.Context, args []string) error {
images := map[string]int{}
err := forFile(args, func(f string, nodes []*parser.Node) error {
for _, n := range nodes {
images = nodeSearch("from", n, images)
}
return nil
})
if err != nil {
return err
}
// setup the tab writer
w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
// print header
fmt.Fprintln(w, "BASE\tCOUNT")
pl := rank(images)
for _, p := range pl {
fmt.Fprintf(w, "%s\t%d\n", p.Key, p.Value)
}
w.Flush()
return nil
}