From 2ad87c25041946479e9e802445fb7b2828984b39 Mon Sep 17 00:00:00 2001 From: Brian McGee Date: Fri, 23 Feb 2024 11:53:27 +0000 Subject: [PATCH 1/2] feat: implement init Signed-off-by: Brian McGee --- cli/cli.go | 1 + init.toml | 11 +++++++++++ main.go | 14 ++++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 init.toml diff --git a/cli/cli.go b/cli/cli.go index e1ed3a6..edb8caa 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -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'."` 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"` + 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."` Stdin bool `help:"Format the context passed in via stdin"` diff --git a/init.toml b/init.toml new file mode 100644 index 0000000..d332172 --- /dev/null +++ b/init.toml @@ -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 = [ "*." ] +# Glob patterns of files to exclude +excludes = [] \ No newline at end of file diff --git a/main.go b/main.go index 714a4af..b32fa87 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + _ "embed" "fmt" "os" @@ -9,6 +10,11 @@ import ( "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 @@ -18,6 +24,14 @@ func main() { 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 } } From 49596b8e08725afd0629a60ce54e56c08c723b37 Mon Sep 17 00:00:00 2001 From: Brian McGee Date: Fri, 23 Feb 2024 11:53:39 +0000 Subject: [PATCH 2/2] fix: setting build Name and Version Signed-off-by: Brian McGee --- nix/packages.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nix/packages.nix b/nix/packages.nix index e0f8604..2a7d2e1 100644 --- a/nix/packages.nix +++ b/nix/packages.nix @@ -31,8 +31,8 @@ ldflags = [ "-s" "-w" - "-X 'build.Name=${pname}'" - "-X 'build.Version=${version}'" + "-X git.numtide.com/numtide/treefmt/build.Name=${pname}" + "-X git.numtide.com/numtide/treefmt/build.Version=v${version}" ]; nativeBuildInputs =