feat: implement init
Signed-off-by: Brian McGee <brian@bmcgee.ie>
This commit is contained in:
parent
9de4fd4cf9
commit
2552b2bfd0
@ -20,6 +20,7 @@ type Format struct {
|
|||||||
Walk walk.Type `enum:"auto,git,filesystem" default:"auto" help:"The method used to traverse the files within --tree-root. Currently supports 'auto', 'git' or 'filesystem'."`
|
Walk walk.Type `enum:"auto,git,filesystem" default:"auto" help:"The method used to traverse the files within --tree-root. Currently supports 'auto', 'git' or 'filesystem'."`
|
||||||
Verbosity int `name:"verbose" short:"v" type:"counter" default:"0" env:"LOG_LEVEL" help:"Set the verbosity of logs e.g. -vv."`
|
Verbosity int `name:"verbose" short:"v" type:"counter" default:"0" env:"LOG_LEVEL" help:"Set the verbosity of logs e.g. -vv."`
|
||||||
Version bool `name:"version" short:"V" help:"Print version"`
|
Version bool `name:"version" short:"V" help:"Print version"`
|
||||||
|
Init bool `name:"init" short:"i" help:"Create a new treefmt.toml"`
|
||||||
|
|
||||||
Paths []string `name:"paths" arg:"" type:"path" optional:"" help:"Paths to format. Defaults to formatting the whole tree."`
|
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"`
|
Stdin bool `help:"Format the context passed in via stdin"`
|
||||||
|
11
init.toml
Normal file
11
init.toml
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
# One CLI to format the code tree - https://git.numtide.com/numtide/treefmt
|
||||||
|
|
||||||
|
[formatter.mylanguage]
|
||||||
|
# Formatter to run
|
||||||
|
command = "command-to-run"
|
||||||
|
# Command-line arguments for the command
|
||||||
|
options = []
|
||||||
|
# Glob pattern of files to include
|
||||||
|
includes = [ "*.<language-extension>" ]
|
||||||
|
# Glob patterns of files to exclude
|
||||||
|
excludes = []
|
18
main.go
18
main.go
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
_ "embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
@ -9,6 +10,11 @@ import (
|
|||||||
"github.com/alecthomas/kong"
|
"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() {
|
func main() {
|
||||||
// This is to maintain compatibility with 1.0.0 which allows specifying the version with a `treefmt --version` flag
|
// 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
|
// on the 'default' command. With Kong it would be better to have `treefmt version` so it would be treated as a
|
||||||
@ -21,6 +27,18 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// For the same reasons above we implement init like this
|
||||||
|
for _, arg := range os.Args {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ctx := kong.Parse(&cli.Cli)
|
ctx := kong.Parse(&cli.Cli)
|
||||||
ctx.FatalIfErrorf(ctx.Run())
|
ctx.FatalIfErrorf(ctx.Run())
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user