-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.go
141 lines (118 loc) · 2.67 KB
/
main.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"context"
"flag"
"os"
"sort"
"strings"
"github.com/genuinetools/pkg/cli"
"github.com/jessfraz/dockfmt/version"
"github.com/moby/buildkit/frontend/dockerfile/parser"
"github.com/sirupsen/logrus"
)
var (
debug bool
)
func main() {
// Create a new cli program.
p := cli.NewProgram()
p.Name = "dockfmt"
p.Description = "Dockerfile format."
// Set the GitCommit and Version.
p.GitCommit = version.GITCOMMIT
p.Version = version.VERSION
// Setup the global flags.
p.FlagSet = flag.NewFlagSet("global", flag.ExitOnError)
p.FlagSet.BoolVar(&debug, "debug", false, "enable debug logging")
p.FlagSet.BoolVar(&debug, "d", false, "enable debug logging")
p.Commands = []cli.Command{
&baseCommand{},
&dumpCommand{},
&formatCommand{},
&maintainerCommand{},
&stagesCommand{},
&workdirCommand{},
}
// Set the before function.
p.Before = func(ctx context.Context) error {
// Set the log level.
if debug {
logrus.SetLevel(logrus.DebugLevel)
}
return nil
}
// Run our program.
p.Run()
}
type pair struct {
Key string
Value int
}
type pairList []pair
func (p pairList) Len() int { return len(p) }
func (p pairList) Less(i, j int) bool { return p[i].Value < p[j].Value }
func (p pairList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func rank(images map[string]int) pairList {
pl := make(pairList, len(images))
i := 0
for k, v := range images {
pl[i] = pair{k, v}
i++
}
sort.Sort(sort.Reverse(pl))
return pl
}
func labelSearch(search string, n *parser.Node, a map[string]int) map[string]int {
if n.Value == "label" {
if n.Next != nil && strings.EqualFold(strings.ToLower(n.Next.Value), strings.ToLower(search)) {
i := strings.Trim(n.Next.Next.Value, "\"")
if v, ok := a[i]; ok {
a[i] = v + 1
} else {
a[i] = 1
}
}
}
return a
}
func nodeSearch(search string, n *parser.Node, a map[string]int) map[string]int {
if n.Value == search {
i := strings.Trim(n.Next.Value, "\"")
if v, ok := a[i]; ok {
a[i] = v + 1
} else {
a[i] = 1
}
}
return a
}
type forFileFunc func(*os.File, []*parser.Node) error
func forFile1(f *os.File, fnc forFileFunc) error {
result, err := parser.Parse(f)
if err != nil {
return err
}
ast := result.AST
nodes := []*parser.Node{ast}
if ast.Children != nil {
nodes = append(nodes, ast.Children...)
}
return fnc(f, nodes)
}
func forFile(args []string, fnc forFileFunc) error {
if len(args) == 0 {
return forFile1(os.Stdin, fnc)
}
for _, fn := range args {
logrus.Debugf("parsing file: %s", fn)
f, err := os.Open(fn)
if err != nil {
return err
}
defer f.Close()
if err = forFile1(f, fnc); err != nil {
return err
}
}
return nil
}