Skip to content

Commit

Permalink
fix: honor user abort when upgrading recipes with check command
Browse files Browse the repository at this point in the history
  • Loading branch information
majori committed Feb 29, 2024
1 parent 3f66cb8 commit 7486129
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 36 deletions.
4 changes: 3 additions & 1 deletion internal/cli/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ func NewCheckCmd() *cobra.Command {
return option.Parse(&opts)
},
RunE: func(cmd *cobra.Command, args []string) error {
return runCheck(cmd, opts)
err := runCheck(cmd, opts)
return errorHandler(cmd, err)
},
PostRun: func(cmd *cobra.Command, args []string) {
cmd.Root().SetContext(cmd.Context())
Expand Down Expand Up @@ -138,6 +139,7 @@ func runCheck(cmd *cobra.Command, opts checkOptions) error {
for sauce, version := range upgrades {
cmd.Printf(" %s upgrade %s:%s\n", os.Args[0], sauce.CheckFrom, version)
}
cmd.Println("\nor rerun the command with '--upgrade' flag to upgrade all recipes to the latest version.")
}

var exitCode int
Expand Down
3 changes: 2 additions & 1 deletion internal/cli/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ my-recipe
return option.Parse(&opts)
},
RunE: func(cmd *cobra.Command, args []string) error {
return runCreate(cmd, opts)
err := runCreate(cmd, opts)
return errorHandler(cmd, err)
},
}

Expand Down
3 changes: 2 additions & 1 deletion internal/cli/eject.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ func NewEjectCmd() *cobra.Command {
return option.Parse(&opts)
},
RunE: func(cmd *cobra.Command, args []string) error {
return runEject(cmd, opts)
err := runEject(cmd, opts)
return errorHandler(cmd, err)
},
Example: `jalapeno eject`,
}
Expand Down
10 changes: 3 additions & 7 deletions internal/cli/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/futurice/jalapeno/pkg/recipe"
"github.com/futurice/jalapeno/pkg/recipeutil"
"github.com/futurice/jalapeno/pkg/ui/survey"
uiutil "github.com/futurice/jalapeno/pkg/ui/util"
"github.com/gofrs/uuid"
"github.com/spf13/cobra"
"golang.org/x/mod/semver"
Expand Down Expand Up @@ -43,7 +42,8 @@ func NewExecuteCmd() *cobra.Command {
return option.Parse(&opts)
},
RunE: func(cmd *cobra.Command, args []string) error {
return runExecute(cmd, opts)
err := runExecute(cmd, opts)
return errorHandler(cmd, err)
},
Example: `# Execute local recipe
jalapeno execute path/to/recipe
Expand Down Expand Up @@ -185,11 +185,7 @@ func runExecute(cmd *cobra.Command, opts executeOptions) error {

promptedValues, err := survey.PromptUserForValues(cmd.InOrStdin(), cmd.OutOrStdout(), varsWithoutValues, values)
if err != nil {
if errors.Is(err, uiutil.ErrUserAborted) {
return nil
} else {
return fmt.Errorf("error when prompting for values: %s", err)
}
return fmt.Errorf("error when prompting for values: %s", err)
}
values = recipeutil.MergeValues(values, promptedValues)
} else {
Expand Down
3 changes: 2 additions & 1 deletion internal/cli/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ func NewPullCmd() *cobra.Command {
return option.Parse(&opts)
},
RunE: func(cmd *cobra.Command, args []string) error {
return runPull(cmd, opts)
err := runPull(cmd, opts)
return errorHandler(cmd, err)
},
Example: `# Pull recipe from OCI repository
jalapeno pull ghcr.io/user/recipe:latest
Expand Down
3 changes: 2 additions & 1 deletion internal/cli/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ func NewPushCmd() *cobra.Command {
return option.Parse(&opts)
},
RunE: func(cmd *cobra.Command, args []string) error {
return runPush(cmd, opts)
err := runPush(cmd, opts)
return errorHandler(cmd, err)
},
Example: `# Push recipe to OCI repository
jalapeno push path/to/recipe ghcr.io/user/recipe
Expand Down
19 changes: 19 additions & 0 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package cli

import (
"context"
"errors"
"fmt"
"time"

"github.com/carlmjohnson/versioninfo"
uiutil "github.com/futurice/jalapeno/pkg/ui/util"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -65,3 +67,20 @@ func NewRootCmd() *cobra.Command {

return cmd
}

func errorHandler(cmd *cobra.Command, err error) error {
if err == nil {
return nil
}

// Print empty line before error message
cmd.Println()

// If the error is a user abort, don't print the error message
if errors.Is(err, uiutil.ErrUserAborted) {
cmd.Println("User aborted")
return nil
}

return err
}
4 changes: 2 additions & 2 deletions internal/cli/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ func NewTestCmd() *cobra.Command {
return option.Parse(&opts)
},
RunE: func(cmd *cobra.Command, args []string) error {
return runTest(cmd, opts)
err := runTest(cmd, opts)
return errorHandler(cmd, err)
},
Example: `# Run recipe tests
jalapeno test path/to/recipe
Expand Down Expand Up @@ -169,7 +170,6 @@ func runTest(cmd *cobra.Command, opts testOptions) error {
}

if len(formattedErrs) > 0 {
cmd.Println()
return fmt.Errorf("recipe tests failed: %w", errors.Join(formattedErrs...))
}

