Skip to content

Commit

Permalink
refactor: unify command contexts (oras-project#899)
Browse files Browse the repository at this point in the history
Signed-off-by: Billy Zha <[email protected]>
  • Loading branch information
qweeah authored and shizhMSFT committed Aug 3, 2023
1 parent eaf0da8 commit a335648
Show file tree
Hide file tree
Showing 19 changed files with 74 additions and 71 deletions.
9 changes: 9 additions & 0 deletions cmd/oras/internal/option/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ limitations under the License.
package option

import (
"context"

"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"oras.land/oras/internal/trace"
)

// Common option struct.
Expand All @@ -30,3 +34,8 @@ 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")
}

// 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)
}
7 changes: 3 additions & 4 deletions cmd/oras/root/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ 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 {
Expand Down Expand Up @@ -86,7 +85,7 @@ Example - Attach file to the manifest tagged 'v1' in an OCI layout folder 'layou
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return runAttach(opts)
return runAttach(cmd.Context(), opts)
},
}

Expand All @@ -98,8 +97,8 @@ Example - Attach file to the manifest tagged 'v1' in an OCI layout folder 'layou
return cmd
}

func runAttach(opts attachOptions) error {
ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose)
func runAttach(ctx context.Context, opts attachOptions) error {
ctx, _ = opts.WithContext(ctx)
annotations, err := opts.LoadManifestAnnotations()
if err != nil {
return err
Expand Down
8 changes: 4 additions & 4 deletions cmd/oras/root/blob/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ limitations under the License.
package blob

import (
"context"
"errors"
"fmt"
"os"

"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 {
Expand Down Expand Up @@ -62,16 +62,16 @@ Example - Delete a blob and print its descriptor:
},
RunE: func(cmd *cobra.Command, args []string) error {
opts.targetRef = args[0]
return deleteBlob(opts)
return deleteBlob(cmd.Context(), opts)
},
}

option.ApplyFlags(&opts, cmd.Flags())
return cmd
}

func deleteBlob(opts deleteBlobOptions) (err error) {
ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose)
func deleteBlob(ctx context.Context, opts deleteBlobOptions) (err error) {
ctx, _ = opts.WithContext(ctx)
repo, err := opts.NewRepository(opts.targetRef, opts.Common)
if err != nil {
return err
Expand Down
8 changes: 4 additions & 4 deletions cmd/oras/root/blob/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
package blob

import (
"context"
"errors"
"fmt"
"io"
Expand All @@ -28,7 +29,6 @@ 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 {
Expand Down Expand Up @@ -80,7 +80,7 @@ Example - Fetch and print a blob from OCI image layout archive file 'layout.tar'
},
Aliases: []string{"get"},
RunE: func(cmd *cobra.Command, args []string) error {
return fetchBlob(opts)
return fetchBlob(cmd.Context(), opts)
},
}

Expand All @@ -89,8 +89,8 @@ Example - Fetch and print a blob from OCI image layout archive file 'layout.tar'
return cmd
}

func fetchBlob(opts fetchBlobOptions) (fetchErr error) {
ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose)
func fetchBlob(ctx context.Context, opts fetchBlobOptions) (fetchErr error) {
ctx, _ = opts.WithContext(ctx)
var target oras.ReadOnlyTarget
target, err := opts.NewReadonlyTarget(ctx, opts.Common)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions cmd/oras/root/blob/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
package blob

import (
"context"
"errors"
"fmt"
"os"
Expand All @@ -25,7 +26,6 @@ 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 {
Expand Down Expand Up @@ -85,7 +85,7 @@ Example - Push blob 'hi.txt' into an OCI layout folder 'layout-dir':
return option.Parse(&opts)
},
RunE: func(cmd *cobra.Command, args []string) error {
return pushBlob(opts)
return pushBlob(cmd.Context(), opts)
},
}

Expand All @@ -95,8 +95,8 @@ Example - Push blob 'hi.txt' into an OCI layout folder 'layout-dir':
return cmd
}

func pushBlob(opts pushBlobOptions) (err error) {
ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose)
func pushBlob(ctx context.Context, opts pushBlobOptions) (err error) {
ctx, _ = opts.WithContext(ctx)

repo, err := opts.NewTarget(opts.Common)
if err != nil {
Expand Down
7 changes: 3 additions & 4 deletions cmd/oras/root/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ 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 {
Expand Down Expand Up @@ -86,7 +85,7 @@ Example - Copy an artifact with multiple tags with concurrency tuned:
return option.Parse(&opts)
},
RunE: func(cmd *cobra.Command, args []string) error {
return runCopy(opts)
return runCopy(cmd.Context(), opts)
},
}
cmd.Flags().BoolVarP(&opts.recursive, "recursive", "r", false, "[Preview] recursively copy the artifact and its referrer artifacts")
Expand All @@ -96,8 +95,8 @@ Example - Copy an artifact with multiple tags with concurrency tuned:
return cmd
}

func runCopy(opts copyOptions) error {
ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose)
func runCopy(ctx context.Context, opts copyOptions) error {
ctx, _ = opts.WithContext(ctx)

// Prepare source
src, err := opts.From.NewReadonlyTarget(ctx, opts.Common)
Expand Down
7 changes: 3 additions & 4 deletions cmd/oras/root/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"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 {
Expand Down Expand Up @@ -80,7 +79,7 @@ Example - Discover referrers of the manifest tagged 'v1' in an OCI layout folder
return option.Parse(&opts)
},
RunE: func(cmd *cobra.Command, args []string) error {
return runDiscover(opts)
return runDiscover(cmd.Context(), opts)
},
}

