-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathformat.go
220 lines (181 loc) · 5.9 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
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
package format
import (
"context"
"errors"
"fmt"
"io"
"os"
"os/signal"
"path/filepath"
"runtime/pprof"
"strings"
"syscall"
"time"
"github.com/charmbracelet/log"
"github.com/numtide/treefmt/config"
"github.com/numtide/treefmt/format"
"github.com/numtide/treefmt/stats"
"github.com/numtide/treefmt/walk"
"github.com/numtide/treefmt/walk/cache"
"github.com/spf13/cobra"
"github.com/spf13/viper"
bolt "go.etcd.io/bbolt"
)
var ErrFailOnChange = errors.New("unexpected changes detected, --fail-on-change is enabled")
func Run(v *viper.Viper, statz *stats.Stats, cmd *cobra.Command, paths []string) error {
cmd.SilenceUsage = true
cfg, err := config.FromViper(v)
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
if cfg.CI {
log.Info("ci mode enabled")
startAfter := time.Now().
// truncate to second precision
Truncate(time.Second).
// add one second
Add(1 * time.Second).
// a little extra to ensure we don't start until the next second
Add(10 * time.Millisecond)
log.Debugf("waiting until %v before continuing", startAfter)
// Wait until we tick over into the next second before processing to ensure our EPOCH level modtime comparisons
// for change detection are accurate.
// This can fail in CI between checkout and running treefmt if everything happens too quickly.
// For humans, the second level precision should not be a problem as they are unlikely to run treefmt in
// sub-second succession.
time.Sleep(time.Until(startAfter))
}
// cpu profiling
if cfg.CPUProfile != "" {
cpuProfile, err := os.Create(cfg.CPUProfile)
if err != nil {
return fmt.Errorf("failed to open file for writing cpu profile: %w", err)
} else if err = pprof.StartCPUProfile(cpuProfile); err != nil {
return fmt.Errorf("failed to start cpu profile: %w", err)
}
defer func() {
pprof.StopCPUProfile()
if err := cpuProfile.Close(); err != nil {
log.Errorf("failed to close cpu profile: %v", err)
}
}()
}
// set a prefix on the default logger
log.SetPrefix("format")
var db *bolt.DB
// open the db unless --no-cache was specified
if !cfg.NoCache {
db, err = cache.Open(cfg.TreeRoot)
if err != nil {
return fmt.Errorf("failed to open cache: %w", err)
}
// ensure db is closed after we're finished
defer func() {
if err := db.Close(); err != nil {
log.Errorf("failed to close cache: %v", err)
}
}()
}
if db != nil {
// clear the cache if desired
if cfg.ClearCache {
if err = cache.Clear(db); err != nil {
return fmt.Errorf("failed to clear cache: %w", err)
}
}
}
// create an overall app context
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// listen for shutdown signal and cancel the context
go func() {
exit := make(chan os.Signal, 1)
signal.Notify(exit, os.Interrupt, syscall.SIGTERM)
<-exit
cancel()
}()
// parse the walk type
walkType, err := walk.TypeString(cfg.Walk)
if err != nil {
return fmt.Errorf("invalid walk type: %w", err)
}
if walkType == walk.Stdin && len(paths) != 1 {
// check we have only received one path arg which we use for the file extension / matching to formatters
return fmt.Errorf("exactly one path should be specified when using the --stdin flag")
}
// checks all paths are contained within the tree root and exist
// also "normalize" paths so they're relative to cfg.TreeRoot
for i, path := range paths {
absolutePath, err := filepath.Abs(path)
if err != nil {
return fmt.Errorf("error computing absolute path of %s: %w", path, err)
}
relativePath, err := filepath.Rel(cfg.TreeRoot, absolutePath)
if err != nil {
return fmt.Errorf("error computing relative path from %s to %s: %s", cfg.TreeRoot, absolutePath, err)
}
if strings.HasPrefix(relativePath, "..") {
return fmt.Errorf("path %s not inside the tree root %s", path, cfg.TreeRoot)
}
paths[i] = relativePath
if walkType != walk.Stdin {
if _, err = os.Stat(absolutePath); err != nil {
return fmt.Errorf("path %s not found", path)
}
}
}
// create a composite formatter which will handle applying the correct formatters to each file we traverse
formatter, err := format.NewCompositeFormatter(cfg, statz)
if err != nil {
return fmt.Errorf("failed to create composite formatter: %w", err)
}
if db != nil {
// compare formatters with the db, busting the cache if the formatters have changed
if err := formatter.BustCache(db); err != nil {
return fmt.Errorf("failed to compare formatters: %w", err)
}
}
// create a new walker for traversing the paths
walker, err := walk.NewCompositeReader(walkType, cfg.TreeRoot, paths, db, statz)
if err != nil {
return fmt.Errorf("failed to create walker: %w", err)
}
// start traversing
files := make([]*walk.File, cfg.BatchSize)
for {
// read the next batch
readCtx, cancel := context.WithTimeout(ctx, 1*time.Second)
n, err := walker.Read(readCtx, files)
// ensure context is cancelled to release resources
cancel()
// format
if err := formatter.Apply(ctx, files[:n]); err != nil {
return fmt.Errorf("formatting failure: %w", err)
}
if errors.Is(err, io.EOF) {
// we have finished traversing
break
} else if err != nil {
// something went wrong
return fmt.Errorf("failed to read files: %w", err)
}
}
// finalize formatting
formatErr := formatter.Close(ctx)
// close the walker, ensuring any pending file release hooks finish
if err = walker.Close(); err != nil {
return fmt.Errorf("failed to close walker: %w", err)
}
// print stats to stdout, unless we are processing from stdin and therefore outputting the results to stdout
if !cfg.Stdin {
statz.Print()
}
if formatErr != nil {
// return an error if any formatting failures were detected
return formatErr
} else if cfg.FailOnChange && statz.Value(stats.Changed) != 0 {
// if fail on change has been enabled, check that no files were actually changed, throwing an error if so
return ErrFailOnChange
}
return nil
}