Skip to content

Commit 6ec5fd6

Browse files
authored
Add a test flag for alloy fmt (#1632)
1 parent e2f36e6 commit 6ec5fd6

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,8 @@ v1.2.0
280280

281281
- Add a `prometheus.exporter.catchpoint` component to collect metrics from Catchpoint. (@bominrahmani)
282282

283+
- Add the `-t/--test` flag to `alloy fmt` to check if a alloy config file is formatted correctly. (@kavfixnel)
284+
283285
### Enhancements
284286

285287
- (_Public preview_) Add native histogram support to `otelcol.receiver.prometheus`. (@wildum)

docs/sources/reference/cli/fmt.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,13 @@ Otherwise, `fmt` reads and formats the file from disk specified by the argument.
2929
The `--write` flag can be specified to replace the contents of the original file on disk with the formatted results.
3030
`--write` can only be provided when `fmt` isn't reading from standard input.
3131

32+
The `--test` flag can be specified to test if the contents of the file are formatted correctly.
33+
34+
The `--write` and `--test` flags are mutually exclusive.
35+
3236
The command fails if the file being formatted has syntactically incorrect {{< param "PRODUCT_NAME" >}} configuration, but doesn't validate whether {{< param "PRODUCT_NAME" >}} components are configured properly.
3337

3438
The following flags are supported:
3539

3640
* `--write`, `-w`: Write the formatted file back to disk when not reading from standard input.
41+
* `--test`, `-t`: Only test the input and return a non-zero exit code if changes would have been made.

internal/alloycli/cmd_fmt.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"io"
88
"os"
9+
"reflect"
910

1011
"github.com/spf13/cobra"
1112

@@ -17,6 +18,7 @@ import (
1718
func fmtCommand() *cobra.Command {
1819
f := &alloyFmt{
1920
write: false,
21+
test: false,
2022
}
2123

2224
cmd := &cobra.Command{
@@ -55,20 +57,26 @@ The -w flag can be used to write the formatted file back to disk. -w can not be
5557
}
5658

5759
cmd.Flags().BoolVarP(&f.write, "write", "w", f.write, "write result to (source) file instead of stdout")
60+
cmd.Flags().BoolVarP(&f.test, "test", "t", f.test, "exit with non-zero when changes would be made. Cannot be used with -w/--write")
5861
return cmd
5962
}
6063

6164
type alloyFmt struct {
6265
write bool
66+
test bool
6367
}
6468

6569
func (ff *alloyFmt) Run(configFile string) error {
70+
if ff.write && ff.test {
71+
return fmt.Errorf("Cannot use -w/--write and -t/--test at the same time")
72+
}
73+
6674
switch configFile {
6775
case "-":
6876
if ff.write {
6977
return fmt.Errorf("cannot use -w with standard input")
7078
}
71-
return format("<stdin>", nil, os.Stdin, false)
79+
return format("<stdin>", nil, os.Stdin, false, ff.test)
7280

7381
default:
7482
fi, err := os.Stat(configFile)
@@ -84,11 +92,11 @@ func (ff *alloyFmt) Run(configFile string) error {
8492
return err
8593
}
8694
defer f.Close()
87-
return format(configFile, fi, f, ff.write)
95+
return format(configFile, fi, f, ff.write, ff.test)
8896
}
8997
}
9098

91-
func format(filename string, fi os.FileInfo, r io.Reader, write bool) error {
99+
func format(filename string, fi os.FileInfo, r io.Reader, write bool, test bool) error {
92100
bb, err := io.ReadAll(r)
93101
if err != nil {
94102
return err
@@ -107,6 +115,14 @@ func format(filename string, fi os.FileInfo, r io.Reader, write bool) error {
107115
// Add a newline at the end of the file.
108116
_, _ = buf.Write([]byte{'\n'})
109117

118+
// If -t/--test flag is check, only check if file is formatted correctly
119+
if test {
120+
if !reflect.DeepEqual(bb, buf.Bytes()) {
121+
return fmt.Errorf("File %s is not formatted correctly", filename)
122+
}
123+
return nil
124+
}
125+
110126
if !write {
111127
_, err := io.Copy(os.Stdout, &buf)
112128
return err

0 commit comments

Comments
 (0)