Expand All @@ -91,8 +90,8 @@ Example - Discover referrers of the manifest tagged 'v1' in an OCI layout folder
return cmd
}

func runDiscover(opts discoverOptions) error {
ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose)
func runDiscover(ctx context.Context, opts discoverOptions) error {
ctx, _ = opts.WithContext(ctx)
repo, err := opts.NewReadonlyTarget(ctx, opts.Common)
if err != nil {
return err
Expand Down
8 changes: 4 additions & 4 deletions cmd/oras/root/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package root

import (
"bufio"
"context"
"errors"
"fmt"
"os"
Expand All @@ -26,7 +27,6 @@ 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 {
Expand Down Expand Up @@ -66,15 +66,15 @@ Example - Log in with username and password in an interactive terminal and no TL
},
RunE: func(cmd *cobra.Command, args []string) error {
opts.Hostname = args[0]
return runLogin(opts)
return runLogin(cmd.Context(), opts)
},
}
option.ApplyFlags(&opts, cmd.Flags())
return cmd
}

func runLogin(opts loginOptions) (err error) {
ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose)
func runLogin(ctx context.Context, opts loginOptions) (err error) {
ctx, _ = opts.WithContext(ctx)

// prompt for credential
if opts.Password == "" {
Expand Down
6 changes: 4 additions & 2 deletions cmd/oras/root/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ limitations under the License.
package root

import (
"context"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"oras.land/oras/internal/credential"
Expand All @@ -41,7 +43,7 @@ Example - Logout:
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opts.hostname = args[0]
return runLogout(opts)
return runLogout(cmd.Context(), opts)
},
}

Expand All @@ -50,7 +52,7 @@ Example - Logout:
return cmd
}

func runLogout(opts logoutOptions) error {
func runLogout(ctx context.Context, opts logoutOptions) error {
if opts.debug {
logrus.SetLevel(logrus.DebugLevel)
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/oras/root/manifest/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
package manifest

import (
"context"
"errors"
"fmt"
"os"
Expand All @@ -24,7 +25,6 @@ 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 {
Expand Down Expand Up @@ -64,9 +64,9 @@ Example - Delete a manifest by digest 'sha256:99e4703fbf30916f549cd6bfa9cdbab614
}
return option.Parse(&opts)
},
RunE: func(_ *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, args []string) error {
opts.targetRef = args[0]
return deleteManifest(opts)
return deleteManifest(cmd.Context(), opts)
},
}

Expand All @@ -75,8 +75,8 @@ Example - Delete a manifest by digest 'sha256:99e4703fbf30916f549cd6bfa9cdbab614
return cmd
}

func deleteManifest(opts deleteOptions) error {
ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose)
func deleteManifest(ctx context.Context, opts deleteOptions) error {
ctx, _ = opts.WithContext(ctx)
repo, err := opts.NewRepository(opts.targetRef, opts.Common)
if err != nil {
return err
Expand Down
8 changes: 4 additions & 4 deletions cmd/oras/root/manifest/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
package manifest

import (
"context"
"encoding/json"
"errors"
"fmt"
Expand All @@ -26,7 +27,6 @@ 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 {
Expand Down Expand Up @@ -79,7 +79,7 @@ Example - Fetch raw manifest from an OCI layout archive file 'layout.tar':
},
Aliases: []string{"get"},
RunE: func(cmd *cobra.Command, args []string) error {
return fetchManifest(opts)
return fetchManifest(cmd.Context(), opts)
},
}

Expand All @@ -89,8 +89,8 @@ Example - Fetch raw manifest from an OCI layout archive file 'layout.tar':
return cmd
}

func fetchManifest(opts fetchOptions) (fetchErr error) {
ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose)
func fetchManifest(ctx context.Context, opts fetchOptions) (fetchErr error) {
ctx, _ = opts.WithContext(ctx)

target, err := opts.NewReadonlyTarget(ctx, opts.Common)
if err != nil {
Expand Down
7 changes: 3 additions & 4 deletions cmd/oras/root/manifest/fetch_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ 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 {
Expand Down Expand Up @@ -77,7 +76,7 @@ Example - Fetch and print the prettified descriptor of the config:
return option.Parse(&opts)
},
RunE: func(cmd *cobra.Command, args []string) error {
return fetchConfig(opts)
return fetchConfig(cmd.Context(), opts)
},
}

Expand All @@ -86,8 +85,8 @@ Example - Fetch and print the prettified descriptor of the config:
return cmd
}

func fetchConfig(opts fetchConfigOptions) (fetchErr error) {
ctx, _ := trace.NewLogger(opts.Debug, opts.Verbose)
func fetchConfig(ctx context.Context, opts fetchConfigOptions) (fetchErr error) {
ctx, _ = opts.WithContext(ctx)

repo, err := opts.NewReadonlyTarget(ctx, opts.Common)
if err != nil {
Expand Down
Loading

0 comments on commit a335648

Please sign in to comment.