Skip to content

Commit

Permalink
Exit non-zero on error (#131)
Browse files Browse the repository at this point in the history
Fixes #130

We change how errors are displayed on invalid config files. The error message for an
invalid config file now looks like this:

```sh
𝛌 upgrade-provider pulumi/pulumi-aiven
error: While parsing config: yaml: line 4: did not find expected ',' or '}'
```

It used to look like this:

```sh
𝛌 upgrade-provider pulumi/pulumi-aiven
Error: While parsing config: yaml: line 4: did not find expected ',' or '}'
Usage:
  upgrade-provider <provider> [flags]

Flags:
      --allow-missing-docs              If true, don't error on missing docs during tfgen.
                                        This is equivalent to setting PULUMI_MISSING_DOCS_ERROR=${! VALUE}.
      --create-failure-issue            Create an issue in the target repository if the upgrade attempt fails in CI.
      --experimental                    Enable experimental features, such as auto token mapping and auto aliasing
  -h, --help                            help for upgrade-provider

...
```
  • Loading branch information
iwahbe authored Aug 31, 2023
1 parent 31867f7 commit b7f53ad
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,20 @@ func cmd() *cobra.Command {
os.Exit(1)
}

// If PersistentPreRunE returns an error, then cobra displays the error *and*
// displays the result of `upgrade-provider --help`. If we don't want help to be
// displayed, we can set failedPreRun and return. Run will immediately fail with
// this error.
var failedPreRun error
cmd := &cobra.Command{
Use: "upgrade-provider <provider>",
Short: "upgrade-provider automates the process of upgrading a TF-bridged provider",
Args: cobra.ExactArgs(1),
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
err := initializeConfig(cmd)
if err != nil {
return err
failedPreRun = err
return nil
}
// Validate argument is {org}/{repo}
tok := strings.Split(args[0], "/")
Expand Down Expand Up @@ -149,6 +155,7 @@ func cmd() *cobra.Command {
return nil
},
Run: func(_ *cobra.Command, args []string) {
exitOnError(failedPreRun)
err := upgrade.UpgradeProvider(context, repoOrg, repoName)
if err != nil && context.CreateFailureIssue {
// $GITHUB_ACTION is a default env var within github
Expand Down Expand Up @@ -228,7 +235,9 @@ This is equivalent to setting PULUMI_MISSING_DOCS_ERROR=${! VALUE}.`)

func main() {
err := cmd().Execute()
contract.IgnoreError(err)
if err != nil {
os.Exit(1)
}
}

// Adapted from https://github.com/carolynvs/stingoftheviper/blob/main/main.go
Expand Down

0 comments on commit b7f53ad

Please sign in to comment.