Skip to content

Commit

Permalink
Merge pull request 'Implement Init and fix setting build variables' (#26
Browse files Browse the repository at this point in the history
) from feat/init into main

Reviewed-on: https://git.numtide.com/numtide/treefmt/pulls/26
Reviewed-by: Jonas Chevalier <[email protected]>
  • Loading branch information
Brian McGee committed Feb 28, 2024
2 parents 9de4fd4 + 49596b8 commit 8333c99
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
11 changes: 11 additions & 0 deletions init.toml
Original file line number Diff line number Diff line change
@@ -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 = []
14 changes: 14 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
_ "embed"
"fmt"
"os"

Expand All @@ -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
Expand All @@ -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
}
}

Expand Down
4 changes: 2 additions & 2 deletions nix/packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down

0 comments on commit 8333c99

Please sign in to comment.