-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathworkdir.go
65 lines (52 loc) · 1.49 KB
/
workdir.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
package main
import (
"context"
"flag"
"fmt"
"os"
"text/tabwriter"
"github.com/moby/buildkit/frontend/dockerfile/parser"
)
const workdirHelp = `List the workdirs for the Dockerfile(s).`
func (cmd *workdirCommand) Name() string { return "workdir" }
func (cmd *workdirCommand) Args() string { return "[OPTIONS] DOCKERFILE [DOCKERFILE...]" }
func (cmd *workdirCommand) ShortHelp() string { return workdirHelp }
func (cmd *workdirCommand) LongHelp() string { return workdirHelp }
func (cmd *workdirCommand) Hidden() bool { return false }
func (cmd *workdirCommand) Register(fs *flag.FlagSet) {
fs.BoolVar(&cmd.noRank, "no-rank", false, "turn off ranking of WORKDIRs")
fs.BoolVar(&cmd.noRank, "N", false, "turn off ranking of WORKDIRs")
}
type workdirCommand struct {
noRank bool
}
func (cmd *workdirCommand) Run(ctx context.Context, args []string) error {
workdirs := map[string]int{}
err := forFile(args, func(f *os.File, nodes []*parser.Node) error {
for _, n := range nodes {
workdirs = nodeSearch("workdir", n, workdirs)
}
return nil
})
if err != nil {
return err
}
// setup the tab writer
w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
if cmd.noRank {
// print header
fmt.Fprintln(w, "WORKDIR")
for workdir := range workdirs {
fmt.Fprintf(w, "%s\n", workdir)
}
} else {
// print header
fmt.Fprintln(w, "WORKDIR\tCOUNT")
pl := rank(workdirs)
for _, p := range pl {
fmt.Fprintf(w, "%s\t%d\n", p.Key, p.Value)
}
}
w.Flush()
return nil
}