From d53f98ea051105f2456fc951109ab1ccfc2da611 Mon Sep 17 00:00:00 2001 From: Brian McGee Date: Thu, 15 Feb 2024 09:43:16 +0000 Subject: [PATCH] feat: support --version Signed-off-by: Brian McGee --- build/build.go | 6 ++++++ cli/cli.go | 1 + main.go | 15 +++++++++++++++ 3 files changed, 22 insertions(+) create mode 100644 build/build.go diff --git a/build/build.go b/build/build.go new file mode 100644 index 0000000..bd1c65e --- /dev/null +++ b/build/build.go @@ -0,0 +1,6 @@ +package build + +var ( + Name = "treefmt" + Version = "v0.0.1+dev" +) diff --git a/cli/cli.go b/cli/cli.go index ae7dbf2..8b23262 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -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"` diff --git a/main.go b/main.go index 35a2685..714a4af 100644 --- a/main.go +++ b/main.go @@ -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()) }