Skip to content

Commit

Permalink
feat: allow specifying formatters in cli
Browse files Browse the repository at this point in the history
Closes #9
  • Loading branch information
brianmcgee committed Dec 25, 2023
1 parent b6405d0 commit 9b84155
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 15 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/charmbracelet/log v0.3.1
github.com/gobwas/glob v0.2.3
github.com/juju/errors v1.0.0
github.com/otiai10/copy v1.14.0
github.com/stretchr/testify v1.8.4
github.com/vmihailenco/msgpack/v5 v5.4.1
github.com/ztrue/shutdown v0.1.1
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
github.com/otiai10/copy v1.14.0 h1:dCI/t1iTdYGtkvCuBG2BgR6KZa83PTclw4U5n2wAllU=
github.com/otiai10/copy v1.14.0/go.mod h1:ECfuL02W+/FkTWZWgQqXPWZgW9oeKCSQ5qVfSc4qc4w=
github.com/otiai10/mint v1.5.1 h1:XaPLeE+9vGbuyEHem1JNk3bYc7KKqyI/na0/mLd/Kks=
github.com/otiai10/mint v1.5.1/go.mod h1:MJm72SBthJjz8qhefc4z1PYEieWmy8Bku7CjcAqyUSM=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
Expand Down
17 changes: 10 additions & 7 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package cli

import "github.com/charmbracelet/log"
import (
"github.com/charmbracelet/log"
)

var Cli = Options{}

type Options struct {
AllowMissingFormatter bool `default:"false" help:"Do not exit with error if a configured formatter is missing"`
ClearCache bool `short:"c" help:"Reset the evaluation cache. Use in case the cache is not precise enough"`
ConfigFile string `type:"existingfile" default:"./treefmt.toml"`
TreeRoot string `type:"existingdir" default:"."`
Verbosity int `name:"verbose" short:"v" type:"counter" default:"0" env:"LOG_LEVEL" help:"Set the verbosity of logs e.g. -vv"`
AllowMissingFormatter bool `default:"false" help:"Do not exit with error if a configured formatter is missing"`
ClearCache bool `short:"c" help:"Reset the evaluation cache. Use in case the cache is not precise enough"`
ConfigFile string `type:"existingfile" default:"./treefmt.toml"`
Formatters []string `help:"Specify formatters to apply. Defaults to all formatters"`
TreeRoot string `type:"existingdir" default:"."`
Verbosity int `name:"verbose" short:"v" type:"counter" default:"0" env:"LOG_LEVEL" help:"Set the verbosity of logs e.g. -vv"`

Format Format `cmd:"" default:"."`
}

