Skip to content

Commit d4aaba9

Browse files
committed
feat: ci mode
Adds a ci flag which does the following: - ensures INFO level logging at a minimum - --no-cache - --fail-on-change This also adds a delay on startup which is intended to ensure we do not start doing anything until we have moved into the second after the one in which the process started. This helps to ensure accurate comparisons of `modtime` using truncated second-level precision for change detection. This is only an issue in scenarios in which treefmt is being executed quickly in succession or directly after an automatic checkout, such as in CI. Signed-off-by: Brian McGee <[email protected]>
1 parent e384466 commit d4aaba9

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

cli/cli.go

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ type Format struct {
3737

3838
CpuProfile string `optional:"" help:"The file into which a cpu profile will be written."`
3939

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+
4042
formatters map[string]*format.Formatter
4143
globalExcludes []glob.Glob
4244

cli/format.go

+31
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,37 @@ func (f *Format) Run() (err error) {
3434
// set log level and other options
3535
f.configureLogging()
3636

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+
3768
// cpu profiling
3869
if f.CpuProfile != "" {
3970
cpuProfile, err := os.Create(f.CpuProfile)

docs/usage.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Flags:
3131
<debug|info|warn|error|fatal>.
3232
--stdin Format the context passed in via stdin.
3333
--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.
3435
```
3536

3637
## Arguments
@@ -112,13 +113,22 @@ Format the context passed in via stdin.
112113

113114
The file into which a cpu profile will be written.
114115

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+
115125
### `-V, --version`
116126

117127
Print version.
118128

119129
## CI integration
120130

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.
122132

123133
You can configure a `treefmt` job in a GitHub pipeline for Ubuntu with `nix-shell` like this:
124134

@@ -141,5 +151,5 @@ jobs:
141151
name: nix-community
142152
authToken: "${{ secrets.CACHIX_AUTH_TOKEN }}"
143153
- name: treefmt
144-
run: nix-shell -p treefmt --run "treefmt --fail-on-change --no-cache"
154+
run: nix-shell -p treefmt --run "treefmt --ci"
145155
```

0 commit comments

Comments
 (0)