From 3aef84cce318eaf6af6c2684002196e635faf4a5 Mon Sep 17 00:00:00 2001 From: Vamsi Avula Date: Wed, 19 Feb 2025 21:50:19 +0530 Subject: [PATCH] cligen: replace --out with an arg fixes #50 Change-Id: Id0000000144b6b54e5920c73987c197aef1e29d2 --- cmd/cligen/main.go | 19 ++++++++----------- cmd/cligen/md.cli | Bin 969 -> 850 bytes cmd/examples/greet/main.go | 2 +- cmd/examples/jj/main.go | 2 +- 4 files changed, 10 insertions(+), 13 deletions(-) diff --git a/cmd/cligen/main.go b/cmd/cligen/main.go index a05e200..9b4611b 100644 --- a/cmd/cligen/main.go +++ b/cmd/cligen/main.go @@ -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 | @@ -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) @@ -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)) } diff --git a/cmd/cligen/md.cli b/cmd/cligen/md.cli index 2a6ff9a1ecc4b66dc239ce220ec5251bae99a03b..b101b6e2938a04e91f37abdf1773cb5be497166d 100644 GIT binary patch delta 268 zcmX@feu+<e%;Xkvk}{+&8r(F(t7ik@0^610yRVvrB$5BM$>3mrG_*YH~?tS!yxk z|E7uh;(jd*j2zAxnK>y%sd6!^9JslQ%H>bFt?alw{`T6|*u< y=3`RgU|>tm$xKhpn|zjOnlu9w6AMtfm^r_+gn=bDF*A<=Lb0+F(kqHhF9QJK$xqGz delta 410 zcmb7y!8dj4VtH%$3gTt{*My1HmGyt$+ZXLwsloz4WrOe{tm)HQHXDP5^?g`=`{8?CtwfD_q z+ifp4(<}QHJ}5Pl_GPE%7Q9rVdnQj?=vIoyVj)UoEHW16g;qm?&Q!rkxy+z_50<=H zA}F-XGv$)*ifmrfpwg7(l`bmH&SfH)&RJbbEm->9);hMp4CD5D#YJrEI!NsRzleW? VxxP57V!B?BMc0tEe>XWG0H5G+g7W|X diff --git a/cmd/examples/greet/main.go b/cmd/examples/greet/main.go index 0dffd48..702838e 100644 --- a/cmd/examples/greet/main.go +++ b/cmd/examples/greet/main.go @@ -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 diff --git a/cmd/examples/jj/main.go b/cmd/examples/jj/main.go index 4c9a711..6ee42cf 100644 --- a/cmd/examples/jj/main.go +++ b/cmd/examples/jj/main.go @@ -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