-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathstages.go
69 lines (55 loc) · 1.51 KB
/
stages.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
package main
import (
"context"
"flag"
"fmt"
"os"
"text/tabwriter"
"github.com/moby/buildkit/frontend/dockerfile/instructions"
"github.com/moby/buildkit/frontend/dockerfile/parser"
)
const stageHelp = `List the stages in the Dockerfile.`
func (cmd *stagesCommand) Name() string { return "stages" }
func (cmd *stagesCommand) Args() string { return "[OPTIONS] DOCKERFILE" }
func (cmd *stagesCommand) ShortHelp() string { return stageHelp }
func (cmd *stagesCommand) LongHelp() string { return stageHelp }
func (cmd *stagesCommand) Hidden() bool { return false }
func (cmd *stagesCommand) Register(fs *flag.FlagSet) {}
type stagesCommand struct{}
func (cmd *stagesCommand) Run(ctx context.Context, args []string) error {
images := []*parser.Node{}
err := forFile(args, func(f *os.File, nodes []*parser.Node) error {
for _, n := range nodes {
if n.Value == "from" {
images = append(images, n)
}
}
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, "STAGE\tINTERPOLATED")
for i, n := range images {
cmd, err := instructions.ParseInstruction(n)
if err != nil {
w.Flush()
return err
}
switch stage := cmd.(type) {
case *instructions.Stage:
stageName := stage.Name
interpolated := false
if stageName == "" {
stageName = fmt.Sprintf("stage-%d", i)
interpolated = true
}
fmt.Fprintf(w, "%s\t%v\n", stageName, interpolated)
}
}
w.Flush()
return nil
}