Skip to content

Commit

Permalink
refactor: move logger method to command (#1355)
Browse files Browse the repository at this point in the history
Signed-off-by: Terry Howe <[email protected]>
Co-authored-by: Billy Zha <[email protected]>
  • Loading branch information
Terry Howe and qweeah authored May 3, 2024
1 parent db85734 commit 979dbd7
Show file tree
Hide file tree
Showing 19 changed files with 66 additions and 25 deletions.
32 changes: 32 additions & 0 deletions cmd/oras/internal/command/logger.go
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 0 additions & 8 deletions cmd/oras/internal/option/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/root/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/root/blob/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/root/blob/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/root/blob/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/root/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/root/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/root/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/root/manifest/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/root/manifest/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/root/manifest/fetch_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/root/manifest/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/root/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/root/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/root/repo/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/root/repo/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/root/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/root/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 979dbd7

Please sign in to comment.