func (c *Options) ConfigureLogger() {
func (c *Options) Configure() {
log.SetReportTimestamp(false)

if c.Verbosity == 0 {
Expand Down
30 changes: 28 additions & 2 deletions internal/cli/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Format struct{}
func (f *Format) Run() error {
start := time.Now()

Cli.ConfigureLogger()
Cli.Configure()

l := log.WithPrefix("format")

Expand All @@ -42,8 +42,34 @@ func (f *Format) Run() error {
return errors.Annotate(err, "failed to read config file")
}

// create optional formatter filter set
formatterSet := make(map[string]bool)
for _, name := range Cli.Formatters {
_, ok := cfg.Formatters[name]
if !ok {
return errors.Errorf("formatter not found in config: %v", name)
}
formatterSet[name] = true
}

includeFormatter := func(name string) bool {
if len(formatterSet) == 0 {
return true
} else {
_, include := formatterSet[name]
return include
}
}

// init formatters
for name, formatter := range cfg.Formatters {
if !includeFormatter(name) {
// remove this formatter
delete(cfg.Formatters, name)
l.Debugf("formatter %v is not in formatter list %v, skipping", name, Cli.Formatters)
continue
}

err = formatter.Init(name)
if err == format.ErrFormatterNotFound && Cli.AllowMissingFormatter {
l.Debugf("formatter not found: %v", name)
Expand Down Expand Up @@ -126,7 +152,7 @@ func (f *Format) Run() error {
}
changes += count

println(fmt.Sprintf("%v files changed in %v", changes, time.Now().Sub(start)))
fmt.Printf("%v files changed in %v", changes, time.Now().Sub(start))
return nil
})

Expand Down
109 changes: 103 additions & 6 deletions internal/cli/format_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package cli

import (
"io"
"os"
"path/filepath"
"testing"

"git.numtide.com/numtide/treefmt/internal/format"
"github.com/BurntSushi/toml"
"github.com/alecthomas/kong"
"github.com/juju/errors"
cp "github.com/otiai10/copy"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -37,10 +41,56 @@ func newKong(t *testing.T, cli interface{}, options ...kong.Option) *kong.Kong {
return parser
}

func newCli(t *testing.T, args ...string) (*kong.Context, error) {
func tempFile(t *testing.T, path string) *os.File {
t.Helper()
file, err := os.Create(path)
if err != nil {
t.Fatalf("failed to create temporary file: %v", err)
}
return file
}

func cmd(t *testing.T, args ...string) ([]byte, error) {
t.Helper()

// create a new kong context
p := newKong(t, &Cli)
return p.Parse(args)
ctx, err := p.Parse(args)
if err != nil {
return nil, err
}

tempDir := t.TempDir()
tempOut := tempFile(t, filepath.Join(tempDir, "combined_output"))

// capture standard outputs before swapping them
stdout := os.Stdout
stderr := os.Stderr

// swap them temporarily
os.Stdout = tempOut
os.Stderr = tempOut

// run the command
if err = ctx.Run(); err != nil {
return nil, err
}

// reset and read the temporary output
if _, err = tempOut.Seek(0, 0); err != nil {
return nil, errors.Annotate(err, "failed to reset temp output for reading")
}

out, err := io.ReadAll(tempOut)
if err != nil {
return nil, errors.Annotate(err, "failed to read temp output")
}

// swap outputs back
os.Stdout = stdout
os.Stderr = stderr

return out, nil
}

func TestAllowMissingFormatter(t *testing.T) {
Expand All @@ -57,12 +107,59 @@ func TestAllowMissingFormatter(t *testing.T) {
},
})

ctx, err := newCli(t, "--config-file", configPath, "--tree-root", tempDir)
_, err := cmd(t, "--config-file", configPath, "--tree-root", tempDir)
as.ErrorIs(err, format.ErrFormatterNotFound)

_, err = cmd(t, "--config-file", configPath, "--tree-root", tempDir, "--allow-missing-formatter")
as.NoError(err)
as.Error(ctx.Run(), format.ErrFormatterNotFound)
}

ctx, err = newCli(t, "--config-file", configPath, "--tree-root", tempDir, "--allow-missing-formatter")
func TestSpecifyingFormatters(t *testing.T) {
as := require.New(t)

tempDir := t.TempDir()
configPath := tempDir + "/treefmt.toml"

as.NoError(cp.Copy("../../test/examples", tempDir), "failed to copy test data to temp dir")

writeConfig(t, configPath, format.Config{
Formatters: map[string]*format.Formatter{
"elm": {
Command: "echo",
Includes: []string{"*.elm"},
},
"nix": {
Command: "echo",
Includes: []string{"*.nix"},
},
"ruby": {
Command: "echo",
Includes: []string{"*.rb"},
},
},
})

out, err := cmd(t, "--clear-cache", "--config-file", configPath, "--tree-root", tempDir)
as.NoError(err)
as.Contains(string(out), "3 files changed")

out, err = cmd(t, "--clear-cache", "--config-file", configPath, "--tree-root", tempDir, "--formatters", "elm,nix")
as.NoError(err)
as.Contains(string(out), "2 files changed")

out, err = cmd(t, "--clear-cache", "--config-file", configPath, "--tree-root", tempDir, "--formatters", "ruby,nix")
as.NoError(err)
as.Contains(string(out), "2 files changed")

out, err = cmd(t, "--clear-cache", "--config-file", configPath, "--tree-root", tempDir, "--formatters", "nix")
as.NoError(err)
as.Contains(string(out), "1 files changed")

// test bad names

out, err = cmd(t, "--clear-cache", "--config-file", configPath, "--tree-root", tempDir, "--formatters", "foo")
as.Errorf(err, "formatter not found in config: foo")

as.NoError(ctx.Run())
out, err = cmd(t, "--clear-cache", "--config-file", configPath, "--tree-root", tempDir, "--formatters", "bar,foo")
as.Errorf(err, "formatter not found in config: bar")
}

0 comments on commit 9b84155

Please sign in to comment.