diff --git a/cmd/oras/internal/command/logger.go b/cmd/oras/internal/command/logger.go new file mode 100644 index 000000000..5492ea3d8 --- /dev/null +++ b/cmd/oras/internal/command/logger.go @@ -0,0 +1,32 @@ +/* +Copyright The ORAS Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package command + +import ( + "context" + + "github.com/sirupsen/logrus" + "github.com/spf13/cobra" + "oras.land/oras/cmd/oras/internal/option" + "oras.land/oras/internal/trace" +) + +// GetLogger returns a new FieldLogger and an associated Context derived from command context. +func GetLogger(cmd *cobra.Command, opts *option.Common) (context.Context, logrus.FieldLogger) { + ctx, logger := trace.NewLogger(cmd.Context(), opts.Debug, opts.Verbose) + cmd.SetContext(ctx) + return ctx, logger +} diff --git a/cmd/oras/internal/option/common.go b/cmd/oras/internal/option/common.go index 60be023b6..d336f3d5a 100644 --- a/cmd/oras/internal/option/common.go +++ b/cmd/oras/internal/option/common.go @@ -16,14 +16,11 @@ limitations under the License. package option import ( - "context" "os" - "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/pflag" "golang.org/x/term" - "oras.land/oras/internal/trace" ) const NoTTYFlag = "no-tty" @@ -44,11 +41,6 @@ func (opts *Common) ApplyFlags(fs *pflag.FlagSet) { fs.BoolVarP(&opts.noTTY, NoTTYFlag, "", false, "[Preview] do not show progress output") } -// WithContext returns a new FieldLogger and an associated Context derived from ctx. -func (opts *Common) WithContext(ctx context.Context) (context.Context, logrus.FieldLogger) { - return trace.NewLogger(ctx, opts.Debug, opts.Verbose) -} - // Parse gets target options from user input. func (opts *Common) Parse(*cobra.Command) error { // use STDERR as TTY output since STDOUT is reserved for pipeable output diff --git a/cmd/oras/root/attach.go b/cmd/oras/root/attach.go index ce34eced5..7b9662ce4 100644 --- a/cmd/oras/root/attach.go +++ b/cmd/oras/root/attach.go @@ -27,6 +27,7 @@ import ( "oras.land/oras-go/v2/content/file" "oras.land/oras-go/v2/registry/remote/auth" "oras.land/oras/cmd/oras/internal/argument" + "oras.land/oras/cmd/oras/internal/command" "oras.land/oras/cmd/oras/internal/display" oerrors "oras.land/oras/cmd/oras/internal/errors" "oras.land/oras/cmd/oras/internal/option" @@ -106,7 +107,7 @@ Example - Attach file to the manifest tagged 'v1' in an OCI image layout folder } func runAttach(cmd *cobra.Command, opts *attachOptions) error { - ctx, logger := opts.WithContext(cmd.Context()) + ctx, logger := command.GetLogger(cmd, &opts.Common) annotations, err := opts.LoadManifestAnnotations() if err != nil { return err diff --git a/cmd/oras/root/blob/delete.go b/cmd/oras/root/blob/delete.go index 3dcca3307..af00e63e6 100644 --- a/cmd/oras/root/blob/delete.go +++ b/cmd/oras/root/blob/delete.go @@ -24,6 +24,7 @@ import ( "oras.land/oras-go/v2/errdef" "oras.land/oras-go/v2/registry/remote/auth" "oras.land/oras/cmd/oras/internal/argument" + "oras.land/oras/cmd/oras/internal/command" oerrors "oras.land/oras/cmd/oras/internal/errors" "oras.land/oras/cmd/oras/internal/option" "oras.land/oras/internal/registryutil" @@ -72,7 +73,7 @@ Example - Delete a blob and print its descriptor: } func deleteBlob(cmd *cobra.Command, opts *deleteBlobOptions) (err error) { - ctx, logger := opts.WithContext(cmd.Context()) + ctx, logger := command.GetLogger(cmd, &opts.Common) blobs, err := opts.NewBlobDeleter(opts.Common, logger) if err != nil { return err diff --git a/cmd/oras/root/blob/fetch.go b/cmd/oras/root/blob/fetch.go index acab90668..44694c428 100644 --- a/cmd/oras/root/blob/fetch.go +++ b/cmd/oras/root/blob/fetch.go @@ -27,6 +27,7 @@ import ( "oras.land/oras-go/v2/content" "oras.land/oras-go/v2/registry/remote" "oras.land/oras/cmd/oras/internal/argument" + "oras.land/oras/cmd/oras/internal/command" "oras.land/oras/cmd/oras/internal/display/status/track" oerrors "oras.land/oras/cmd/oras/internal/errors" "oras.land/oras/cmd/oras/internal/option" @@ -95,7 +96,7 @@ Example - Fetch and print a blob from OCI image layout archive file 'layout.tar' } func fetchBlob(cmd *cobra.Command, opts *fetchBlobOptions) (fetchErr error) { - ctx, logger := opts.WithContext(cmd.Context()) + ctx, logger := command.GetLogger(cmd, &opts.Common) var target oras.ReadOnlyTarget target, err := opts.NewReadonlyTarget(ctx, opts.Common, logger) if err != nil { diff --git a/cmd/oras/root/blob/push.go b/cmd/oras/root/blob/push.go index b399600c3..44fa4477b 100644 --- a/cmd/oras/root/blob/push.go +++ b/cmd/oras/root/blob/push.go @@ -26,6 +26,7 @@ import ( "github.com/spf13/cobra" "oras.land/oras-go/v2" "oras.land/oras/cmd/oras/internal/argument" + "oras.land/oras/cmd/oras/internal/command" "oras.land/oras/cmd/oras/internal/display/status" "oras.land/oras/cmd/oras/internal/display/status/track" oerrors "oras.land/oras/cmd/oras/internal/errors" @@ -101,7 +102,7 @@ Example - Push blob 'hi.txt' into an OCI image layout folder 'layout-dir': } func pushBlob(cmd *cobra.Command, opts *pushBlobOptions) (err error) { - ctx, logger := opts.WithContext(cmd.Context()) + ctx, logger := command.GetLogger(cmd, &opts.Common) target, err := opts.NewTarget(opts.Common, logger) if err != nil { diff --git a/cmd/oras/root/cp.go b/cmd/oras/root/cp.go index fb844baef..bf59d5d0f 100644 --- a/cmd/oras/root/cp.go +++ b/cmd/oras/root/cp.go @@ -32,6 +32,7 @@ import ( "oras.land/oras-go/v2/registry/remote" "oras.land/oras-go/v2/registry/remote/auth" "oras.land/oras/cmd/oras/internal/argument" + "oras.land/oras/cmd/oras/internal/command" "oras.land/oras/cmd/oras/internal/display/status" "oras.land/oras/cmd/oras/internal/display/status/track" oerrors "oras.land/oras/cmd/oras/internal/errors" @@ -107,7 +108,7 @@ Example - Copy an artifact with multiple tags with concurrency tuned: } func runCopy(cmd *cobra.Command, opts *copyOptions) error { - ctx, logger := opts.WithContext(cmd.Context()) + ctx, logger := command.GetLogger(cmd, &opts.Common) // Prepare source src, err := opts.From.NewReadonlyTarget(ctx, opts.Common, logger) diff --git a/cmd/oras/root/discover.go b/cmd/oras/root/discover.go index b9d894b6d..b3e7b695a 100644 --- a/cmd/oras/root/discover.go +++ b/cmd/oras/root/discover.go @@ -26,6 +26,7 @@ import ( "oras.land/oras-go/v2" "oras.land/oras-go/v2/registry" "oras.land/oras/cmd/oras/internal/argument" + "oras.land/oras/cmd/oras/internal/command" "oras.land/oras/cmd/oras/internal/display" "oras.land/oras/cmd/oras/internal/display/metadata" oerrors "oras.land/oras/cmd/oras/internal/errors" @@ -106,7 +107,7 @@ Example - Discover referrers of the manifest tagged 'v1' in an OCI image layout } func runDiscover(cmd *cobra.Command, opts *discoverOptions) error { - ctx, logger := opts.WithContext(cmd.Context()) + ctx, logger := command.GetLogger(cmd, &opts.Common) repo, err := opts.NewReadonlyTarget(ctx, opts.Common, logger) if err != nil { return err diff --git a/cmd/oras/root/login.go b/cmd/oras/root/login.go index be34b13ef..76f68182b 100644 --- a/cmd/oras/root/login.go +++ b/cmd/oras/root/login.go @@ -26,6 +26,7 @@ import ( "golang.org/x/term" "oras.land/oras-go/v2/registry/remote/credentials" "oras.land/oras/cmd/oras/internal/argument" + "oras.land/oras/cmd/oras/internal/command" oerrors "oras.land/oras/cmd/oras/internal/errors" "oras.land/oras/cmd/oras/internal/option" "oras.land/oras/internal/credential" @@ -77,7 +78,7 @@ Example - Log in with username and password in an interactive terminal and no TL } func runLogin(cmd *cobra.Command, opts loginOptions) (err error) { - ctx, logger := opts.WithContext(cmd.Context()) + ctx, logger := command.GetLogger(cmd, &opts.Common) outWriter := cmd.OutOrStdout() // prompt for credential diff --git a/cmd/oras/root/manifest/delete.go b/cmd/oras/root/manifest/delete.go index 22ea206cd..2d1aa7988 100644 --- a/cmd/oras/root/manifest/delete.go +++ b/cmd/oras/root/manifest/delete.go @@ -24,6 +24,7 @@ import ( "oras.land/oras-go/v2/errdef" "oras.land/oras-go/v2/registry/remote/auth" "oras.land/oras/cmd/oras/internal/argument" + "oras.land/oras/cmd/oras/internal/command" oerrors "oras.land/oras/cmd/oras/internal/errors" "oras.land/oras/cmd/oras/internal/option" "oras.land/oras/internal/registryutil" @@ -76,7 +77,7 @@ Example - Delete a manifest by digest 'sha256:99e4703fbf30916f549cd6bfa9cdbab614 } func deleteManifest(cmd *cobra.Command, opts *deleteOptions) error { - ctx, logger := opts.WithContext(cmd.Context()) + ctx, logger := command.GetLogger(cmd, &opts.Common) manifests, err := opts.NewManifestDeleter(opts.Common, logger) if err != nil { return err diff --git a/cmd/oras/root/manifest/fetch.go b/cmd/oras/root/manifest/fetch.go index 0e6edd104..499c14403 100644 --- a/cmd/oras/root/manifest/fetch.go +++ b/cmd/oras/root/manifest/fetch.go @@ -23,6 +23,7 @@ import ( "oras.land/oras-go/v2" "oras.land/oras-go/v2/registry/remote" "oras.land/oras/cmd/oras/internal/argument" + "oras.land/oras/cmd/oras/internal/command" "oras.land/oras/cmd/oras/internal/display" oerrors "oras.land/oras/cmd/oras/internal/errors" "oras.land/oras/cmd/oras/internal/option" @@ -105,7 +106,7 @@ Example - Fetch raw manifest from an OCI layout archive file 'layout.tar': } func fetchManifest(cmd *cobra.Command, opts *fetchOptions) (fetchErr error) { - ctx, logger := opts.WithContext(cmd.Context()) + ctx, logger := command.GetLogger(cmd, &opts.Common) target, err := opts.NewReadonlyTarget(ctx, opts.Common, logger) if err != nil { diff --git a/cmd/oras/root/manifest/fetch_config.go b/cmd/oras/root/manifest/fetch_config.go index 6e10acc27..e41776459 100644 --- a/cmd/oras/root/manifest/fetch_config.go +++ b/cmd/oras/root/manifest/fetch_config.go @@ -27,6 +27,7 @@ import ( "oras.land/oras-go/v2" "oras.land/oras-go/v2/content" "oras.land/oras/cmd/oras/internal/argument" + "oras.land/oras/cmd/oras/internal/command" oerrors "oras.land/oras/cmd/oras/internal/errors" "oras.land/oras/cmd/oras/internal/option" "oras.land/oras/internal/descriptor" @@ -88,7 +89,7 @@ Example - Fetch and print the prettified descriptor of the config: } func fetchConfig(cmd *cobra.Command, opts *fetchConfigOptions) (fetchErr error) { - ctx, logger := opts.WithContext(cmd.Context()) + ctx, logger := command.GetLogger(cmd, &opts.Common) repo, err := opts.NewReadonlyTarget(ctx, opts.Common, logger) if err != nil { diff --git a/cmd/oras/root/manifest/push.go b/cmd/oras/root/manifest/push.go index c59eeccfd..38cd7f526 100644 --- a/cmd/oras/root/manifest/push.go +++ b/cmd/oras/root/manifest/push.go @@ -29,6 +29,7 @@ import ( "oras.land/oras-go/v2/errdef" "oras.land/oras-go/v2/registry/remote" "oras.land/oras/cmd/oras/internal/argument" + "oras.land/oras/cmd/oras/internal/command" "oras.land/oras/cmd/oras/internal/display/status" oerrors "oras.land/oras/cmd/oras/internal/errors" "oras.land/oras/cmd/oras/internal/manifest" @@ -109,7 +110,7 @@ Example - Push a manifest to an OCI image layout folder 'layout-dir' and tag wit } func pushManifest(cmd *cobra.Command, opts pushOptions) error { - ctx, logger := opts.WithContext(cmd.Context()) + ctx, logger := command.GetLogger(cmd, &opts.Common) var target oras.Target var err error target, err = opts.NewTarget(opts.Common, logger) diff --git a/cmd/oras/root/pull.go b/cmd/oras/root/pull.go index 6a2e10b98..9dc62cbb1 100644 --- a/cmd/oras/root/pull.go +++ b/cmd/oras/root/pull.go @@ -28,6 +28,7 @@ import ( "oras.land/oras-go/v2/content" "oras.land/oras-go/v2/content/file" "oras.land/oras/cmd/oras/internal/argument" + "oras.land/oras/cmd/oras/internal/command" "oras.land/oras/cmd/oras/internal/display" "oras.land/oras/cmd/oras/internal/display/metadata" "oras.land/oras/cmd/oras/internal/display/status" @@ -108,7 +109,7 @@ Example - Pull artifact files from an OCI layout archive 'layout.tar': } func runPull(cmd *cobra.Command, opts *pullOptions) error { - ctx, logger := opts.WithContext(cmd.Context()) + ctx, logger := command.GetLogger(cmd, &opts.Common) // Copy Options copyOptions := oras.DefaultCopyOptions copyOptions.Concurrency = opts.concurrency diff --git a/cmd/oras/root/push.go b/cmd/oras/root/push.go index 2fca76bf6..cab1b3c74 100644 --- a/cmd/oras/root/push.go +++ b/cmd/oras/root/push.go @@ -27,6 +27,7 @@ import ( "oras.land/oras-go/v2/content/memory" "oras.land/oras-go/v2/registry/remote/auth" "oras.land/oras/cmd/oras/internal/argument" + "oras.land/oras/cmd/oras/internal/command" "oras.land/oras/cmd/oras/internal/display" "oras.land/oras/cmd/oras/internal/display/status" oerrors "oras.land/oras/cmd/oras/internal/errors" @@ -149,7 +150,7 @@ Example - Push file "hi.txt" into an OCI image layout folder 'layout-dir' with t } func runPush(cmd *cobra.Command, opts *pushOptions) error { - ctx, logger := opts.WithContext(cmd.Context()) + ctx, logger := command.GetLogger(cmd, &opts.Common) annotations, err := opts.LoadManifestAnnotations() if err != nil { return err diff --git a/cmd/oras/root/repo/ls.go b/cmd/oras/root/repo/ls.go index 6f8ec933d..d5215ab0f 100644 --- a/cmd/oras/root/repo/ls.go +++ b/cmd/oras/root/repo/ls.go @@ -22,6 +22,7 @@ import ( "github.com/spf13/cobra" "oras.land/oras/cmd/oras/internal/argument" + "oras.land/oras/cmd/oras/internal/command" oerrors "oras.land/oras/cmd/oras/internal/errors" "oras.land/oras/cmd/oras/internal/option" "oras.land/oras/internal/repository" @@ -71,7 +72,7 @@ Example - List the repositories under the registry that include values lexically } func listRepository(cmd *cobra.Command, opts *repositoryOptions) error { - ctx, logger := opts.WithContext(cmd.Context()) + ctx, logger := command.GetLogger(cmd, &opts.Common) reg, err := opts.Remote.NewRegistry(opts.hostname, opts.Common, logger) if err != nil { return err diff --git a/cmd/oras/root/repo/tags.go b/cmd/oras/root/repo/tags.go index ca7d96679..62b5714af 100644 --- a/cmd/oras/root/repo/tags.go +++ b/cmd/oras/root/repo/tags.go @@ -22,6 +22,7 @@ import ( "github.com/opencontainers/go-digest" "github.com/spf13/cobra" "oras.land/oras/cmd/oras/internal/argument" + "oras.land/oras/cmd/oras/internal/command" oerrors "oras.land/oras/cmd/oras/internal/errors" "oras.land/oras/cmd/oras/internal/option" ) @@ -79,7 +80,7 @@ Example - [Experimental] Show tags associated with a digest: } func showTags(cmd *cobra.Command, opts *showTagsOptions) error { - ctx, logger := opts.WithContext(cmd.Context()) + ctx, logger := command.GetLogger(cmd, &opts.Common) finder, err := opts.NewReadonlyTarget(ctx, opts.Common, logger) if err != nil { return err diff --git a/cmd/oras/root/resolve.go b/cmd/oras/root/resolve.go index b3ea41cc8..ed4f826b0 100644 --- a/cmd/oras/root/resolve.go +++ b/cmd/oras/root/resolve.go @@ -21,6 +21,7 @@ import ( "github.com/spf13/cobra" "oras.land/oras-go/v2" "oras.land/oras/cmd/oras/internal/argument" + "oras.land/oras/cmd/oras/internal/command" oerrors "oras.land/oras/cmd/oras/internal/errors" "oras.land/oras/cmd/oras/internal/option" ) @@ -61,7 +62,7 @@ Example - Resolve digest of the target artifact: } func runResolve(cmd *cobra.Command, opts *resolveOptions) error { - ctx, logger := opts.WithContext(cmd.Context()) + ctx, logger := command.GetLogger(cmd, &opts.Common) repo, err := opts.NewReadonlyTarget(ctx, opts.Common, logger) if err != nil { return err diff --git a/cmd/oras/root/tag.go b/cmd/oras/root/tag.go index 65eda710c..4eec4c0bd 100644 --- a/cmd/oras/root/tag.go +++ b/cmd/oras/root/tag.go @@ -23,6 +23,7 @@ import ( "oras.land/oras-go/v2" "oras.land/oras-go/v2/errdef" "oras.land/oras/cmd/oras/internal/argument" + "oras.land/oras/cmd/oras/internal/command" "oras.land/oras/cmd/oras/internal/display/status" oerrors "oras.land/oras/cmd/oras/internal/errors" "oras.land/oras/cmd/oras/internal/option" @@ -96,7 +97,7 @@ Example - Tag the manifest 'v1.0.1' to 'v1.0.2' in an OCI image layout folder 'l } func tagManifest(cmd *cobra.Command, opts *tagOptions) error { - ctx, logger := opts.WithContext(cmd.Context()) + ctx, logger := command.GetLogger(cmd, &opts.Common) target, err := opts.NewTarget(opts.Common, logger) if err != nil { return err