File tree 3 files changed +45
-2
lines changed
3 files changed +45
-2
lines changed Original file line number Diff line number Diff line change @@ -37,6 +37,8 @@ type Format struct {
37
37
38
38
CpuProfile string `optional:"" help:"The file into which a cpu profile will be written."`
39
39
40
+ Ci bool `help:"Runs treefmt in a CI mode, enabling --no-cache, --fail-on-change and adjusting some other settings best suited to a CI use case."`
41
+
40
42
formatters map [string ]* format.Formatter
41
43
globalExcludes []glob.Glob
42
44
Original file line number Diff line number Diff line change @@ -34,6 +34,37 @@ func (f *Format) Run() (err error) {
34
34
// set log level and other options
35
35
f .configureLogging ()
36
36
37
+ // ci mode
38
+ if f .Ci {
39
+ f .NoCache = true
40
+ f .FailOnChange = true
41
+
42
+ // ensure INFO level
43
+ if f .Verbosity < 1 {
44
+ f .Verbosity = 1
45
+ }
46
+ // reconfigure logging
47
+ f .configureLogging ()
48
+
49
+ log .Info ("ci mode enabled" )
50
+
51
+ startAfter := time .Now ().
52
+ // truncate to second precision
53
+ Truncate (time .Second ).
54
+ // add one second
55
+ Add (1 * time .Second ).
56
+ // a little extra to ensure we don't start until the next second
57
+ Add (10 * time .Millisecond )
58
+
59
+ log .Debugf ("waiting until %v before continuing" , startAfter )
60
+
61
+ // Wait until we tick over into the next second before processing to ensure our EPOCH level modtime comparisons
62
+ // for change detection are accurate.
63
+ // This can fail in CI between checkout and running treefmt if everything happens too quickly.
64
+ // For humans, the second level precision should not be a problem as they are unlikely to run treefmt in sub-second succession.
65
+ <- time .After (time .Until (startAfter ))
66
+ }
67
+
37
68
// cpu profiling
38
69
if f .CpuProfile != "" {
39
70
cpuProfile , err := os .Create (f .CpuProfile )
Original file line number Diff line number Diff line change 31
31
<debug|info|warn|error|fatal>.
32
32
--stdin Format the context passed in via stdin.
33
33
--cpu-profile=STRING The file into which a cpu profile will be written.
34
+ --ci Runs treefmt in a CI mode, enabling --no-cache, --fail-on-change and adjusting some other settings best suited to a CI use case.
34
35
```
35
36
36
37
## Arguments
@@ -112,13 +113,22 @@ Format the context passed in via stdin.
112
113
113
114
The file into which a cpu profile will be written.
114
115
116
+ ### ` --ci `
117
+
118
+ Runs treefmt in a CI mode which does the following:
119
+
120
+ - ensures ` INFO ` level logging at a minimum
121
+ - enables ` --no-cache ` and ` --fail-on-change `
122
+ - introduces a small startup delay so we do not start processing until the second after the process started, thereby
123
+ ensuring the accuracy of our change detection based on second-level ` modtime ` .
124
+
115
125
### ` -V, --version `
116
126
117
127
Print version.
118
128
119
129
## CI integration
120
130
121
- Typically, you would use ` treefmt ` in CI with the ` --fail-on-change ` and ` --no-cache flags ` .
131
+ Typically, you would use ` treefmt ` in CI with the ` --ci ` flag .
122
132
123
133
You can configure a ` treefmt ` job in a GitHub pipeline for Ubuntu with ` nix-shell ` like this:
124
134
@@ -141,5 +151,5 @@ jobs:
141
151
name : nix-community
142
152
authToken : " ${{ secrets.CACHIX_AUTH_TOKEN }}"
143
153
- name : treefmt
144
- run : nix-shell -p treefmt --run "treefmt --fail-on-change --no-cache "
154
+ run : nix-shell -p treefmt --run "treefmt --ci "
145
155
` ` `
You can’t perform that action at this time.
0 commit comments