Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/analyze: use exit status 1 and 2 for errors, improve error messages for invalid cli arguments #967

Merged
merged 8 commits into from
Nov 15, 2023

Conversation

maxfisher-g
Copy link
Contributor

@maxfisher-g maxfisher-g commented Nov 14, 2023

  1. print errors for missing flags
  2. better use of exit codes
  3. [internal] consoliate context object initialisation

…flags, exit codes)

Signed-off-by: Max Fisher <maxfisher@google.com>
Copy link
Collaborator

@adg adg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the headline news of this change is that you're now returning a nonzero exit code if the command fails; that should probably be in the commit subject line.

@@ -167,7 +168,7 @@ func staticAnalysis(ctx context.Context, pkg *pkgmanager.Pkg, resultStores *work
}
}

func main() {
func run() (int, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a compelling reason to distinguish the usage errors vs other failures? The code gets a lot simpler if you just return either status 0 or 1.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, you could just return error and leave the exit code decision up to main()

Copy link
Contributor Author

@maxfisher-g maxfisher-g Nov 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just thinking of matching other unix utilities in this way. Since the options are quite complex for this program (and there is also an intermediate docker layer whose options are handled by a helper script), I think it helps for usability to distinguish user errors from program errors.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is anything checking for the difference though?
If I saw "exit code 1" or "exit code 2" I'd still need to look at the other logs to figure out what went wrong.
I think unless another program is going to interpret your exit code in some meaningful way, it's better to err on the side of simplicity and just return 0 or 1.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. I'll implement the error type as you suggested in the other comment but just return exit code 1 on any error until there's a clear need to return distinct error codes.

cmd/analyze/main.go Outdated Show resolved Hide resolved
cmd/analyze/main.go Outdated Show resolved Hide resolved
@maxfisher-g
Copy link
Contributor Author

I think the headline news of this change is that you're now returning a nonzero exit code if the command fails; that should probably be in the commit subject line.

fair point, I'll add this to the final commit message and/or PR heading

Signed-off-by: Max Fisher <maxfisher@google.com>
@maxfisher-g maxfisher-g changed the title cmd/analyze usability improvements cmd/analyze: return exit code on error, print message if required flags are missing Nov 14, 2023
@maxfisher-g maxfisher-g requested a review from adg November 14, 2023 06:36
@@ -23,6 +24,13 @@ import (
"github.com/ossf/package-analysis/pkg/api/pkgecosystem"
)

const (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The more idiomatic way to do this in Go is to return an error and check for its type to determine the kind of error. In this case, you can declare a usageError type and then return that, check for it in main, and return exit code 2 in that case. (for all other errors just return error code 1)

type usageError struct {
  error
}

func usagef(format string, args ...any) error {
  return usageError{fmt.Errorf(format, args...)}
}

func run() error {
  ...
  if err := featureflags.Update(*features); err != nil {
    return usagef("failed to parse flags: %w", err)
  }
  ...
  return nil
}

func main() {
  if err := run(); err !=  nil {
    fmt.Fprintln(os.Stderr, err)
    if errors.As(err, &usageError{}) {
      os.Exit(2)
    }
    os.Exit(1)
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion, this idiom is really nice! I'll implement it.

…or 1

Signed-off-by: Max Fisher <maxfisher@google.com>
@maxfisher-g maxfisher-g requested a review from adg November 15, 2023 02:12
cmd/analyze/main.go Outdated Show resolved Hide resolved
cmd/analyze/main.go Show resolved Hide resolved
…ror or not, streamline error message for invalid feature flag

Signed-off-by: Max Fisher <maxfisher@google.com>
@maxfisher-g maxfisher-g changed the title cmd/analyze: return exit code on error, print message if required flags are missing cmd/analyze: use exit status 1 for errors, improve error messages for invalid cli arguments Nov 15, 2023
@@ -54,7 +54,7 @@ func Update(flags string) error {
if ff, ok := flagRegistry[n]; ok {
ff.isEnabled = isEnabled
} else {
return fmt.Errorf("%w: %s", ErrUndefinedFlag, n)
return fmt.Errorf("%w '%s'", ErrUndefinedFlag, n)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use %q

Copy link
Collaborator

@adg adg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM modulo my last comments

… available ecosystems in usage text

Signed-off-by: Max Fisher <maxfisher@google.com>
@maxfisher-g maxfisher-g changed the title cmd/analyze: use exit status 1 for errors, improve error messages for invalid cli arguments cmd/analyze: use exit status 1 and 2 for errors, improve error messages for invalid cli arguments Nov 15, 2023
@maxfisher-g maxfisher-g enabled auto-merge (squash) November 15, 2023 05:39
@maxfisher-g maxfisher-g merged commit 1740a95 into main Nov 15, 2023
12 checks passed
@maxfisher-g maxfisher-g deleted the analyze_main_cli_cleanups branch November 15, 2023 05:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants