Skip to content

Commit 2ad87c2

Browse files
committed
feat: implement init
Signed-off-by: Brian McGee <[email protected]>
1 parent 9de4fd4 commit 2ad87c2

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

cli/cli.go

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type Format struct {
2020
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'."`
2121
Verbosity int `name:"verbose" short:"v" type:"counter" default:"0" env:"LOG_LEVEL" help:"Set the verbosity of logs e.g. -vv."`
2222
Version bool `name:"version" short:"V" help:"Print version"`
23+
Init bool `name:"init" short:"i" help:"Create a new treefmt.toml"`
2324

2425
Paths []string `name:"paths" arg:"" type:"path" optional:"" help:"Paths to format. Defaults to formatting the whole tree."`
2526
Stdin bool `help:"Format the context passed in via stdin"`

init.toml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# One CLI to format the code tree - https://git.numtide.com/numtide/treefmt
2+
3+
[formatter.mylanguage]
4+
# Formatter to run
5+
command = "command-to-run"
6+
# Command-line arguments for the command
7+
options = []
8+
# Glob pattern of files to include
9+
includes = [ "*.<language-extension>" ]
10+
# Glob patterns of files to exclude
11+
excludes = []

main.go

+14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
_ "embed"
45
"fmt"
56
"os"
67

@@ -9,6 +10,11 @@ import (
910
"github.com/alecthomas/kong"
1011
)
1112

13+
// We embed the sample toml file for use with the init flag.
14+
//
15+
//go:embed init.toml
16+
var initBytes []byte
17+
1218
func main() {
1319
// This is to maintain compatibility with 1.0.0 which allows specifying the version with a `treefmt --version` flag
1420
// 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() {
1824
if arg == "--version" || arg == "-V" {
1925
fmt.Printf("%s %s\n", build.Name, build.Version)
2026
return
27+
} else if arg == "--init" || arg == "-i" {
28+
if err := os.WriteFile("treefmt.toml", initBytes, 0o644); err != nil {
29+
fmt.Printf("Failed to write treefmt.toml: %v\n", err)
30+
os.Exit(1)
31+
}
32+
33+
fmt.Printf("Generated treefmt.toml. Now it's your turn to edit it.\n")
34+
return
2135
}
2236
}
2337

0 commit comments

Comments
 (0)