Merge pull request #1782 from aluzzardi/do-help-improvements
dagger do: Improve help message
This commit is contained in:
commit
21c6af0678
@ -3,6 +3,7 @@ package cmd
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@ -110,34 +111,44 @@ func getTargetPath(args []string) cue.Path {
|
||||
func doHelpCmd(cmd *cobra.Command, _ []string) {
|
||||
lg := logger.New()
|
||||
|
||||
fmt.Printf("%s\n\n%s", cmd.Short, cmd.UsageString())
|
||||
fmt.Println(cmd.Short)
|
||||
|
||||
err := printActions(os.Stdout, getTargetPath(cmd.Flags().Args()))
|
||||
|
||||
fmt.Printf("\n%s", cmd.UsageString())
|
||||
|
||||
p, err := loadPlan()
|
||||
if err != nil {
|
||||
lg.Fatal().Err(err).Msg("failed to load plan")
|
||||
}
|
||||
}
|
||||
|
||||
func printActions(w io.Writer, target cue.Path) error {
|
||||
p, err := loadPlan()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
target := getTargetPath(cmd.Flags().Args())
|
||||
action := p.Action().FindByPath(target)
|
||||
if action == nil {
|
||||
lg.Fatal().Msg(fmt.Sprintf("action %s not found", target.String()))
|
||||
return
|
||||
return fmt.Errorf("action %s not found", target.String())
|
||||
}
|
||||
|
||||
if len(action.Name) < 1 {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
||||
fmt.Println("")
|
||||
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.StripEscape)
|
||||
defer w.Flush()
|
||||
fmt.Printf("\nAvailable Actions:\n")
|
||||
tw := tabwriter.NewWriter(w, 0, 0, 1, ' ', tabwriter.StripEscape)
|
||||
defer tw.Flush()
|
||||
|
||||
for _, a := range action.Children {
|
||||
if !a.Hidden {
|
||||
lineParts := []string{"", a.Name, strings.TrimSpace(a.Comment)}
|
||||
fmt.Fprintln(w, strings.Join(lineParts, "\t"))
|
||||
lineParts := []string{"", a.Name, a.Documentation}
|
||||
fmt.Fprintln(tw, strings.Join(lineParts, "\t"))
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -8,9 +8,8 @@ type Action struct {
|
||||
Name string
|
||||
Hidden bool
|
||||
Path cue.Path
|
||||
Comment string
|
||||
Documentation string
|
||||
Children []*Action
|
||||
// pkg string
|
||||
}
|
||||
|
||||
func (a *Action) AddChild(c *Action) {
|
||||
|
11
plan/plan.go
11
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,16 +212,11 @@ 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,
|
||||
Documentation: v.DocSummary(),
|
||||
Children: []*Action{},
|
||||
}
|
||||
prevAction.AddChild(a)
|
||||
|
Reference in New Issue
Block a user