diff --git a/pkg/cmd/attestation/download/download.go b/pkg/cmd/attestation/download/download.go index 830610c43..30c5bfe5e 100644 --- a/pkg/cmd/attestation/download/download.go +++ b/pkg/cmd/attestation/download/download.go @@ -8,6 +8,7 @@ import ( "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/cmd/attestation/artifact" "github.com/cli/cli/v2/pkg/cmd/attestation/api" + "github.com/cli/cli/v2/pkg/cmd/attestation/logger" "github.com/MakeNowJust/heredoc" "github.com/spf13/cobra" @@ -56,7 +57,7 @@ func NewDownloadCmd(f *cmdutil.Factory) *cobra.Command { opts.APIClient = api.NewLiveClient() // Create a logger for use throughout the download command - opts.ConfigureLogger() + opts.Logger = logger.NewLogger(f.IOStreams, false, opts.Verbose) // Configure the live OCI client opts.ConfigureOCIClient() diff --git a/pkg/cmd/attestation/download/download_test.go b/pkg/cmd/attestation/download/download_test.go index 73f59f731..528e435ff 100644 --- a/pkg/cmd/attestation/download/download_test.go +++ b/pkg/cmd/attestation/download/download_test.go @@ -31,7 +31,7 @@ func TestRunDownload(t *testing.T) { DigestAlgorithm: "sha512", OutputPath: tempDir, Limit: 30, - Logger: logger.NewDefaultLogger(), + Logger: logger.NewSystemLogger(), } t.Run("fetch and store attestations successfully", func(t *testing.T) { diff --git a/pkg/cmd/attestation/download/options.go b/pkg/cmd/attestation/download/options.go index 216558540..9b330bb85 100644 --- a/pkg/cmd/attestation/download/options.go +++ b/pkg/cmd/attestation/download/options.go @@ -21,12 +21,6 @@ type Options struct { Verbose bool } -// ConfigureLogger configures a logger using configuration provided -// through the options -func (opts *Options) ConfigureLogger() { - opts.Logger = logger.NewLogger(false, opts.Verbose) -} - // ConfigureOCIClient configures an OCI client func (opts *Options) ConfigureOCIClient() { opts.OCIClient = oci.NewLiveClient() diff --git a/pkg/cmd/attestation/inspect/inspect.go b/pkg/cmd/attestation/inspect/inspect.go index 3bf68ed8e..e2c8c4f51 100644 --- a/pkg/cmd/attestation/inspect/inspect.go +++ b/pkg/cmd/attestation/inspect/inspect.go @@ -7,6 +7,7 @@ import ( "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/cmd/attestation/artifact" + "github.com/cli/cli/v2/pkg/cmd/attestation/logger" "github.com/cli/cli/v2/pkg/cmd/attestation/verification" "github.com/MakeNowJust/heredoc" @@ -49,7 +50,7 @@ func NewInspectCmd(f *cmdutil.Factory) *cobra.Command { `), PreRunE: func(cmd *cobra.Command, args []string) error { // Create a logger for use throughout the inspect command - opts.ConfigureLogger() + opts.Logger = logger.NewDefaultLogger(f.IOStreams) // set the artifact path opts.ArtifactPath = args[0] diff --git a/pkg/cmd/attestation/inspect/inspect_test.go b/pkg/cmd/attestation/inspect/inspect_test.go index 3b94bbc0d..92a682dc5 100644 --- a/pkg/cmd/attestation/inspect/inspect_test.go +++ b/pkg/cmd/attestation/inspect/inspect_test.go @@ -23,7 +23,7 @@ func TestRunInspect(t *testing.T) { res := test.SuppressAndRestoreOutput() defer res() - logger := logger.NewDefaultLogger() + logger := logger.NewSystemLogger() opts := Options{ ArtifactPath: artifactPath, diff --git a/pkg/cmd/attestation/inspect/options.go b/pkg/cmd/attestation/inspect/options.go index ce8c132f8..6c028bfc4 100644 --- a/pkg/cmd/attestation/inspect/options.go +++ b/pkg/cmd/attestation/inspect/options.go @@ -25,12 +25,6 @@ func (opts *Options) Clean() { opts.BundlePath = filepath.Clean(opts.BundlePath) } -// ConfigureLogger configures a logger using configuration provided -// through the options -func (opts *Options) ConfigureLogger() { - opts.Logger = logger.NewLogger(false, opts.Verbose) -} - // AreFlagsValid checks that the provided flag combination is valid // and returns an error otherwise func (opts *Options) AreFlagsValid() error { diff --git a/pkg/cmd/attestation/logger/logger.go b/pkg/cmd/attestation/logger/logger.go index 8fcd2511f..e46cf70e7 100644 --- a/pkg/cmd/attestation/logger/logger.go +++ b/pkg/cmd/attestation/logger/logger.go @@ -15,11 +15,9 @@ type Logger struct { verbose bool } -func NewLogger(isQuiet, isVerbose bool) *Logger { - io := iostreams.System() - colorScheme := io.ColorScheme() +func NewLogger(io *iostreams.IOStreams, isQuiet, isVerbose bool) *Logger { return &Logger{ - ColorScheme: colorScheme, + ColorScheme: io.ColorScheme(), IO: io, quiet: isQuiet, verbose: isVerbose, @@ -27,11 +25,15 @@ func NewLogger(isQuiet, isVerbose bool) *Logger { } // NewDefaultLogger returns a Logger that with the default logging settings -func NewDefaultLogger() *Logger { +func NewDefaultLogger(io *iostreams.IOStreams) *Logger { isQuiet := false isVerbose := false - return NewLogger(isQuiet, isVerbose) + return NewLogger(io, isQuiet, isVerbose) +} + +func NewSystemLogger() *Logger { + return NewDefaultLogger(iostreams.System()) } // Printf writes the formatted arguments to the stdout writer. diff --git a/pkg/cmd/attestation/verify/options.go b/pkg/cmd/attestation/verify/options.go index 19aa1c23e..6d4a5a7f0 100644 --- a/pkg/cmd/attestation/verify/options.go +++ b/pkg/cmd/attestation/verify/options.go @@ -38,12 +38,6 @@ func (opts *Options) ConfigureOCIClient() { opts.OCIClient = oci.NewLiveClient() } -// ConfigureLogger configures a logger using configuration provided -// through the options -func (opts *Options) ConfigureLogger() { - opts.Logger = logger.NewLogger(opts.Quiet, opts.Verbose) -} - // Clean cleans the file path option values func (opts *Options) Clean() { if opts.BundlePath != "" { diff --git a/pkg/cmd/attestation/verify/verify.go b/pkg/cmd/attestation/verify/verify.go index 5eeb131a4..ae414ee85 100644 --- a/pkg/cmd/attestation/verify/verify.go +++ b/pkg/cmd/attestation/verify/verify.go @@ -71,7 +71,7 @@ func NewVerifyCmd(f *cmdutil.Factory) *cobra.Command { opts.APIClient = api.NewLiveClient() // Create a logger for use throughout the verify command - opts.ConfigureLogger() + opts.Logger = logger.NewLogger(f.IOStreams, opts.Quiet, opts.Verbose) // Configure the live OCI client opts.ConfigureOCIClient() diff --git a/pkg/cmd/attestation/verify/verify_test.go b/pkg/cmd/attestation/verify/verify_test.go index 6bad2f0aa..670d372cc 100644 --- a/pkg/cmd/attestation/verify/verify_test.go +++ b/pkg/cmd/attestation/verify/verify_test.go @@ -24,7 +24,7 @@ func TestRunVerify(t *testing.T) { res := test.SuppressAndRestoreOutput() defer res() - logger := logger.NewDefaultLogger() + logger := logger.NewSystemLogger() publicGoodOpts := Options{ ArtifactPath: test.NormalizeRelativePath("../test/data/sigstore-js-2.1.0.tgz"), @@ -176,7 +176,7 @@ func TestVerifySLSAPredicateType_InvalidPredicate(t *testing.T) { }, } - err := verifySLSAPredicateType(logger.NewDefaultLogger(), apr) + err := verifySLSAPredicateType(logger.NewSystemLogger(), apr) assert.Error(t, err) assert.ErrorIs(t, err, ErrNoMatchingSLSAPredicate) } diff --git a/pkg/cmd/attestation/verifytufroot/verify-tuf-root.go b/pkg/cmd/attestation/verifytufroot/verify-tuf-root.go index bb6f1c484..349e936cc 100644 --- a/pkg/cmd/attestation/verifytufroot/verify-tuf-root.go +++ b/pkg/cmd/attestation/verifytufroot/verify-tuf-root.go @@ -34,7 +34,7 @@ func NewVerifyTUFRootCmd(f *cmdutil.Factory) *cobra.Command { gh attestation tuf-root-verify --mirror https://tuf-repo.github.com --root /path/to/1.root.json `), Run: func(cmd *cobra.Command, args []string) { - logger := logger.NewDefaultLogger() + logger := logger.NewDefaultLogger(f.IOStreams) if err := verifyTUFRoot(mirror, root); err != nil { fmt.Sprintln(logger.IO.Out, logger.ColorScheme.Redf("Failed to verify the TUF repository: %s", err)) os.Exit(1)