feat: support --version

Signed-off-by: Brian McGee <brian@bmcgee.ie>
This commit is contained in:
Brian McGee 2024-02-15 09:43:16 +00:00
parent d4ab015bc6
commit d53f98ea05
Signed by: brianmcgee
GPG Key ID: D49016E76AD1E8C0
3 changed files with 22 additions and 0 deletions

6
build/build.go Normal file
View File

@ -0,0 +1,6 @@
package build
var (
Name = "treefmt"
Version = "v0.0.1+dev"
)

View File

@ -18,6 +18,7 @@ type Format struct {
TreeRoot string `type:"existingdir" default:"."`
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."`
Version bool `name:"version" short:"V" help:"Print version"`
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"`

15
main.go
View File

@ -1,11 +1,26 @@
package main
import (
"fmt"
"os"
"git.numtide.com/numtide/treefmt/build"
"git.numtide.com/numtide/treefmt/cli"
"github.com/alecthomas/kong"
)
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
}
}
ctx := kong.Parse(&cli.Cli)
ctx.FatalIfErrorf(ctx.Run())
}