Skip to content

Commit

Permalink
cligen: replace --out with an arg
Browse files Browse the repository at this point in the history
fixes #50

Change-Id: Id0000000144b6b54e5920c73987c197aef1e29d2
  • Loading branch information
avamsi committed Feb 19, 2025
1 parent e79fb0f commit 3aef84c
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 13 deletions.
19 changes: 8 additions & 11 deletions cmd/cligen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,13 @@ func pkgDir(pkg *packages.Package) string {
return ""
}

type parseOptions struct {
Out string // output file to write metadata to
Debug bool // whether to print metadata
type options struct {
Debug bool // whether to print metadata
}

// cligen recursively parses the metadata of all Go packages in the current
// directory and its subdirectories, and writes it to the given output file.
//
//cli:usage cligen [opts]
func parse(opts *parseOptions) {
func cligen(opts *options, out string) {
var (
rootMd internal.RawMetadata
mode = (packages.NeedName | packages.NeedFiles |
Expand Down Expand Up @@ -134,7 +131,7 @@ func parse(opts *parseOptions) {
// invocation even if nothing about the metadata actually changed, which is
// not ideal (causes VCS noise, for example). To avoid this, we compare the
// new metadata with the old one and write only if they are different.
oldEncoded, err := os.ReadFile(opts.Out)
oldEncoded, err := os.ReadFile(out)
if !errors.Is(err, fs.ErrNotExist) { // if exists
assert.Nil(err)
oldDecoded := internal.DecodeAsRawMetadata(oldEncoded)
Expand All @@ -144,14 +141,14 @@ func parse(opts *parseOptions) {
}
// #nosec G306 -- G306 expects 0o600 or less but 0o644 is fine here as the
// metadata is not really sensitive (and is expected to be committed).
assert.Nil(os.WriteFile(opts.Out, rootMd.Encode(), 0o644))
fmt.Println("cligen: (re)generated", opts.Out)
assert.Nil(os.WriteFile(out, rootMd.Encode(), 0o644))
fmt.Println("cligen: (re)generated", out)
}

//go:generate go tool cligen --out=md.cli
//go:generate go tool cligen md.cli
//go:embed md.cli
var md []byte

func main() {
climate.RunAndExit(climate.Func(parse), climate.WithMetadata(md))
climate.RunAndExit(climate.Func(cligen), climate.WithMetadata(md))
}
Binary file modified cmd/cligen/md.cli
Binary file not shown.
2 changes: 1 addition & 1 deletion cmd/examples/greet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func greet(opts *greetOptions) {

// * These only work if you generate and pass along "metadata" like below --

//go:generate go tool cligen --out=md.cli
//go:generate go tool cligen md.cli
//go:embed md.cli
var md []byte

Expand Down
2 changes: 1 addition & 1 deletion cmd/examples/jj/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (g *git) Export() {
fmt.Println("export", g.J)
}

//go:generate go tool cligen --out=md.cli
//go:generate go tool cligen md.cli
//go:embed md.cli
var md []byte

Expand Down

0 comments on commit 3aef84c

Please sign in to comment.