pass iostream struct from command

Signed-off-by: Meredith Lancaster <malancas@github.com>
This commit is contained in:
Meredith Lancaster 2024-03-04 15:30:48 -07:00
parent e35bec9474
commit fa22214380
11 changed files with 18 additions and 32 deletions

View file

@ -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()

View file

@ -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) {

View file

@ -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()

View file

@ -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]

View file

@ -23,7 +23,7 @@ func TestRunInspect(t *testing.T) {
res := test.SuppressAndRestoreOutput()
defer res()
logger := logger.NewDefaultLogger()
logger := logger.NewSystemLogger()
opts := Options{
ArtifactPath: artifactPath,

View file

@ -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 {

View file

@ -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.

View file

@ -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 != "" {

View file

@ -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()

View file

@ -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)
}

View file

@ -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)