do: simplify help management

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi 2022-03-10 15:11:55 -08:00
parent 5114992a53
commit 993c706faa
3 changed files with 36 additions and 51 deletions

View File

@ -22,7 +22,6 @@ import (
var doCmd = &cobra.Command{ var doCmd = &cobra.Command{
Use: "do [OPTIONS] ACTION [SUBACTION...]", Use: "do [OPTIONS] ACTION [SUBACTION...]",
Short: "Execute a dagger action.", Short: "Execute a dagger action.",
// Args: cobra.MinimumNArgs(1),
PreRun: func(cmd *cobra.Command, args []string) { PreRun: func(cmd *cobra.Command, args []string) {
// Fix Viper bug for duplicate flags: // Fix Viper bug for duplicate flags:
// https://github.com/spf13/viper/issues/233 // https://github.com/spf13/viper/issues/233
@ -32,7 +31,7 @@ var doCmd = &cobra.Command{
}, },
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 { if len(args) < 1 {
doHelp(cmd, nil) doHelpCmd(cmd, nil)
return return
} }
@ -58,14 +57,8 @@ var doCmd = &cobra.Command{
p, err := loadPlan() p, err := loadPlan()
if err != nil { if err != nil {
errstring := err.Error()
if strings.Contains(errstring, "cannot find package") && strings.Contains(errstring, "alpha.dagger.io") {
lg.Fatal().Msg("Attempting to load a dagger 0.1.0 project. Please upgrade your config to be compatible with this version of dagger. Contact the Dagger team if you need help!")
} else {
lg.Fatal().Err(err).Msg("failed to load plan") lg.Fatal().Err(err).Msg("failed to load plan")
} }
}
target := getTargetPath(args) target := getTargetPath(args)
doneCh := common.TrackCommand(ctx, cmd, &telemetry.Property{ doneCh := common.TrackCommand(ctx, cmd, &telemetry.Property{
@ -114,52 +107,32 @@ func getTargetPath(args []string) cue.Path {
return cue.MakePath(selectors...) return cue.MakePath(selectors...)
} }
func doHelp(cmd *cobra.Command, _ []string) { func doHelpCmd(cmd *cobra.Command, _ []string) {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.StripEscape) lg := logger.New()
defer w.Flush()
planPath := viper.GetString("plan") fmt.Printf("%s\n\n%s", cmd.Short, cmd.UsageString())
var (
errorMsg string
loadedMsg string
actionLookupPathMsg string
action *plan.Action
actions []*plan.Action
)
p, err := loadPlan() p, err := loadPlan()
if err != nil { if err != nil {
errstring := err.Error() lg.Fatal().Err(err).Msg("failed to load plan")
if strings.Contains(errstring, "cannot find package") && strings.Contains(errstring, "alpha.dagger.io") {
errorMsg = "Attempting to load a dagger 0.1.0 project. Please upgrade your config to be compatible with this version of dagger. Contact the Dagger team if you need help!\n\n"
// lg.Fatal().Msg("Attempting to load a dagger 0.1.0 project. Please upgrade your config to be compatible with this version of dagger. Contact the Dagger team if you need help!")
} else {
errorMsg = "Error: failed to load plan\n\n"
} }
} else {
loadedMsg = "Plan loaded from " + planPath target := getTargetPath(cmd.Flags().Args())
actionLookupPath := getTargetPath(cmd.Flags().Args()) action := p.Action().FindByPath(target)
action = p.Action().FindByPath(actionLookupPath)
if action == nil { if action == nil {
errorMsg = "Error: action not found\n\n" lg.Fatal().Msg(fmt.Sprintf("action %s not found", target.String()))
} else { return
actions = action.Children
actionLookupPathMsg = fmt.Sprintf(`%s:`, actionLookupPath.String())
} }
if len(action.Name) < 1 {
return
} }
fmt.Printf(`%s%s
%s fmt.Println("")
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.StripEscape)
defer w.Flush()
%s for _, a := range action.Children {
%s
`, errorMsg, cmd.Short, cmd.UsageString(), loadedMsg, actionLookupPathMsg)
// fmt.Fprintln(w, "Actions\tDescription\tPackage")
// fmt.Fprintln(w, "\t\t")
for _, a := range actions {
if !a.Hidden { if !a.Hidden {
lineParts := []string{"", a.Name, strings.TrimSpace(a.Comment)} lineParts := []string{"", a.Name, strings.TrimSpace(a.Comment)}
fmt.Fprintln(w, strings.Join(lineParts, "\t")) fmt.Fprintln(w, strings.Join(lineParts, "\t"))
@ -176,7 +149,7 @@ func init() {
doCmd.Flags().StringArray("cache-from", []string{}, doCmd.Flags().StringArray("cache-from", []string{},
"External cache sources (eg. user/app:cache, type=local,src=path/to/dir)") "External cache sources (eg. user/app:cache, type=local,src=path/to/dir)")
doCmd.SetHelpFunc(doHelp) doCmd.SetHelpFunc(doHelpCmd)
if err := viper.BindPFlags(doCmd.Flags()); err != nil { if err := viper.BindPFlags(doCmd.Flags()); err != nil {
panic(err) panic(err)

View File

@ -2,7 +2,9 @@ package plan
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"strings"
"cuelang.org/go/cue" "cuelang.org/go/cue"
cueflow "cuelang.org/go/tools/flow" cueflow "cuelang.org/go/tools/flow"
@ -16,6 +18,7 @@ import (
) )
var ( var (
ErrIncompatiblePlan = errors.New("attempting to load a dagger 0.1.0 project.\nPlease upgrade your config to be compatible with this version of dagger. Contact the Dagger team if you need help")
ActionSelector = cue.Str("actions") ActionSelector = cue.Str("actions")
ClientSelector = cue.Str("client") ClientSelector = cue.Str("client")
) )
@ -44,6 +47,12 @@ func Load(ctx context.Context, cfg Config) (*Plan, error) {
v, err := compiler.Build("", nil, cfg.Args...) v, err := compiler.Build("", nil, cfg.Args...)
if err != nil { if err != nil {
errstring := err.Error()
if strings.Contains(errstring, "cannot find package") && strings.Contains(errstring, "alpha.dagger.io") {
return nil, ErrIncompatiblePlan
}
return nil, err return nil, err
} }

View File

@ -38,9 +38,12 @@ setup() {
refute_output --partial 'client.filesystem."./dependent_do".write' refute_output --partial 'client.filesystem."./dependent_do".write'
} }
@test "plan/do: Nice error message for 0.1.0 projects" { @test "plan/do: nice error message for 0.1.0 projects" {
run "$DAGGER" "do" -p ./plan/do/error_message_for_0.1_projects.cue run "$DAGGER" "do" -p ./plan/do/error_message_for_0.1_projects.cue
assert_output --partial "Attempting to load a dagger 0.1.0 project." assert_output --partial "attempting to load a dagger 0.1.0 project."
run "$DAGGER" "do" -p ./plan/do/error_message_for_0.1_projects.cue test
assert_output --partial "attempting to load a dagger 0.1.0 project."
} }
@test "plan/hello" { @test "plan/hello" {