Skip to content

Commit

Permalink
feat: add --on-unmatched
Browse files Browse the repository at this point in the history
By default, if a path does not match any formatter a log message at WARN level will be emitted. A user can change this by providing the `--on-unmatched` or `-u` flag and specifying a log level `debug,info,warn,error,fatal`.

If fatal, the process will exit with an error on the first unmatched path encountered.

Closes #302

Signed-off-by: Brian McGee <brian@bmcgee.ie>
  • Loading branch information
brianmcgee committed May 29, 2024
1 parent 6cf9524 commit 1b517c6
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 7 deletions.
4 changes: 3 additions & 1 deletion cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ type Format struct {
Version bool `name:"version" short:"V" help:"Print version."`
Init bool `name:"init" short:"i" help:"Create a new treefmt.toml."`

OnUnmatched log.Level `name:"on-unmatched" short:"u" default:"warn" help:"Log paths that did not match any formatters at the specified log level. Possible values are debug,info,warn,error,fatal."`

Paths []string `name:"paths" arg:"" type:"path" optional:"" help:"Paths to format. Defaults to formatting the whole tree."`
Stdin bool `help:"Format the context passed in via stdin."`

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

func ConfigureLogging() {
func configureLogging() {
log.SetReportTimestamp(false)

if Cli.Verbosity == 0 {
Expand Down
9 changes: 7 additions & 2 deletions cli/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ var (
)

func (f *Format) Run() (err error) {
// set log level and other options
configureLogging()

// cpu profiling
if Cli.CpuProfile != "" {
cpuProfile, err := os.Create(Cli.CpuProfile)
Expand Down Expand Up @@ -355,8 +358,10 @@ func applyFormatters(ctx context.Context) func() error {
}

if len(matches) == 0 {
// no match, so we send it direct to the processed channel
log.Debugf("no match found: %s", file.Path)
if Cli.OnUnmatched == log.FatalLevel {
return fmt.Errorf("no formatter for path: %s", file.Path)
}
log.Logf(Cli.OnUnmatched, "no formatter for path: %s", file.Path)
processedCh <- file
} else {
// record the match
Expand Down
58 changes: 58 additions & 0 deletions cli/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"bufio"
"fmt"
"os"
"os/exec"
"path"
Expand All @@ -21,6 +22,63 @@ import (
"github.com/stretchr/testify/require"
)

func TestOnUnmatched(t *testing.T) {
as := require.New(t)

// capture current cwd, so we can replace it after the test is finished
cwd, err := os.Getwd()
as.NoError(err)

t.Cleanup(func() {
// return to the previous working directory
as.NoError(os.Chdir(cwd))
})

tempDir := test.TempExamples(t)

paths := []string{
"go/go.mod",
"haskell/haskell.cabal",
"haskell/treefmt.toml",
"html/scripts/.gitkeep",
"nixpkgs.toml",
"python/requirements.txt",
"rust/Cargo.toml",
"touch.toml",
"treefmt.toml",
}

out, err := cmd(t, "-C", tempDir, "--allow-missing-formatter", "--on-unmatched", "fatal")
as.ErrorContains(err, fmt.Sprintf("no formatter for path: %s/%s", tempDir, paths[0]))

checkOutput := func(level string, output []byte) {
for _, p := range paths {
as.Contains(string(output), fmt.Sprintf("%s format: no formatter for path: %s/%s", level, tempDir, p))
}
}

// default is warn
out, err = cmd(t, "-C", tempDir, "--allow-missing-formatter", "-c")
as.NoError(err)
checkOutput("WARN", out)

out, err = cmd(t, "-C", tempDir, "--allow-missing-formatter", "-c", "--on-unmatched", "warn")
as.NoError(err)
checkOutput("WARN", out)

out, err = cmd(t, "-C", tempDir, "--allow-missing-formatter", "-c", "-u", "error")
as.NoError(err)
checkOutput("ERRO", out)

out, err = cmd(t, "-C", tempDir, "--allow-missing-formatter", "-c", "-v", "--on-unmatched", "info")
as.NoError(err)
checkOutput("INFO", out)

out, err = cmd(t, "-C", tempDir, "--allow-missing-formatter", "-c", "-vv", "-u", "debug")
as.NoError(err)
checkOutput("DEBU", out)
}

func TestCpuProfile(t *testing.T) {
as := require.New(t)
tempDir := test.TempExamples(t)
Expand Down
7 changes: 6 additions & 1 deletion cli/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"path/filepath"
"testing"

"github.com/charmbracelet/log"

"git.numtide.com/numtide/treefmt/stats"

"git.numtide.com/numtide/treefmt/test"
Expand All @@ -33,7 +35,7 @@ func cmd(t *testing.T, args ...string) ([]byte, error) {
t.Helper()

// create a new kong context
p := newKong(t, &Cli)
p := newKong(t, &Cli, Options...)
ctx, err := p.Parse(args)
if err != nil {
return nil, err
Expand All @@ -50,6 +52,8 @@ func cmd(t *testing.T, args ...string) ([]byte, error) {
os.Stdout = tempOut
os.Stderr = tempOut

log.SetOutput(tempOut)

// run the command
if err = ctx.Run(); err != nil {
return nil, err
Expand All @@ -68,6 +72,7 @@ func cmd(t *testing.T, args ...string) ([]byte, error) {
// swap outputs back
os.Stdout = stdout
os.Stderr = stderr
log.SetOutput(stderr)

return out, nil
}
Expand Down
39 changes: 39 additions & 0 deletions cli/mappers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cli

import (
"fmt"
"reflect"

"github.com/alecthomas/kong"
"github.com/charmbracelet/log"
)

var Options []kong.Option

func init() {
Options = []kong.Option{
kong.TypeMapper(reflect.TypeOf(log.DebugLevel), logLevelDecoder()),
}
}

func logLevelDecoder() kong.MapperFunc {
return func(ctx *kong.DecodeContext, target reflect.Value) error {
t, err := ctx.Scan.PopValue("string")
if err != nil {
return err
}
var str string
switch v := t.Value.(type) {
case string:
str = v
default:
return fmt.Errorf("expected a string but got %q (%T)", t, t.Value)
}
level, err := log.ParseLevel(str)
if err != nil {
return fmt.Errorf("failed to parse '%v' as log level: %w", level, err)
}
target.Set(reflect.ValueOf(level))
return nil
}
}
11 changes: 10 additions & 1 deletion docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Flags:
-v, --verbose Set the verbosity of logs e.g. -vv ($LOG_LEVEL).
-V, --version Print version.
-i, --init Create a new treefmt.toml.
-u, --on-unmatched=warn Log paths that did not match any formatters at the specified log level. Possible values are debug,info,warn,error,fatal.
--stdin Format the context passed in via stdin.
--cpu-profile=STRING The file into which a cpu profile will be written.
```
Expand Down Expand Up @@ -95,9 +96,17 @@ while `-vv` will also show `[DEBUG]` messages.

Create a new `treefmt.toml`.

### `-u --on-unmatched`

Log paths that did not match any formatters at the specified log level. Possible values are debug,info,warn,error,fatal.

### `--stdin`

Format the content passed in via stdin.
Format the context passed in via stdin.

### `--cpu-profile`

The file into which a cpu profile will be written.

### `-V, --version`

Expand Down
3 changes: 1 addition & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ func main() {
}
}

ctx := kong.Parse(&cli.Cli)
cli.ConfigureLogging()
ctx := kong.Parse(&cli.Cli, cli.Options...)
ctx.FatalIfErrorf(ctx.Run())
}

0 comments on commit 1b517c6

Please sign in to comment.