Skip to content
New issue

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

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

Already on GitHub? # to your account

fix: error message for trust policy #933

Merged
merged 5 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions cmd/notation/policy/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ func importCmd() *cobra.Command {
Example - Import trust policy configuration from a file:
notation policy import my_policy.json
`,
Args: cobra.ExactArgs(1),
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("requires 1 argument but received %d.\nUsage: notation policy import <path-to-policy.json>\nPlease specify a trust policy file as the argument", len(args))
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
opts.filePath = args[0]
return runImport(cmd, opts)
Expand Down Expand Up @@ -71,7 +76,7 @@ func runImport(command *cobra.Command, opts importOpts) error {
// optional confirmation
if !opts.force {
if _, err := trustpolicy.LoadDocument(); err == nil {
confirmed, err := cmdutil.AskForConfirmation(os.Stdin, "Existing trust policy configuration found, do you want to overwrite it?", opts.force)
confirmed, err := cmdutil.AskForConfirmation(os.Stdin, "The trust policy configuration already exists, do you want to overwrite it?", opts.force)
if err != nil {
return err
}
Expand Down
5 changes: 4 additions & 1 deletion cmd/notation/policy/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@
// core process
policyJSON, err := os.ReadFile(policyPath)
if err != nil {
return fmt.Errorf("failed to load trust policy configuration, you may import one via `notation policy import <path-to-policy.json>`: %w", err)
if os.IsNotExist(err) {
return fmt.Errorf("failed to show trust policy configuration because the trust policy file does not exist.\nYou may import one via `notation policy import <path-to-policy.json>`")
}
return fmt.Errorf("failed to load trust policy configuration: %w", err)

Check warning on line 65 in cmd/notation/policy/show.go

View check run for this annotation

Codecov / codecov/patch

cmd/notation/policy/show.go#L65

Added line #L65 was not covered by tests
}
var doc trustpolicy.Document
if err = json.Unmarshal(policyJSON, &doc); err == nil {
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/suite/command/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var _ = Describe("trust policy maintainer", func() {
Host(Opts(), func(notation *utils.ExecOpts, artifact *Artifact, vhost *utils.VirtualHost) {
notation.ExpectFailure().
Exec("policy", "show").
MatchErrKeyWords("failed to load trust policy configuration", "notation policy import")
MatchErrKeyWords("failed to show trust policy configuration", "notation policy import")
})
})

Expand Down
Loading