Expand Down
23 changes: 3 additions & 20 deletions internal/cli/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/futurice/jalapeno/pkg/recipeutil"
"github.com/futurice/jalapeno/pkg/ui/conflict"
"github.com/futurice/jalapeno/pkg/ui/survey"
uiutil "github.com/futurice/jalapeno/pkg/ui/util"
"github.com/gofrs/uuid"
"github.com/spf13/cobra"
"golang.org/x/mod/semver"
Expand Down Expand Up @@ -46,7 +45,8 @@ func NewUpgradeCmd() *cobra.Command {
return option.Parse(&opts)
},
RunE: func(cmd *cobra.Command, args []string) error {
return runUpgrade(cmd, opts)
err := runUpgrade(cmd, opts)
return errorHandler(cmd, err)
},
Example: `# Upgrade recipe with local recipe
jalapeno upgrade path/to/recipe
Expand Down Expand Up @@ -200,7 +200,6 @@ func runUpgrade(cmd *cobra.Command, opts upgradeOptions) error {
opts.Values.ParseEnvironmentVariables,
)
if err != nil {
cmd.Println()
return fmt.Errorf("failed to parse provided values: %w", err)
}

Expand All @@ -226,7 +225,6 @@ func runUpgrade(cmd *cobra.Command, opts upgradeOptions) error {
if v.Default != "" {
defaultValue, err := v.ParseDefaultValue()
if err != nil {
cmd.Println()
return fmt.Errorf("failed to parse default value for variable '%s': %w", v.Name, err)
}
values[v.Name] = defaultValue
Expand All @@ -237,7 +235,6 @@ func runUpgrade(cmd *cobra.Command, opts upgradeOptions) error {

// If there are still variables without values, return error
if len(varsWithoutDefaultValues) > 0 {
cmd.Println()
return recipeutil.NewNoInputError(varsWithoutDefaultValues)
}
}
Expand All @@ -251,10 +248,6 @@ func runUpgrade(cmd *cobra.Command, opts upgradeOptions) error {
)

if err != nil {
if errors.Is(err, uiutil.ErrUserAborted) {
return nil
}

return fmt.Errorf("error when prompting for values: %w", err)
}

Expand All @@ -279,7 +272,6 @@ func runUpgrade(cmd *cobra.Command, opts upgradeOptions) error {
if data, err := os.ReadFile(filepath.Join(opts.Dir, recipe.IgnoreFileName)); err == nil {
ignorePatterns = append(ignorePatterns, strings.Split(string(data), "\n")...)
} else if !errors.Is(err, fs.ErrNotExist) {
cmd.Println()
// something else happened than trying to read an ignore file that does not exist
return fmt.Errorf("failed to read ignore file: %w\n\n%s", err, cliutil.MakeRetryMessage(os.Args, values))
}
Expand All @@ -292,7 +284,6 @@ func runUpgrade(cmd *cobra.Command, opts upgradeOptions) error {
skip := false
for _, pattern := range ignorePatterns {
if matched, err := filepath.Match(pattern, path); err != nil {
cmd.Println()
return fmt.Errorf("bad ignore pattern '%s': %w\n\n%s", pattern, err, cliutil.MakeRetryMessage(os.Args, values))
} else if matched {
// file was marked as ignored for upgrades
Expand Down Expand Up @@ -348,13 +339,7 @@ func runUpgrade(cmd *cobra.Command, opts upgradeOptions) error {
)

if err != nil {
if errors.Is(err, uiutil.ErrUserAborted) {
cmd.Printf("User aborted\n\n%s\n", cliutil.MakeRetryMessage(os.Args, values))
return nil
}

cmd.Println()
return fmt.Errorf("error when prompting for question: %w\n\n%s", err, cliutil.MakeRetryMessage(os.Args, values))
return fmt.Errorf("error when solving file conflicts: %w", err)
}

if bytes.Equal(conflictResult, prevFile.Content) {
Expand All @@ -378,7 +363,6 @@ func runUpgrade(cmd *cobra.Command, opts upgradeOptions) error {
keep := false
for _, pattern := range ignorePatterns {
if matched, err := filepath.Match(pattern, filename); err != nil {
cmd.Println()
return fmt.Errorf("bad ignore pattern '%s': %w\n\n%s", pattern, err, cliutil.MakeRetryMessage(os.Args, values))
} else if matched {
keep = true
Expand All @@ -389,7 +373,6 @@ func runUpgrade(cmd *cobra.Command, opts upgradeOptions) error {
if !keep {
err := os.Remove(filepath.Join(opts.Dir, filename))
if err != nil {
cmd.Println()
return fmt.Errorf("failed to remove deprecated file '%s': %w", filename, err)
}

Expand Down
3 changes: 2 additions & 1 deletion internal/cli/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ func NewValidateCmd() *cobra.Command {
return option.Parse(&opts)
},
RunE: func(cmd *cobra.Command, args []string) error {
return runValidate(cmd, opts)
err := runValidate(cmd, opts)
return errorHandler(cmd, err)
},
Args: cobra.ExactArgs(1),
Example: `jalapeno validate path/to/recipe`,
Expand Down
3 changes: 2 additions & 1 deletion internal/cli/why.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ func NewWhyCmd() *cobra.Command {
return option.Parse(&opts)
},
RunE: func(cmd *cobra.Command, args []string) error {
return runWhy(cmd, opts)
err := runWhy(cmd, opts)
return errorHandler(cmd, err)
},
Example: `jalapeno why path/to/file`,
}
Expand Down

0 comments on commit 7486129

Please sign in to comment.