From 660d167c8434b19da455828e1f30ee7f3864638c Mon Sep 17 00:00:00 2001 From: Terry Howe Date: Mon, 20 Mar 2023 20:59:50 -0600 Subject: [PATCH] refactor: Logger initializes logging (#867) Signed-off-by: Terry Howe --- cmd/oras/attach.go | 3 ++- cmd/oras/blob/delete.go | 3 ++- cmd/oras/blob/fetch.go | 3 ++- cmd/oras/blob/push.go | 3 ++- cmd/oras/cp.go | 3 ++- cmd/oras/discover.go | 13 +++++++------ cmd/oras/internal/option/common.go | 17 ----------------- cmd/oras/login.go | 3 ++- cmd/oras/manifest/delete.go | 3 ++- cmd/oras/manifest/fetch.go | 3 ++- cmd/oras/manifest/fetch_config.go | 3 ++- cmd/oras/manifest/push.go | 3 ++- cmd/oras/pull.go | 3 ++- cmd/oras/push.go | 3 ++- cmd/oras/repo/ls.go | 3 ++- cmd/oras/repo/tags.go | 3 ++- cmd/oras/tag/tag.go | 3 ++- internal/trace/context.go | 16 +++++++++++++--- 18 files changed, 50 insertions(+), 41 deletions(-) diff --git a/cmd/oras/attach.go b/cmd/oras/attach.go index f436c9ab1..53828b706 100644 --- a/cmd/oras/attach.go +++ b/cmd/oras/attach.go @@ -28,6 +28,7 @@ import ( "oras.land/oras-go/v2/content/file" "oras.land/oras/cmd/oras/internal/option" "oras.land/oras/internal/graph" + "oras.land/oras/internal/trace" ) type attachOptions struct { @@ -98,7 +99,7 @@ Example - Attach file to the manifest tagged 'v1' in an OCI layout folder 'layou } func runAttach(opts attachOptions) error { - ctx, _ := opts.SetLoggerLevel() + ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose) annotations, err := opts.LoadManifestAnnotations() if err != nil { return err diff --git a/cmd/oras/blob/delete.go b/cmd/oras/blob/delete.go index d0551a702..9b99eb80a 100644 --- a/cmd/oras/blob/delete.go +++ b/cmd/oras/blob/delete.go @@ -23,6 +23,7 @@ import ( "github.com/spf13/cobra" "oras.land/oras-go/v2/errdef" "oras.land/oras/cmd/oras/internal/option" + "oras.land/oras/internal/trace" ) type deleteBlobOptions struct { @@ -70,7 +71,7 @@ Example - Delete a blob and print its descriptor: } func deleteBlob(opts deleteBlobOptions) (err error) { - ctx, _ := opts.SetLoggerLevel() + ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose) repo, err := opts.NewRepository(opts.targetRef, opts.Common) if err != nil { return err diff --git a/cmd/oras/blob/fetch.go b/cmd/oras/blob/fetch.go index f5c9b05f4..614a414aa 100644 --- a/cmd/oras/blob/fetch.go +++ b/cmd/oras/blob/fetch.go @@ -28,6 +28,7 @@ import ( "oras.land/oras-go/v2/content" "oras.land/oras-go/v2/registry/remote" "oras.land/oras/cmd/oras/internal/option" + "oras.land/oras/internal/trace" ) type fetchBlobOptions struct { @@ -89,7 +90,7 @@ Example - Fetch and print a blob from OCI image layout archive file 'layout.tar' } func fetchBlob(opts fetchBlobOptions) (fetchErr error) { - ctx, _ := opts.SetLoggerLevel() + ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose) var target oras.ReadOnlyTarget target, err := opts.NewReadonlyTarget(ctx, opts.Common) if err != nil { diff --git a/cmd/oras/blob/push.go b/cmd/oras/blob/push.go index 35b6e1619..9a21bbd64 100644 --- a/cmd/oras/blob/push.go +++ b/cmd/oras/blob/push.go @@ -25,6 +25,7 @@ import ( "oras.land/oras/cmd/oras/internal/display" "oras.land/oras/cmd/oras/internal/option" "oras.land/oras/internal/file" + "oras.land/oras/internal/trace" ) type pushBlobOptions struct { @@ -95,7 +96,7 @@ Example - Push blob 'hi.txt' into an OCI layout folder 'layout-dir': } func pushBlob(opts pushBlobOptions) (err error) { - ctx, _ := opts.SetLoggerLevel() + ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose) repo, err := opts.NewTarget(opts.Common) if err != nil { diff --git a/cmd/oras/cp.go b/cmd/oras/cp.go index 6214a1415..02edfcd40 100644 --- a/cmd/oras/cp.go +++ b/cmd/oras/cp.go @@ -28,6 +28,7 @@ import ( "oras.land/oras/cmd/oras/internal/display" "oras.land/oras/cmd/oras/internal/option" "oras.land/oras/internal/graph" + "oras.land/oras/internal/trace" ) type copyOptions struct { @@ -96,7 +97,7 @@ Example - Copy an artifact with multiple tags with concurrency tuned: } func runCopy(opts copyOptions) error { - ctx, _ := opts.SetLoggerLevel() + ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose) // Prepare source src, err := opts.From.NewReadonlyTarget(ctx, opts.Common) diff --git a/cmd/oras/discover.go b/cmd/oras/discover.go index 02904b92b..606f61faf 100644 --- a/cmd/oras/discover.go +++ b/cmd/oras/discover.go @@ -22,15 +22,16 @@ import ( "os" "strings" - "gopkg.in/yaml.v3" - "oras.land/oras-go/v2" - "oras.land/oras/cmd/oras/internal/option" - "oras.land/oras/internal/graph" - "github.com/need-being/go-tree" "github.com/opencontainers/image-spec/specs-go" ocispec "github.com/opencontainers/image-spec/specs-go/v1" "github.com/spf13/cobra" + "gopkg.in/yaml.v3" + + "oras.land/oras-go/v2" + "oras.land/oras/cmd/oras/internal/option" + "oras.land/oras/internal/graph" + "oras.land/oras/internal/trace" ) type discoverOptions struct { @@ -91,7 +92,7 @@ Example - Discover referrers of the manifest tagged 'v1' in an OCI layout folder } func runDiscover(opts discoverOptions) error { - ctx, _ := opts.SetLoggerLevel() + ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose) repo, err := opts.NewReadonlyTarget(ctx, opts.Common) if err != nil { return err diff --git a/cmd/oras/internal/option/common.go b/cmd/oras/internal/option/common.go index e9af3b226..9d058ef81 100644 --- a/cmd/oras/internal/option/common.go +++ b/cmd/oras/internal/option/common.go @@ -16,11 +16,7 @@ limitations under the License. package option import ( - "context" - - "github.com/sirupsen/logrus" "github.com/spf13/pflag" - "oras.land/oras/internal/trace" ) // Common option struct. @@ -34,16 +30,3 @@ func (opts *Common) ApplyFlags(fs *pflag.FlagSet) { fs.BoolVarP(&opts.Debug, "debug", "d", false, "debug mode") fs.BoolVarP(&opts.Verbose, "verbose", "v", false, "verbose output") } - -// SetLoggerLevel sets up the logger based on common options. -func (opts *Common) SetLoggerLevel() (context.Context, logrus.FieldLogger) { - var logLevel logrus.Level - if opts.Debug { - logLevel = logrus.DebugLevel - } else if opts.Verbose { - logLevel = logrus.InfoLevel - } else { - logLevel = logrus.WarnLevel - } - return trace.WithLoggerLevel(context.Background(), logLevel) -} diff --git a/cmd/oras/login.go b/cmd/oras/login.go index b102444a8..ae2cd8cd4 100644 --- a/cmd/oras/login.go +++ b/cmd/oras/login.go @@ -26,6 +26,7 @@ import ( "github.com/spf13/cobra" "oras.land/oras/cmd/oras/internal/option" "oras.land/oras/internal/credential" + "oras.land/oras/internal/trace" ) type loginOptions struct { @@ -73,7 +74,7 @@ Example - Log in with username and password in an interactive terminal and no TL } func runLogin(opts loginOptions) (err error) { - ctx, _ := opts.SetLoggerLevel() + ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose) // prompt for credential if opts.Password == "" { diff --git a/cmd/oras/manifest/delete.go b/cmd/oras/manifest/delete.go index da1d67638..5007911a3 100644 --- a/cmd/oras/manifest/delete.go +++ b/cmd/oras/manifest/delete.go @@ -24,6 +24,7 @@ import ( "oras.land/oras-go/v2/errdef" oerrors "oras.land/oras/cmd/oras/internal/errors" "oras.land/oras/cmd/oras/internal/option" + "oras.land/oras/internal/trace" ) type deleteOptions struct { @@ -75,7 +76,7 @@ Example - Delete a manifest by digest 'sha256:99e4703fbf30916f549cd6bfa9cdbab614 } func deleteManifest(opts deleteOptions) error { - ctx, _ := opts.SetLoggerLevel() + ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose) repo, err := opts.NewRepository(opts.targetRef, opts.Common) if err != nil { return err diff --git a/cmd/oras/manifest/fetch.go b/cmd/oras/manifest/fetch.go index 6a4d6cdb0..de10e5dcb 100644 --- a/cmd/oras/manifest/fetch.go +++ b/cmd/oras/manifest/fetch.go @@ -26,6 +26,7 @@ import ( "oras.land/oras-go/v2" "oras.land/oras-go/v2/registry/remote" "oras.land/oras/cmd/oras/internal/option" + "oras.land/oras/internal/trace" ) type fetchOptions struct { @@ -89,7 +90,7 @@ Example - Fetch raw manifest from an OCI layout archive file 'layout.tar': } func fetchManifest(opts fetchOptions) (fetchErr error) { - ctx, _ := opts.SetLoggerLevel() + ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose) target, err := opts.NewReadonlyTarget(ctx, opts.Common) if err != nil { diff --git a/cmd/oras/manifest/fetch_config.go b/cmd/oras/manifest/fetch_config.go index e0245f968..61115ba55 100644 --- a/cmd/oras/manifest/fetch_config.go +++ b/cmd/oras/manifest/fetch_config.go @@ -28,6 +28,7 @@ import ( "oras.land/oras-go/v2/content" "oras.land/oras/cmd/oras/internal/option" "oras.land/oras/internal/descriptor" + "oras.land/oras/internal/trace" ) type fetchConfigOptions struct { @@ -86,7 +87,7 @@ Example - Fetch and print the prettified descriptor of the config: } func fetchConfig(opts fetchConfigOptions) (fetchErr error) { - ctx, _ := opts.SetLoggerLevel() + ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose) repo, err := opts.NewReadonlyTarget(ctx, opts.Common) if err != nil { diff --git a/cmd/oras/manifest/push.go b/cmd/oras/manifest/push.go index 63de78c1d..74dd2d297 100644 --- a/cmd/oras/manifest/push.go +++ b/cmd/oras/manifest/push.go @@ -31,6 +31,7 @@ import ( "oras.land/oras/cmd/oras/internal/display" "oras.land/oras/cmd/oras/internal/option" "oras.land/oras/internal/file" + "oras.land/oras/internal/trace" ) type pushOptions struct { @@ -104,7 +105,7 @@ Example - Push a manifest to an OCI layout folder 'layout-dir' and tag with 'v1' } func pushManifest(opts pushOptions) error { - ctx, _ := opts.SetLoggerLevel() + ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose) var target oras.Target var err error target, err = opts.NewTarget(opts.Common) diff --git a/cmd/oras/pull.go b/cmd/oras/pull.go index 3bd1ffd7e..e66fc56dd 100644 --- a/cmd/oras/pull.go +++ b/cmd/oras/pull.go @@ -30,6 +30,7 @@ import ( "oras.land/oras/cmd/oras/internal/fileref" "oras.land/oras/cmd/oras/internal/option" "oras.land/oras/internal/graph" + "oras.land/oras/internal/trace" ) type pullOptions struct { @@ -183,7 +184,7 @@ func runPull(opts pullOptions) error { return ret, nil } - ctx, _ := opts.SetLoggerLevel() + ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose) target, err := opts.NewReadonlyTarget(ctx, opts.Common) if err != nil { return err diff --git a/cmd/oras/push.go b/cmd/oras/push.go index 921681438..37e3a74ab 100644 --- a/cmd/oras/push.go +++ b/cmd/oras/push.go @@ -30,6 +30,7 @@ import ( "oras.land/oras/cmd/oras/internal/display" "oras.land/oras/cmd/oras/internal/fileref" "oras.land/oras/cmd/oras/internal/option" + "oras.land/oras/internal/trace" ) type pushOptions struct { @@ -123,7 +124,7 @@ Example - Push file "hi.txt" into an OCI layout folder 'layout-dir' with tag 'te } func runPush(opts pushOptions) error { - ctx, _ := opts.SetLoggerLevel() + ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose) annotations, err := opts.LoadManifestAnnotations() if err != nil { return err diff --git a/cmd/oras/repo/ls.go b/cmd/oras/repo/ls.go index 55ee0e29a..0840bf566 100644 --- a/cmd/oras/repo/ls.go +++ b/cmd/oras/repo/ls.go @@ -22,6 +22,7 @@ import ( "github.com/spf13/cobra" "oras.land/oras/cmd/oras/internal/option" "oras.land/oras/internal/repository" + "oras.land/oras/internal/trace" ) type repositoryOptions struct { @@ -68,7 +69,7 @@ Example - List the repositories under the registry that include values lexically } func listRepository(opts repositoryOptions) error { - ctx, _ := opts.SetLoggerLevel() + ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose) reg, err := opts.Remote.NewRegistry(opts.hostname, opts.Common) if err != nil { return err diff --git a/cmd/oras/repo/tags.go b/cmd/oras/repo/tags.go index ed72cf138..1a2b26c1f 100644 --- a/cmd/oras/repo/tags.go +++ b/cmd/oras/repo/tags.go @@ -22,6 +22,7 @@ import ( "github.com/opencontainers/go-digest" "github.com/spf13/cobra" "oras.land/oras/cmd/oras/internal/option" + "oras.land/oras/internal/trace" ) type showTagsOptions struct { @@ -77,7 +78,7 @@ Example - Show tags associated with a digest: } func showTags(opts showTagsOptions) error { - ctx, logger := opts.SetLoggerLevel() + ctx, logger := trace.NewLogger(opts.Debug, opts.Verbose) finder, err := opts.NewReadonlyTarget(ctx, opts.Common) if err != nil { return err diff --git a/cmd/oras/tag/tag.go b/cmd/oras/tag/tag.go index 29c0f01e1..8859ea6df 100644 --- a/cmd/oras/tag/tag.go +++ b/cmd/oras/tag/tag.go @@ -22,6 +22,7 @@ import ( "oras.land/oras-go/v2" "oras.land/oras/cmd/oras/internal/display" "oras.land/oras/cmd/oras/internal/option" + "oras.land/oras/internal/trace" ) type tagOptions struct { @@ -71,7 +72,7 @@ Example - Tag the manifest 'v1.0.1' to 'v1.0.2' in an OCI layout folder 'layout- } func tagManifest(opts tagOptions) error { - ctx, _ := opts.SetLoggerLevel() + ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose) target, err := opts.NewTarget(opts.Common) if err != nil { return err diff --git a/internal/trace/context.go b/internal/trace/context.go index 0043d7dfd..249fa8a6c 100644 --- a/internal/trace/context.go +++ b/internal/trace/context.go @@ -26,11 +26,21 @@ type contextKey int // loggerKey is the associated key type for logger entry in context. const loggerKey contextKey = iota -// WithLoggerLevel returns a context with logrus log entry. -func WithLoggerLevel(ctx context.Context, level logrus.Level) (context.Context, logrus.FieldLogger) { +// NewLogger returns a logger. +func NewLogger(debug bool, verbose bool) (context.Context, logrus.FieldLogger) { + var logLevel logrus.Level + if debug { + logLevel = logrus.DebugLevel + } else if verbose { + logLevel = logrus.InfoLevel + } else { + logLevel = logrus.WarnLevel + } + logger := logrus.New() logger.SetFormatter(&logrus.TextFormatter{DisableQuote: true}) - logger.SetLevel(level) + logger.SetLevel(logLevel) + ctx := context.Background() entry := logger.WithContext(ctx) return context.WithValue(ctx, loggerKey, entry), entry }