From 2d4b6ef2eef48c06567a38ab89ceb18025209ce8 Mon Sep 17 00:00:00 2001 From: Joel Longtine Date: Thu, 10 Mar 2022 14:53:37 -0700 Subject: [PATCH 1/6] Print error for 0.1.0 plans being loaded Signed-off-by: Joel Longtine --- cmd/dagger/cmd/do.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/cmd/dagger/cmd/do.go b/cmd/dagger/cmd/do.go index 7d74acc9..06cf8f9f 100644 --- a/cmd/dagger/cmd/do.go +++ b/cmd/dagger/cmd/do.go @@ -58,7 +58,13 @@ var doCmd = &cobra.Command{ p, err := loadPlan() if err != nil { - lg.Fatal().Err(err).Msg("failed to load plan") + errstring := err.Error() + + if strings.Contains(errstring, "cannot find package") && strings.Contains(errstring, "alpha.dagger.io") { + lg.Fatal().Msg("Attempting to load a 0.1.0 plan. Please upgrade your plan to use the latest version of dagger. Contact the Dagger team if you need help!") + } else { + lg.Fatal().Err(err).Msg("failed to load plan") + } } target := getTargetPath(args) From b3ac2b71670a9323a53566e9db5a7e0bb37e1e79 Mon Sep 17 00:00:00 2001 From: Joel Longtine Date: Thu, 10 Mar 2022 14:55:52 -0700 Subject: [PATCH 2/6] Change message Signed-off-by: Joel Longtine --- cmd/dagger/cmd/do.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/dagger/cmd/do.go b/cmd/dagger/cmd/do.go index 06cf8f9f..d53504ee 100644 --- a/cmd/dagger/cmd/do.go +++ b/cmd/dagger/cmd/do.go @@ -61,7 +61,7 @@ var doCmd = &cobra.Command{ errstring := err.Error() if strings.Contains(errstring, "cannot find package") && strings.Contains(errstring, "alpha.dagger.io") { - lg.Fatal().Msg("Attempting to load a 0.1.0 plan. Please upgrade your plan to use the latest version of dagger. Contact the Dagger team if you need help!") + 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") } From 43b9df74a862fefeeea7acf32a2fdf7fea68a3b3 Mon Sep 17 00:00:00 2001 From: Joel Longtine Date: Thu, 10 Mar 2022 15:15:50 -0700 Subject: [PATCH 3/6] Add test Signed-off-by: Joel Longtine --- cmd/dagger/cmd/do.go | 8 ++- tests/plan.bats | 5 ++ .../do/error_message_for_0.1_projects.cue | 60 +++++++++++++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 tests/plan/do/error_message_for_0.1_projects.cue diff --git a/cmd/dagger/cmd/do.go b/cmd/dagger/cmd/do.go index d53504ee..e137f661 100644 --- a/cmd/dagger/cmd/do.go +++ b/cmd/dagger/cmd/do.go @@ -130,7 +130,13 @@ func doHelp(cmd *cobra.Command, _ []string) { p, err := loadPlan() if err != nil { - errorMsg = "Error: failed to load plan\n\n" + errstring := err.Error() + 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 actionLookupPath := getTargetPath(cmd.Flags().Args()) diff --git a/tests/plan.bats b/tests/plan.bats index 2d6b4866..30432aee 100644 --- a/tests/plan.bats +++ b/tests/plan.bats @@ -38,6 +38,11 @@ setup() { refute_output --partial 'client.filesystem."./dependent_do".write' } +@test "plan/do: Nice error message for 0.1.0 projects" { + run "$DAGGER" "do" -p ./plan/do/error_message_for_0.1_projects.cue + assert_output --partial "Attempting to load a 0.1.0 plan." +} + @test "plan/hello" { # Europa loader handles the cwd differently, therefore we need to CD into the tree at or below the parent of cue.mod cd "$TESTDIR" diff --git a/tests/plan/do/error_message_for_0.1_projects.cue b/tests/plan/do/error_message_for_0.1_projects.cue new file mode 100644 index 00000000..4cd6b10e --- /dev/null +++ b/tests/plan/do/error_message_for_0.1_projects.cue @@ -0,0 +1,60 @@ +package main + +import ( + "alpha.dagger.io/dagger" + "alpha.dagger.io/aws/s3" + "alpha.dagger.io/netlify" + "alpha.dagger.io/os" +) + +repo: dagger.#Artifact & dagger.#Input + +hello: { + + dir: dagger.#Artifact & dagger.#Input + + ctr: os.#Container & { + command: """ + ls -l /src > /tmp/out + """ + mount: "/src": from: dir + } + + f: os.#File & { + from: ctr + path: "/tmp/out" + } + + message: f.contents & dagger.#Output +} + +// Website +web: { + source: os.#Dir & { + from: repo + path: "web" + } + + url: string & dagger.#Output + + // Where to host the website? + provider: *"s3" | "netlify" & dagger.#Input + + // Deploy to AWS S3 + if provider == "s3" { + url: "\(bucket.url)index.html" + bucket: s3.#Put & { + contentType: "text/html" + "source": source + } + } + + // Deploy to Netlify + if provider == "netlify" { + url: site.url + + site: netlify.#Site & { + contents: source + } + } +} From 8b3650c08da5c28311c9669bda3bf1f8a51f8c8c Mon Sep 17 00:00:00 2001 From: Joel Longtine Date: Thu, 10 Mar 2022 15:19:59 -0700 Subject: [PATCH 4/6] Fix test Signed-off-by: Joel Longtine --- tests/plan.bats | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/plan.bats b/tests/plan.bats index 30432aee..ecbe8a7d 100644 --- a/tests/plan.bats +++ b/tests/plan.bats @@ -40,7 +40,7 @@ setup() { @test "plan/do: Nice error message for 0.1.0 projects" { run "$DAGGER" "do" -p ./plan/do/error_message_for_0.1_projects.cue - assert_output --partial "Attempting to load a 0.1.0 plan." + assert_output --partial "Attempting to load a 0.1.0 project." } @test "plan/hello" { From 5114992a53a9310012b744529e987b30f67d33bf Mon Sep 17 00:00:00 2001 From: Joel Longtine Date: Thu, 10 Mar 2022 15:20:36 -0700 Subject: [PATCH 5/6] One more try. Signed-off-by: Joel Longtine --- tests/plan.bats | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/plan.bats b/tests/plan.bats index ecbe8a7d..b993ba47 100644 --- a/tests/plan.bats +++ b/tests/plan.bats @@ -40,7 +40,7 @@ setup() { @test "plan/do: Nice error message for 0.1.0 projects" { run "$DAGGER" "do" -p ./plan/do/error_message_for_0.1_projects.cue - assert_output --partial "Attempting to load a 0.1.0 project." + assert_output --partial "Attempting to load a dagger 0.1.0 project." } @test "plan/hello" { From 993c706faa8c657e4c218104b8186b36c4689079 Mon Sep 17 00:00:00 2001 From: Andrea Luzzardi Date: Thu, 10 Mar 2022 15:11:55 -0800 Subject: [PATCH 6/6] do: simplify help management Signed-off-by: Andrea Luzzardi --- cmd/dagger/cmd/do.go | 67 +++++++++++++------------------------------- plan/plan.go | 13 +++++++-- tests/plan.bats | 7 +++-- 3 files changed, 36 insertions(+), 51 deletions(-) diff --git a/cmd/dagger/cmd/do.go b/cmd/dagger/cmd/do.go index e137f661..4c679968 100644 --- a/cmd/dagger/cmd/do.go +++ b/cmd/dagger/cmd/do.go @@ -22,7 +22,6 @@ import ( var doCmd = &cobra.Command{ Use: "do [OPTIONS] ACTION [SUBACTION...]", Short: "Execute a dagger action.", - // Args: cobra.MinimumNArgs(1), PreRun: func(cmd *cobra.Command, args []string) { // Fix Viper bug for duplicate flags: // https://github.com/spf13/viper/issues/233 @@ -32,7 +31,7 @@ var doCmd = &cobra.Command{ }, Run: func(cmd *cobra.Command, args []string) { if len(args) < 1 { - doHelp(cmd, nil) + doHelpCmd(cmd, nil) return } @@ -58,13 +57,7 @@ var doCmd = &cobra.Command{ p, err := loadPlan() 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) @@ -114,52 +107,32 @@ func getTargetPath(args []string) cue.Path { return cue.MakePath(selectors...) } -func doHelp(cmd *cobra.Command, _ []string) { - w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.StripEscape) - defer w.Flush() +func doHelpCmd(cmd *cobra.Command, _ []string) { + lg := logger.New() - planPath := viper.GetString("plan") - - var ( - errorMsg string - loadedMsg string - actionLookupPathMsg string - action *plan.Action - actions []*plan.Action - ) + fmt.Printf("%s\n\n%s", cmd.Short, cmd.UsageString()) p, err := loadPlan() if err != nil { - errstring := err.Error() - 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 - actionLookupPath := getTargetPath(cmd.Flags().Args()) - action = p.Action().FindByPath(actionLookupPath) - if action == nil { - errorMsg = "Error: action not found\n\n" - } else { - actions = action.Children - actionLookupPathMsg = fmt.Sprintf(`%s:`, actionLookupPath.String()) - } + lg.Fatal().Err(err).Msg("failed to load plan") } - fmt.Printf(`%s%s -%s + 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 + } -%s + if len(action.Name) < 1 { + return + } -%s -`, errorMsg, cmd.Short, cmd.UsageString(), loadedMsg, actionLookupPathMsg) + fmt.Println("") + w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.StripEscape) + defer w.Flush() - // fmt.Fprintln(w, "Actions\tDescription\tPackage") - // fmt.Fprintln(w, "\t\t") - for _, a := range actions { + for _, a := range action.Children { if !a.Hidden { lineParts := []string{"", a.Name, strings.TrimSpace(a.Comment)} fmt.Fprintln(w, strings.Join(lineParts, "\t")) @@ -176,7 +149,7 @@ func init() { doCmd.Flags().StringArray("cache-from", []string{}, "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 { panic(err) diff --git a/plan/plan.go b/plan/plan.go index fe8b9e10..54de4eaa 100644 --- a/plan/plan.go +++ b/plan/plan.go @@ -2,7 +2,9 @@ package plan import ( "context" + "errors" "fmt" + "strings" "cuelang.org/go/cue" cueflow "cuelang.org/go/tools/flow" @@ -16,8 +18,9 @@ import ( ) var ( - ActionSelector = cue.Str("actions") - ClientSelector = cue.Str("client") + 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") + ClientSelector = cue.Str("client") ) type Plan struct { @@ -44,6 +47,12 @@ func Load(ctx context.Context, cfg Config) (*Plan, error) { v, err := compiler.Build("", nil, cfg.Args...) 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 } diff --git a/tests/plan.bats b/tests/plan.bats index b993ba47..ac4e9430 100644 --- a/tests/plan.bats +++ b/tests/plan.bats @@ -38,9 +38,12 @@ setup() { 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 - 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" {