-
Notifications
You must be signed in to change notification settings - Fork 33
/
format.go
180 lines (143 loc) · 4.19 KB
/
format.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
package main
import (
"bytes"
"fmt"
"os"
"runtime"
"github.com/hashicorp/go-multierror"
"github.com/nokia/ntt/internal/fs"
"github.com/nokia/ntt/ttcn3/v2/printer"
"github.com/pmezard/go-difflib/difflib"
"github.com/spf13/cobra"
)
var (
FormatCommand = &cobra.Command{
Hidden: true,
Use: "format",
Short: "format files according to the canonical TTCN-3 style",
Long: `Format files according to the canonical TTCN-3 style.
The format command reads the given source files and formats them according to
the standard TTCN-3 style. Files from imports or generated files are not
formatted.
If a directory is given as argument, the command scans the directory for
manifest files (package.yml) and potential TTCN-3 source files to format.
Without any arguments, the command reads scans the current directory for
manifest files (package.yml) and potential TTCN-3 source files.
CANONICAL STYLE
Formatting style is a matter of taste and a subject of debate. A canonical
style settles such debates and makes code easier to read due to a consistent
style across projects and teams.
For this reason, the format command does not support custom confirguration like
maximum line length or indentation style.
EXIT STATUS
Exit status is zero if no errors were encountered, and non-zero otherwise.
The --diff or --list flags will cause the command to exit with non-zero status,
if any files need to be formatted.
WARNING
The canonical style is still developing and will change in the future as is
this tool. Do not use it in production yet.
`,
RunE: func(cmd *cobra.Command, args []string) error {
srcs, err := fs.TTCN3Files(Project.Sources...)
if err != nil {
return err
}
var merr *multierror.Error
for _, src := range srcs {
if err := processFile(src); err != nil {
merr = multierror.Append(merr, err)
}
}
if formattedFiles > 0 && (listFiles || diff) {
return fmt.Errorf("%d files with format differences", formattedFiles)
}
return merr.ErrorOrNil()
},
}
listFiles, diff, inplace bool
formattedFiles int
spaces int
)
func init() {
FormatCommand.Flags().BoolVarP(&inplace, "in-place", "i", false, "format files in place")
FormatCommand.Flags().BoolVarP(&diff, "diff", "d", false, "display diff instead of rewriting files. Exit with non-zero status if any files need to be formatted")
FormatCommand.Flags().BoolVarP(&listFiles, "list", "l", false, "list files whose formatting differs. Exit with non-zero status if any files need to be formatted")
FormatCommand.Flags().IntVarP(&spaces, "tabs-to-spaces", "s", 0, "convert each tab to N spaces")
}
func processFile(path string) error {
src, err := fs.Content(path)
if err != nil {
return err
}
var buf bytes.Buffer
p := printer.NewCanonicalPrinter(&buf)
p.Indent = -1
if spaces > 0 {
p.UseSpaces = true
p.TabWidth = spaces
}
if err := p.Fprint(src); err != nil {
return err
}
res := buf.Bytes()
if !bytes.Equal(src, res) {
formattedFiles++
if listFiles {
fmt.Fprintln(os.Stdout, path)
}
if diff {
d, err := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{
A: difflib.SplitLines(string(src)),
B: difflib.SplitLines(string(res)),
FromFile: fmt.Sprintf("%s.orig", path),
ToFile: path,
Context: 1,
})
if err != nil {
return err
}
fmt.Fprintln(os.Stdout, d)
}
if inplace {
info, err := os.Stat(path)
if err != nil {
return err
}
perm := info.Mode().Perm()
backup, err := backupFile(src, perm)
if err != nil {
return err
}
if err := os.WriteFile(path, res, perm); err != nil {
os.Rename(backup, path)
return err
}
if err := os.Remove(backup); err != nil {
return err
}
}
}
if !inplace && !diff && !listFiles {
fmt.Fprint(os.Stdout, string(res))
}
return nil
}
func backupFile(b []byte, perm os.FileMode) (string, error) {
f, err := os.CreateTemp("", "ntt-format-")
if err != nil {
return "", err
}
if runtime.GOOS != "windows" {
if err := os.Chmod(f.Name(), perm); err != nil {
f.Close()
os.Remove(f.Name())
return "", err
}
}
if _, err := f.Write(b); err != nil {
f.Close()
os.Remove(f.Name())
return "", err
}
return f.Name(), nil
}