forked from jamesog/nginxfmt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nginxfmt.go
150 lines (131 loc) · 2.99 KB
/
nginxfmt.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
// Sections of this code are Copyright 2009 The Go Authors. All rights reserved.
package main
import (
"bytes"
"flag"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"github.com/aluttik/go-crossplane"
)
var (
list = flag.Bool("l", false, "list files whose formatting differs from nginxfmt's")
write = flag.Bool("w", false, "write result to (source) file instead of stdout")
)
const (
indent = 2
)
func usage() {
fmt.Fprintf(os.Stderr, "usage: nginxfmt [flags] [path ...]\n")
flag.PrintDefaults()
}
func format(config crossplane.Config) ([]byte, error) {
var buf bytes.Buffer
err := crossplane.Build(&buf, config, &crossplane.BuildOptions{Indent: indent})
return buf.Bytes(), err
}
// If in == nil, the source is the contents of the file with the given filename.
func processFile(filename string, info fs.FileInfo, in io.Reader, out io.Writer) error {
src, err := readFile(filename, in)
if err != nil {
return err
}
p, err := crossplane.Parse(filename, &crossplane.ParseOptions{
Open: func(string) (io.Reader, error) { return bytes.NewReader(src), nil },
ParseComments: true,
StopParsingOnError: true,
})
if err != nil {
return err
}
res, err := format(p.Config[0])
if err != nil {
return err
}
if !bytes.Equal(src, res) {
if *list {
fmt.Println(filename)
}
if *write {
perm := info.Mode().Perm()
bakname := filename + ".bak"
err = os.Rename(filename, bakname)
if err != nil {
return err
}
err = os.WriteFile(filename, res, perm)
if err != nil {
os.Rename(bakname, filename)
return err
}
err = os.Remove(bakname)
if err != nil {
return err
}
}
}
if !*list && !*write {
res = append(res, '\n')
_, err = out.Write(res)
}
return err
}
func readFile(filename string, in io.Reader) ([]byte, error) {
if in == nil {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
in = f
defer f.Close()
}
src, err := io.ReadAll(in)
return src, err
}
func main() {
err := nginxfmtMain(os.Stdout)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func nginxfmtMain(out io.Writer) error {
flag.Usage = usage
flag.Parse()
args := flag.Args()
if len(args) == 0 {
if *write {
return fmt.Errorf("error: cannot use -w with standard input")
}
return processFile("<stdin>", nil, os.Stdin, out)
}
for _, arg := range args {
switch info, err := os.Stat(arg); {
case err != nil:
return err
case !info.IsDir():
err := processFile(arg, info, nil, out)
if err != nil {
fmt.Fprintf(os.Stderr, "error processing %s: %v\n", arg, err)
}
default:
err := filepath.WalkDir(arg, func(path string, f fs.DirEntry, err error) error {
if err != nil || f.IsDir() {
return err
}
info, err := f.Info()
if err != nil {
fmt.Fprintf(os.Stderr, "%s: error: %v", path, err)
return nil
}
return processFile(path, info, nil, out)
})
if err != nil {
fmt.Fprintf(os.Stderr, "%s: error walking directory: %v", arg, err)
}
}
}
return nil
}