-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
fmt.go
290 lines (233 loc) · 5.89 KB
/
fmt.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package command
import (
"bytes"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclparse"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/posener/complete"
"golang.org/x/crypto/ssh/terminal"
)
const (
stdinArg = "-"
stdinPath = "<stdin>"
)
type FormatCommand struct {
Meta
diagWr hcl.DiagnosticWriter
parser *hclparse.Parser
hclDiags hcl.Diagnostics
errs *multierror.Error
list bool
check bool
checkSuccess bool
recursive bool
writeFile bool
writeStdout bool
paths []string
stdin io.Reader
}
func (*FormatCommand) Help() string {
helpText := `
Usage: nomad fmt [flags] paths ...
Formats Nomad agent configuration and job file to a canonical format.
If a path is a directory, it will recursively format all files
with .nomad and .hcl extensions in the directory.
If you provide a single dash (-) as argument, fmt will read from standard
input (STDIN) and output the processed output to standard output (STDOUT).
Format Options:
-check
Check if the files are valid HCL files. If not, exit status of the
command will be 1 and the incorrect files will not be formatted. This
flag overrides any -write flag value.
-list
List the files which contain formatting inconsistencies. Defaults
to -list=true.
-recursive
Process files in subdirectories. By default only the given (or current)
directory is processed.
-write
Overwrite the input files. Defaults to -write=true. Ignored if the input
comes from STDIN.
`
return strings.TrimSpace(helpText)
}
func (*FormatCommand) Synopsis() string {
return "Rewrites Nomad config and job files to canonical format"
}
func (*FormatCommand) AutocompleteArgs() complete.Predictor {
return complete.PredictFiles("*")
}
func (*FormatCommand) AutocompleteFlags() complete.Flags {
return complete.Flags{
"-check": complete.PredictNothing,
"-write": complete.PredictNothing,
"-list": complete.PredictNothing,
"-recursive": complete.PredictNothing,
}
}
func (f *FormatCommand) Name() string { return "fmt" }
func (f *FormatCommand) Run(args []string) int {
if f.stdin == nil {
f.stdin = os.Stdin
}
flags := f.Meta.FlagSet(f.Name(), FlagSetClient)
flags.Usage = func() { f.Ui.Output(f.Help()) }
flags.BoolVar(&f.check, "check", false, "")
flags.BoolVar(&f.writeFile, "write", true, "")
flags.BoolVar(&f.list, "list", true, "")
flags.BoolVar(&f.recursive, "recursive", false, "")
if err := flags.Parse(args); err != nil {
return 1
}
f.checkSuccess = true
f.parser = hclparse.NewParser()
color := terminal.IsTerminal(int(os.Stderr.Fd()))
w, _, err := terminal.GetSize(int(os.Stdout.Fd()))
if err != nil {
w = 80
}
f.diagWr = hcl.NewDiagnosticTextWriter(os.Stderr, f.parser.Files(), uint(w), color)
if len(flags.Args()) == 0 {
f.paths = []string{"."}
} else if flags.Args()[0] == stdinArg {
f.writeStdout = true
f.writeFile = false
f.list = false
} else {
f.paths = flags.Args()
}
if f.check {
f.writeFile = false
}
if !f.list && !f.writeFile {
f.writeStdout = true
}
f.fmt()
if f.hclDiags.HasErrors() {
f.diagWr.WriteDiagnostics(f.hclDiags)
}
if f.errs != nil {
f.Ui.Error(f.errs.Error())
f.Ui.Error(commandErrorText(f))
}
if f.hclDiags.HasErrors() || f.errs != nil {
return 1
}
if !f.checkSuccess {
return 1
}
return 0
}
func (f *FormatCommand) fmt() {
if len(f.paths) == 0 {
f.processFile(stdinPath, f.stdin)
return
}
for _, path := range f.paths {
info, err := os.Stat(path)
if err != nil {
f.appendError(fmt.Errorf("No file or directory at %s", path))
continue
}
if info.IsDir() {
f.processDir(path)
} else {
if isNomadFile(info) {
fp, err := os.Open(path)
if err != nil {
f.appendError(fmt.Errorf("Failed to open file %s: %w", path, err))
continue
}
f.processFile(path, fp)
fp.Close()
} else {
f.appendError(fmt.Errorf("Only .nomad and .hcl files can be processed using nomad fmt"))
continue
}
}
}
}
func (f *FormatCommand) processDir(path string) {
entries, err := os.ReadDir(path)
if err != nil {
f.appendError(fmt.Errorf("Failed to list directory %s", path))
return
}
for _, entry := range entries {
name := entry.Name()
subpath := filepath.Join(path, name)
if entry.IsDir() {
if f.recursive {
f.processDir(subpath)
}
continue
}
info, err := entry.Info()
if err != nil {
f.appendError(err)
continue
}
if isNomadFile(info) {
fp, err := os.Open(subpath)
if err != nil {
f.appendError(fmt.Errorf("Failed to open file %s: %w", path, err))
continue
}
f.processFile(subpath, fp)
fp.Close()
}
}
}
func (f *FormatCommand) processFile(path string, r io.Reader) {
src, err := io.ReadAll(r)
if err != nil {
f.appendError(fmt.Errorf("Failed to read file %s: %w", path, err))
return
}
f.parser.AddFile(path, &hcl.File{
Body: hcl.EmptyBody(),
Bytes: src,
})
_, syntaxDiags := hclsyntax.ParseConfig(src, path, hcl.InitialPos)
if syntaxDiags.HasErrors() {
f.hclDiags = append(f.hclDiags, syntaxDiags...)
return
}
formattedFile, diags := hclwrite.ParseConfig(src, path, hcl.InitialPos)
if diags.HasErrors() {
f.hclDiags = append(f.hclDiags, diags...)
return
}
out := formattedFile.Bytes()
if !bytes.Equal(src, out) {
if f.list {
f.Ui.Output(path)
}
if f.check {
f.checkSuccess = false
}
if f.writeFile {
if err := os.WriteFile(path, out, 0644); err != nil {
f.appendError(fmt.Errorf("Failed to write file %s: %w", path, err))
return
}
}
}
if f.writeStdout {
f.Ui.Output(string(out))
}
}
func isNomadFile(file fs.FileInfo) bool {
return !file.IsDir() && (filepath.Ext(file.Name()) == ".nomad" || filepath.Ext(file.Name()) == ".hcl")
}
func (f *FormatCommand) appendError(err error) {
f.errs = multierror.Append(f.errs, err)
}