treefmt/main.go
Brian McGee 1b517c6502
feat: add --on-unmatched
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>
2024-05-29 10:32:02 +01:00

41 lines
1.1 KiB
Go

package main
import (
_ "embed"
"fmt"
"os"
"git.numtide.com/numtide/treefmt/build"
"git.numtide.com/numtide/treefmt/cli"
"github.com/alecthomas/kong"
)
// We embed the sample toml file for use with the init flag.
//
//go:embed init.toml
var initBytes []byte
func main() {
// This is to maintain compatibility with 1.0.0 which allows specifying the version with a `treefmt --version` flag
// on the 'default' command. With Kong it would be better to have `treefmt version` so it would be treated as a
// separate command. As it is, we would need to weaken some of the `existingdir` and `existingfile` checks kong is
// doing for us in the default format command.
for _, arg := range os.Args {
if arg == "--version" || arg == "-V" {
fmt.Printf("%s %s\n", build.Name, build.Version)
return
} else if arg == "--init" || arg == "-i" {
if err := os.WriteFile("treefmt.toml", initBytes, 0o644); err != nil {
fmt.Printf("Failed to write treefmt.toml: %v\n", err)
os.Exit(1)
}
fmt.Printf("Generated treefmt.toml. Now it's your turn to edit it.\n")
return
}
}
ctx := kong.Parse(&cli.Cli, cli.Options...)
ctx.FatalIfErrorf(ctx.Run())
}