From ae4e61aaa15e5b49923d8c6cca21a36b1a26a537 Mon Sep 17 00:00:00 2001 From: Andrea Luzzardi Date: Fri, 11 Mar 2022 11:32:29 -0800 Subject: [PATCH] dagger do: Improve help message - Only print first line of comment - Add "Available Options" section header Fixes #1777 Signed-off-by: Andrea Luzzardi --- cmd/dagger/cmd/do.go | 4 ++-- compiler/value.go | 14 ++++++++++++++ plan/action.go | 11 +++++------ plan/plan.go | 19 ++++++------------- 4 files changed, 27 insertions(+), 21 deletions(-) diff --git a/cmd/dagger/cmd/do.go b/cmd/dagger/cmd/do.go index 4c679968..61730f67 100644 --- a/cmd/dagger/cmd/do.go +++ b/cmd/dagger/cmd/do.go @@ -128,13 +128,13 @@ func doHelpCmd(cmd *cobra.Command, _ []string) { return } - fmt.Println("") + fmt.Printf("\nAvailable Actions:\n") w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.StripEscape) defer w.Flush() for _, a := range action.Children { if !a.Hidden { - lineParts := []string{"", a.Name, strings.TrimSpace(a.Comment)} + lineParts := []string{"", a.Name, a.Documentation} fmt.Fprintln(w, strings.Join(lineParts, "\t")) } } diff --git a/compiler/value.go b/compiler/value.go index 54ad84fd..bf9eb42e 100644 --- a/compiler/value.go +++ b/compiler/value.go @@ -6,6 +6,7 @@ import ( "path/filepath" "sort" "strconv" + "strings" "cuelang.org/go/cue" "cuelang.org/go/cue/ast" @@ -346,3 +347,16 @@ func (v *Value) Default() (*Value, bool) { func (v *Value) Doc() []*ast.CommentGroup { return v.Cue().Doc() } + +// DocSummary returns the first line of documentation +func (v *Value) DocSummary() string { + doc := "" + for _, c := range v.Doc() { + doc += c.Text() + } + + doc = strings.TrimSpace(doc) + doc = strings.SplitN(doc, "\n", 2)[0] + + return doc +} diff --git a/plan/action.go b/plan/action.go index 0cb79a23..eece24b4 100644 --- a/plan/action.go +++ b/plan/action.go @@ -5,12 +5,11 @@ import ( ) type Action struct { - Name string - Hidden bool - Path cue.Path - Comment string - Children []*Action - // pkg string + Name string + Hidden bool + Path cue.Path + Documentation string + Children []*Action } func (a *Action) AddChild(c *Action) { diff --git a/plan/plan.go b/plan/plan.go index 042e4dfc..04adf17a 100644 --- a/plan/plan.go +++ b/plan/plan.go @@ -199,9 +199,7 @@ func (p *Plan) fillAction() { if !actions.Exists() { return } - for _, cg := range actions.Doc() { - p.action.Comment += cg.Text() - } + p.action.Documentation = actions.DocSummary() tasks := flow.Tasks() @@ -214,17 +212,12 @@ func (p *Plan) fillAction() { a := prevAction.FindByPath(path) if a == nil { v := p.Source().LookupPath(path) - childComment := "" - for _, cg := range v.Doc() { - childComment += cg.Text() - } - a = &Action{ - Name: s.String(), - Hidden: s.PkgPath() != "", - Path: path, - Comment: childComment, - Children: []*Action{}, + Name: s.String(), + Hidden: s.PkgPath() != "", + Path: path, + Documentation: v.DocSummary(), + Children: []*Action{}, } prevAction.AddChild(a) }