From 5fa1cae468d74de69bbfc9b5900c7fc076cdb3c2 Mon Sep 17 00:00:00 2001 From: "R.I.Pienaar" Date: Tue, 19 Dec 2023 16:03:02 +0100 Subject: [PATCH 1/3] (#49) Handle introspect correctly when top has required flags or args Signed-off-by: R.I.Pienaar --- app.go | 6 +++++- app_test.go | 12 ++++++++++++ go.mod | 4 ++-- go.sum | 4 ++-- 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/app.go b/app.go index 8fb4139..2f06bbd 100644 --- a/app.go +++ b/app.go @@ -44,6 +44,7 @@ type Application struct { noInterspersed bool // can flags be interspersed with args (or must they come first) defaultEnvars bool completion bool + introspect bool cheats map[string]string cheatTags []string helpFlagIsSet bool @@ -89,7 +90,7 @@ func New(name, help string) *Application { a.Flag("completion-bash", "Output possible completions for the given args.").Hidden().UnNegatableBoolVar(&a.completion) a.Flag("completion-script-bash", "Generate completion script for bash.").Hidden().PreAction(a.generateBashCompletionScript).UnNegatableBool() a.Flag("completion-script-zsh", "Generate completion script for ZSH.").Hidden().PreAction(a.generateZSHCompletionScript).UnNegatableBool() - a.Flag("fisk-introspect", "Introspect the application model").Hidden().Action(a.introspectAction).UnNegatableBool() + a.Flag("fisk-introspect", "Introspect the application model").Hidden().Action(a.introspectAction).UnNegatableBoolVar(&a.introspect) return a } @@ -255,6 +256,9 @@ func (a *Application) Parse(args []string) (command string, err error) { if a.completion { a.generateBashCompletion(context) a.terminate(0) + } else if a.introspect { + a.introspectAction(context) + a.terminate(0) } else { if parseErr != nil { return "", parseErr diff --git a/app_test.go b/app_test.go index d5db766..980ea86 100644 --- a/app_test.go +++ b/app_test.go @@ -584,9 +584,21 @@ var docFS embed.FS func TestCheatFile(t *testing.T) { c := newTestApp().CheatFile(docFS, "", "doc.go") + c.Command("x", "x").CheatFile(docFS, "y", "doc.go") assert.Contains(t, c.cheats["test"], "Package fisk provides") assert.Contains(t, c.cheats["y"], "Package fisk provides") + +} + +func TestFiskIntrospect(t *testing.T) { + var buf bytes.Buffer + c := newTestApp() + c.usageWriter = &buf + c.errorWriter = &buf + c.Flag("required", "required").Required().String() + c.MustParseWithUsage([]string{"--fisk-introspect"}) + assert.NotContains(t, buf.String(), "required flag --required not provided") } func TestParseWithUsage(t *testing.T) { diff --git a/go.mod b/go.mod index 663f10c..e0fb601 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,10 @@ module github.com/choria-io/fisk -go 1.19 +go 1.20 require ( github.com/stretchr/testify v1.8.4 - golang.org/x/text v0.13.0 + golang.org/x/text v0.14.0 ) require ( diff --git a/go.sum b/go.sum index addfcb8..e228b7b 100644 --- a/go.sum +++ b/go.sum @@ -4,8 +4,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= -golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= From a5b8657922c42a35903126ebb8868f58712bbd6f Mon Sep 17 00:00:00 2001 From: "R.I.Pienaar" Date: Tue, 19 Dec 2023 16:04:45 +0100 Subject: [PATCH 2/3] (misc) Bump ci versions Signed-off-by: R.I.Pienaar --- .github/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index f6ff468..23012a7 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -5,7 +5,7 @@ jobs: test: strategy: matrix: - go: ["1.19", "1.20"] + go: ["1.20", "1.21"] runs-on: ubuntu-latest steps: From edda7b99a937ae59186011b3ea88ffc4bf4ef11a Mon Sep 17 00:00:00 2001 From: "R.I.Pienaar" Date: Tue, 19 Dec 2023 16:09:28 +0100 Subject: [PATCH 3/3] (misc) Handle deprecated methods Signed-off-by: R.I.Pienaar --- usage.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/usage.go b/usage.go index b0e1b3d..8e59c95 100644 --- a/usage.go +++ b/usage.go @@ -5,6 +5,7 @@ import ( "bytes" "fmt" "go/doc" + "go/doc/comment" "io" "strings" "text/template" @@ -33,7 +34,14 @@ func formatTwoColumns(w io.Writer, indent, padding, width int, rows [][2]string) for _, row := range rows { buf := bytes.NewBuffer(nil) - doc.ToText(buf, row[1], "", preIndent, width-s-padding-indent) + d := new(doc.Package).Parser().Parse(row[1]) + pr := &comment.Printer{ + TextPrefix: "", + TextCodePrefix: preIndent, + TextWidth: width - s - padding - indent, + } + buf.Write(pr.Text(d)) + lines := strings.Split(strings.TrimRight(buf.String(), "\n"), "\n") fmt.Fprintf(w, "%s%-*s%*s", indentStr, s, row[0], padding, "") if len(row[0]) >= max { @@ -134,7 +142,15 @@ func (a *Application) UsageForContextWithTemplate(context *ParseContext, indent "Wrap": func(indent int, s string) string { buf := bytes.NewBuffer(nil) indentText := strings.Repeat(" ", indent) - doc.ToText(buf, s, indentText, " "+indentText, width-indent) + + d := new(doc.Package).Parser().Parse(s) + pr := &comment.Printer{ + TextPrefix: indentText, + TextCodePrefix: " " + indentText, + TextWidth: width - indent, + } + buf.Write(pr.Text(d)) + return buf.String() }, "FormatFlag": formatFlag,