diff --git a/pkg/cmd/attestation/attestation.go b/pkg/cmd/attestation/attestation.go index ec74e08ce..aa445f71a 100644 --- a/pkg/cmd/attestation/attestation.go +++ b/pkg/cmd/attestation/attestation.go @@ -32,7 +32,7 @@ func NewCmdAttestation(f *cmdutil.Factory) *cobra.Command { root.AddCommand(download.NewDownloadCmd(f, nil)) root.AddCommand(inspect.NewInspectCmd(f, nil)) root.AddCommand(verify.NewVerifyCmd(f, nil)) - root.AddCommand(tufrootverify.NewTUFRootVerifyCmd(f)) + root.AddCommand(tufrootverify.NewTUFRootVerifyCmd(f, nil)) return root } diff --git a/pkg/cmd/attestation/tufrootverify/tuf-root-verify.go b/pkg/cmd/attestation/tufrootverify/tufrootverify.go similarity index 89% rename from pkg/cmd/attestation/tufrootverify/tuf-root-verify.go rename to pkg/cmd/attestation/tufrootverify/tufrootverify.go index 631f46176..224edd6c9 100644 --- a/pkg/cmd/attestation/tufrootverify/tuf-root-verify.go +++ b/pkg/cmd/attestation/tufrootverify/tufrootverify.go @@ -5,7 +5,6 @@ import ( "os" "github.com/cli/cli/v2/pkg/cmd/attestation/auth" - "github.com/cli/cli/v2/pkg/cmd/attestation/logging" "github.com/cli/cli/v2/pkg/cmd/attestation/verification" "github.com/cli/cli/v2/pkg/cmdutil" @@ -14,7 +13,7 @@ import ( "github.com/spf13/cobra" ) -func NewTUFRootVerifyCmd(f *cmdutil.Factory) *cobra.Command { +func NewTUFRootVerifyCmd(f *cmdutil.Factory, runF func() error) *cobra.Command { var mirror string var root string var cmd = cobra.Command{ @@ -38,16 +37,20 @@ func NewTUFRootVerifyCmd(f *cmdutil.Factory) *cobra.Command { gh attestation tuf-root-verify --mirror https://tuf-repo.github.com --root /path/to/1.root.json `), RunE: func(cmd *cobra.Command, args []string) error { - logger := logging.NewDefaultLogger(f.IOStreams) - if err := auth.IsHostSupported(); err != nil { return err } + if runF != nil { + return runF() + } + if err := tufRootVerify(mirror, root); err != nil { return fmt.Errorf("Failed to verify the TUF repository: %w", err) } - fmt.Sprintln(logger.IO.Out, logger.ColorScheme.Green("Successfully verified the TUF repository")) + + io := f.IOStreams + fmt.Sprintln(io.Out, io.ColorScheme().Green("Successfully verified the TUF repository")) return nil }, } diff --git a/pkg/cmd/attestation/tufrootverify/tufrootverify_test.go b/pkg/cmd/attestation/tufrootverify/tufrootverify_test.go new file mode 100644 index 000000000..f48357487 --- /dev/null +++ b/pkg/cmd/attestation/tufrootverify/tufrootverify_test.go @@ -0,0 +1,57 @@ +package tufrootverify + +import ( + "bytes" + "testing" + + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + + "github.com/google/shlex" + "github.com/stretchr/testify/assert" +) + +func TestNewTUFRootVerifyCmd(t *testing.T) { + testIO, _, _, _ := iostreams.Test() + f := &cmdutil.Factory{ + IOStreams: testIO, + } + + testcases := []struct { + name string + cli string + wantsErr bool + }{ + { + name: "Missing mirror flag", + cli: "--root ../verification/embed/tuf-repo.github.com/root.json", + wantsErr: true, + }, + { + name: "Missing root flag", + cli: "--mirror https://tuf-repo.github.com", + wantsErr: true, + }, + } + + for _, tc := range testcases { + t.Run(tc.name, func(t *testing.T) { + cmd := NewTUFRootVerifyCmd(f, func() error { + return nil + }) + + argv, err := shlex.Split(tc.cli) + assert.NoError(t, err) + cmd.SetArgs(argv) + cmd.SetIn(&bytes.Buffer{}) + cmd.SetOut(&bytes.Buffer{}) + cmd.SetErr(&bytes.Buffer{}) + _, err = cmd.ExecuteC() + if tc.wantsErr { + assert.Error(t, err) + return + } + assert.NoError(t, err) + }) + } +}