dagger do: Improve help message

- Only print first line of comment
- Add "Available Options" section header

Fixes #1777

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi 2022-03-11 11:32:29 -08:00
parent aacabb1393
commit ae4e61aaa1
4 changed files with 27 additions and 21 deletions

View File

@ -128,13 +128,13 @@ func doHelpCmd(cmd *cobra.Command, _ []string) {
return return
} }
fmt.Println("") fmt.Printf("\nAvailable Actions:\n")
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.StripEscape) w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.StripEscape)
defer w.Flush() defer w.Flush()
for _, a := range action.Children { for _, a := range action.Children {
if !a.Hidden { if !a.Hidden {
lineParts := []string{"", a.Name, strings.TrimSpace(a.Comment)} lineParts := []string{"", a.Name, a.Documentation}
fmt.Fprintln(w, strings.Join(lineParts, "\t")) fmt.Fprintln(w, strings.Join(lineParts, "\t"))
} }
} }

View File

@ -6,6 +6,7 @@ import (
"path/filepath" "path/filepath"
"sort" "sort"
"strconv" "strconv"
"strings"
"cuelang.org/go/cue" "cuelang.org/go/cue"
"cuelang.org/go/cue/ast" "cuelang.org/go/cue/ast"
@ -346,3 +347,16 @@ func (v *Value) Default() (*Value, bool) {
func (v *Value) Doc() []*ast.CommentGroup { func (v *Value) Doc() []*ast.CommentGroup {
return v.Cue().Doc() 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
}

View File

@ -5,12 +5,11 @@ import (
) )
type Action struct { type Action struct {
Name string Name string
Hidden bool Hidden bool
Path cue.Path Path cue.Path
Comment string Documentation string
Children []*Action Children []*Action
// pkg string
} }
func (a *Action) AddChild(c *Action) { func (a *Action) AddChild(c *Action) {

View File

@ -199,9 +199,7 @@ func (p *Plan) fillAction() {
if !actions.Exists() { if !actions.Exists() {
return return
} }
for _, cg := range actions.Doc() { p.action.Documentation = actions.DocSummary()
p.action.Comment += cg.Text()
}
tasks := flow.Tasks() tasks := flow.Tasks()
@ -214,17 +212,12 @@ func (p *Plan) fillAction() {
a := prevAction.FindByPath(path) a := prevAction.FindByPath(path)
if a == nil { if a == nil {
v := p.Source().LookupPath(path) v := p.Source().LookupPath(path)
childComment := ""
for _, cg := range v.Doc() {
childComment += cg.Text()
}
a = &Action{ a = &Action{
Name: s.String(), Name: s.String(),
Hidden: s.PkgPath() != "", Hidden: s.PkgPath() != "",
Path: path, Path: path,
Comment: childComment, Documentation: v.DocSummary(),
Children: []*Action{}, Children: []*Action{},
} }
prevAction.AddChild(a) prevAction.AddChild(a)
} }