Skip to content

Commit

Permalink
Pass cobra.Command to all functions
Browse files Browse the repository at this point in the history
Signed-off-by: Terry Howe <[email protected]>
  • Loading branch information
Terry Howe committed Mar 12, 2024
1 parent 40dafa8 commit fe4654b
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 23 deletions.
6 changes: 3 additions & 3 deletions cmd/oras/root/blob/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ Example - Push blob 'hi.txt' into an OCI image layout folder 'layout-dir':
return option.Parse(&opts)
},
RunE: func(cmd *cobra.Command, args []string) error {
return pushBlob(cmd.Context(), &opts)
return pushBlob(cmd, &opts)
},
}

Expand All @@ -100,8 +100,8 @@ Example - Push blob 'hi.txt' into an OCI image layout folder 'layout-dir':
return oerrors.Command(cmd, &opts.Target)
}

func pushBlob(ctx context.Context, opts *pushBlobOptions) (err error) {
ctx, logger := opts.WithContext(ctx)
func pushBlob(cmd *cobra.Command, opts *pushBlobOptions) (err error) {
ctx, logger := opts.WithContext(cmd.Context())

target, err := opts.NewTarget(opts.Common, logger)
if err != nil {
Expand Down
7 changes: 3 additions & 4 deletions cmd/oras/root/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ limitations under the License.
package root

import (
"context"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -69,15 +68,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(cmd.Context(), opts)
return runLogin(cmd, opts)
},
}
option.ApplyFlags(&opts, cmd.Flags())
return oerrors.Command(cmd, &opts.Remote)
}

func runLogin(ctx context.Context, opts loginOptions) (err error) {
ctx, logger := opts.WithContext(ctx)
func runLogin(cmd *cobra.Command, opts loginOptions) (err error) {
ctx, logger := opts.WithContext(cmd.Context())

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

import (
"context"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"oras.land/oras-go/v2/registry/remote/credentials"
Expand Down Expand Up @@ -46,7 +44,7 @@ Example - Logout:
Args: oerrors.CheckArgs(argument.Exactly(1), "the registry you want to log out"),
RunE: func(cmd *cobra.Command, args []string) error {
opts.hostname = args[0]
return runLogout(cmd.Context(), opts)
return runLogout(cmd, opts)

Check warning on line 47 in cmd/oras/root/logout.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/root/logout.go#L47

Added line #L47 was not covered by tests
},
}

Expand All @@ -55,7 +53,8 @@ Example - Logout:
return cmd
}

func runLogout(ctx context.Context, opts logoutOptions) error {
func runLogout(cmd *cobra.Command, opts logoutOptions) error {
ctx := cmd.Context()

Check warning on line 57 in cmd/oras/root/logout.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/root/logout.go#L56-L57

Added lines #L56 - L57 were not covered by tests
if opts.debug {
logrus.SetLevel(logrus.DebugLevel)
}
Expand Down
7 changes: 3 additions & 4 deletions cmd/oras/root/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ limitations under the License.
package root

import (
"context"
"errors"
"strings"

Expand Down Expand Up @@ -122,7 +121,7 @@ Example - Push file "hi.txt" into an OCI image layout folder 'layout-dir' with t
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return runPush(cmd.Context(), &opts)
return runPush(cmd, &opts)
},
}
cmd.Flags().StringVarP(&opts.manifestConfigRef, "config", "", "", "`path` of image config file")
Expand All @@ -133,8 +132,8 @@ Example - Push file "hi.txt" into an OCI image layout folder 'layout-dir' with t
return oerrors.Command(cmd, &opts.Target)
}

func runPush(ctx context.Context, opts *pushOptions) error {
ctx, logger := opts.WithContext(ctx)
func runPush(cmd *cobra.Command, opts *pushOptions) error {
ctx, logger := opts.WithContext(cmd.Context())
annotations, err := opts.LoadManifestAnnotations()
if err != nil {
return err
Expand Down
7 changes: 3 additions & 4 deletions cmd/oras/root/repo/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ limitations under the License.
package repo

import (
"context"
"errors"
"fmt"
"strings"
Expand Down Expand Up @@ -62,7 +61,7 @@ Example - List the repositories under the registry that include values lexically
if opts.hostname, opts.namespace, err = repository.ParseRepoPath(args[0]); err != nil {
return fmt.Errorf("could not parse repository path: %w", err)
}
return listRepository(cmd.Context(), &opts)
return listRepository(cmd, &opts)
},
}

Expand All @@ -71,8 +70,8 @@ Example - List the repositories under the registry that include values lexically
return oerrors.Command(cmd, &opts.Remote)
}

func listRepository(ctx context.Context, opts *repositoryOptions) error {
ctx, logger := opts.WithContext(ctx)
func listRepository(cmd *cobra.Command, opts *repositoryOptions) error {
ctx, logger := opts.WithContext(cmd.Context())
reg, err := opts.Remote.NewRegistry(opts.hostname, opts.Common, logger)
if err != nil {
return err
Expand Down
7 changes: 3 additions & 4 deletions cmd/oras/root/repo/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ limitations under the License.
package repo

import (
"context"
"fmt"
"strings"

Expand Down Expand Up @@ -70,7 +69,7 @@ Example - [Experimental] Show tags associated with a digest:
return option.Parse(&opts)
},
RunE: func(cmd *cobra.Command, args []string) error {
return showTags(cmd.Context(), &opts)
return showTags(cmd, &opts)
},
}
cmd.Flags().StringVar(&opts.last, "last", "", "start after the tag specified by `last`")
Expand All @@ -79,8 +78,8 @@ Example - [Experimental] Show tags associated with a digest:
return oerrors.Command(cmd, &opts.Target)
}

func showTags(ctx context.Context, opts *showTagsOptions) error {
ctx, logger := opts.WithContext(ctx)
func showTags(cmd *cobra.Command, opts *showTagsOptions) error {
ctx, logger := opts.WithContext(cmd.Context())
finder, err := opts.NewReadonlyTarget(ctx, opts.Common, logger)
if err != nil {
return err
Expand Down

0 comments on commit fe4654b

Please sign in to comment.