Skip to content

Commit

Permalink
Refactor [g]crane to make threading options easier (#845)
Browse files Browse the repository at this point in the history
* Add WithAuth options to pkg/crane

* Refactor gcrane

This will just overwrite certain portions of crane now so that flags and
options and stuff stay in sync.

* Refactor crane

* Use gcrane.Keychain
  • Loading branch information
jonjohnsonjr committed Nov 30, 2020
1 parent 59645bd commit 0349253
Show file tree
Hide file tree
Showing 25 changed files with 141 additions and 165 deletions.
8 changes: 3 additions & 5 deletions cmd/crane/cmd/append.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ import (
"github.com/spf13/cobra"
)

func init() { Root.AddCommand(NewCmdAppend()) }

// NewCmdAppend creates a new cobra.Command for the append subcommand.
func NewCmdAppend() *cobra.Command {
func NewCmdAppend(options *[]crane.Option) *cobra.Command {
var baseRef, newTag, outFile string
var newLayers []string

Expand All @@ -44,7 +42,7 @@ func NewCmdAppend() *cobra.Command {
base = empty.Image

} else {
base, err = crane.Pull(baseRef, options...)
base, err = crane.Pull(baseRef, *options...)
if err != nil {
log.Fatalf("pulling %s: %v", baseRef, err)
}
Expand All @@ -60,7 +58,7 @@ func NewCmdAppend() *cobra.Command {
log.Fatalf("writing output %q: %v", outFile, err)
}
} else {
if err := crane.Push(img, newTag, options...); err != nil {
if err := crane.Push(img, newTag, *options...); err != nil {
log.Fatalf("pushing image %s: %v", newTag, err)
}
}
Expand Down
2 changes: 0 additions & 2 deletions cmd/crane/cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ import (
"github.com/spf13/cobra"
)

func init() { Root.AddCommand(NewCmdAuth()) }

// NewCmdAuth creates a new cobra.Command for the auth subcommand.
func NewCmdAuth() *cobra.Command {
cmd := &cobra.Command{
Expand Down
6 changes: 2 additions & 4 deletions cmd/crane/cmd/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,16 @@ import (
"github.com/spf13/cobra"
)

func init() { Root.AddCommand(NewCmdBlob()) }

// NewCmdBlob creates a new cobra.Command for the blob subcommand.
func NewCmdBlob() *cobra.Command {
func NewCmdBlob(options *[]crane.Option) *cobra.Command {
return &cobra.Command{
Use: "blob BLOB",
Short: "Read a blob from the registry",
Example: "crane blob ubuntu@sha256:4c1d20cdee96111c8acf1858b62655a37ce81ae48648993542b7ac363ac5c0e5 > blob.tar.gz",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
src := args[0]
layer, err := crane.PullLayer(src, options...)
layer, err := crane.PullLayer(src, *options...)
if err != nil {
log.Fatalf("pulling layer %s: %v", src, err)
}
Expand Down
6 changes: 2 additions & 4 deletions cmd/crane/cmd/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,15 @@ import (
"github.com/spf13/cobra"
)

func init() { Root.AddCommand(NewCmdCatalog()) }

// NewCmdCatalog creates a new cobra.Command for the repos subcommand.
func NewCmdCatalog() *cobra.Command {
func NewCmdCatalog(options *[]crane.Option) *cobra.Command {
return &cobra.Command{
Use: "catalog",
Short: "List the repos in a registry",
Args: cobra.ExactArgs(1),
Run: func(_ *cobra.Command, args []string) {
reg := args[0]
repos, err := crane.Catalog(reg, options...)
repos, err := crane.Catalog(reg, *options...)
if err != nil {
log.Fatalf("reading repos for %s: %v", reg, err)
}
Expand Down
6 changes: 2 additions & 4 deletions cmd/crane/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@ import (
"github.com/spf13/cobra"
)

func init() { Root.AddCommand(NewCmdConfig()) }

// NewCmdConfig creates a new cobra.Command for the config subcommand.
func NewCmdConfig() *cobra.Command {
func NewCmdConfig(options *[]crane.Option) *cobra.Command {
return &cobra.Command{
Use: "config IMAGE",
Short: "Get the config of an image",
Args: cobra.ExactArgs(1),
Run: func(_ *cobra.Command, args []string) {
cfg, err := crane.Config(args[0], options...)
cfg, err := crane.Config(args[0], *options...)
if err != nil {
log.Fatalf("fetching config: %v", err)
}
Expand Down
6 changes: 2 additions & 4 deletions cmd/crane/cmd/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,16 @@ import (
"github.com/spf13/cobra"
)

func init() { Root.AddCommand(NewCmdCopy()) }

// NewCmdCopy creates a new cobra.Command for the copy subcommand.
func NewCmdCopy() *cobra.Command {
func NewCmdCopy(options *[]crane.Option) *cobra.Command {
return &cobra.Command{
Use: "copy SRC DST",
Aliases: []string{"cp"},
Short: "Efficiently copy a remote image from src to dst",
Args: cobra.ExactArgs(2),
Run: func(_ *cobra.Command, args []string) {
src, dst := args[0], args[1]
if err := crane.Copy(src, dst, options...); err != nil {
if err := crane.Copy(src, dst, *options...); err != nil {
log.Fatal(err)
}
},
Expand Down
6 changes: 2 additions & 4 deletions cmd/crane/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@ import (
"github.com/spf13/cobra"
)

func init() { Root.AddCommand(NewCmdDelete()) }

// NewCmdDelete creates a new cobra.Command for the delete subcommand.
func NewCmdDelete() *cobra.Command {
func NewCmdDelete(options *[]crane.Option) *cobra.Command {
return &cobra.Command{
Use: "delete IMAGE",
Short: "Delete an image reference from its registry",
Args: cobra.ExactArgs(1),
Run: func(_ *cobra.Command, args []string) {
ref := args[0]
if err := crane.Delete(ref, options...); err != nil {
if err := crane.Delete(ref, *options...); err != nil {
log.Fatalf("deleting %s: %v", ref, err)
}
},
Expand Down
6 changes: 2 additions & 4 deletions cmd/crane/cmd/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@ import (
"github.com/spf13/cobra"
)

func init() { Root.AddCommand(NewCmdDigest()) }

// NewCmdDigest creates a new cobra.Command for the digest subcommand.
func NewCmdDigest() *cobra.Command {
func NewCmdDigest(options *[]crane.Option) *cobra.Command {
return &cobra.Command{
Use: "digest IMAGE",
Short: "Get the digest of an image",
Args: cobra.ExactArgs(1),
Run: func(_ *cobra.Command, args []string) {
digest, err := crane.Digest(args[0], options...)
digest, err := crane.Digest(args[0], *options...)
if err != nil {
log.Fatalf("computing digest: %v", err)
}
Expand Down
6 changes: 2 additions & 4 deletions cmd/crane/cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ import (
"github.com/spf13/cobra"
)

func init() { Root.AddCommand(NewCmdExport()) }

// NewCmdExport creates a new cobra.Command for the export subcommand.
func NewCmdExport() *cobra.Command {
func NewCmdExport(options *[]crane.Option) *cobra.Command {
return &cobra.Command{
Use: "export IMAGE TARBALL",
Short: "Export contents of a remote image as a tarball",
Expand All @@ -44,7 +42,7 @@ func NewCmdExport() *cobra.Command {
}
defer f.Close()

img, err := crane.Pull(src, options...)
img, err := crane.Pull(src, *options...)
if err != nil {
log.Fatal(err)
}
Expand Down
6 changes: 2 additions & 4 deletions cmd/crane/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,15 @@ import (
"github.com/spf13/cobra"
)

func init() { Root.AddCommand(NewCmdList()) }

// NewCmdList creates a new cobra.Command for the ls subcommand.
func NewCmdList() *cobra.Command {
func NewCmdList(options *[]crane.Option) *cobra.Command {
return &cobra.Command{
Use: "ls REPO",
Short: "List the tags in a repo",
Args: cobra.ExactArgs(1),
Run: func(_ *cobra.Command, args []string) {
repo := args[0]
tags, err := crane.ListTags(repo, options...)
tags, err := crane.ListTags(repo, *options...)
if err != nil {
log.Fatalf("reading tags for %s: %v", repo, err)
}
Expand Down
6 changes: 2 additions & 4 deletions cmd/crane/cmd/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,15 @@ import (
"github.com/spf13/cobra"
)

func init() { Root.AddCommand(NewCmdManifest()) }

// NewCmdManifest creates a new cobra.Command for the manifest subcommand.
func NewCmdManifest() *cobra.Command {
func NewCmdManifest(options *[]crane.Option) *cobra.Command {
return &cobra.Command{
Use: "manifest IMAGE",
Short: "Get the manifest of an image",
Args: cobra.ExactArgs(1),
Run: func(_ *cobra.Command, args []string) {
src := args[0]
manifest, err := crane.Manifest(src, options...)
manifest, err := crane.Manifest(src, *options...)
if err != nil {
log.Fatalf("fetching manifest %s: %v", src, err)
}
Expand Down
6 changes: 2 additions & 4 deletions cmd/crane/cmd/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ import (
"github.com/spf13/cobra"
)

func init() { Root.AddCommand(NewCmdPull()) }

// NewCmdPull creates a new cobra.Command for the pull subcommand.
func NewCmdPull() *cobra.Command {
func NewCmdPull(options *[]crane.Option) *cobra.Command {
var cachePath, format string

cmd := &cobra.Command{
Expand All @@ -35,7 +33,7 @@ func NewCmdPull() *cobra.Command {
Args: cobra.ExactArgs(2),
Run: func(_ *cobra.Command, args []string) {
src, path := args[0], args[1]
img, err := crane.Pull(src, options...)
img, err := crane.Pull(src, *options...)
if err != nil {
log.Fatal(err)
}
Expand Down
6 changes: 2 additions & 4 deletions cmd/crane/cmd/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ import (
"github.com/spf13/cobra"
)

func init() { Root.AddCommand(NewCmdPush()) }

// NewCmdPush creates a new cobra.Command for the push subcommand.
func NewCmdPush() *cobra.Command {
func NewCmdPush(options *[]crane.Option) *cobra.Command {
return &cobra.Command{
Use: "push TARBALL IMAGE",
Short: "Push image contents as a tarball to a remote registry",
Expand All @@ -36,7 +34,7 @@ func NewCmdPush() *cobra.Command {
log.Fatalf("loading %s as tarball: %v", path, err)
}

if err := crane.Push(img, tag, options...); err != nil {
if err := crane.Push(img, tag, *options...); err != nil {
log.Fatalf("pushing %s: %v", tag, err)
}
},
Expand Down
12 changes: 5 additions & 7 deletions cmd/crane/cmd/rebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,26 @@ import (
"github.com/spf13/cobra"
)

func init() { Root.AddCommand(NewCmdRebase()) }

// NewCmdRebase creates a new cobra.Command for the rebase subcommand.
func NewCmdRebase() *cobra.Command {
func NewCmdRebase(options *[]crane.Option) *cobra.Command {
var orig, oldBase, newBase, rebased string

rebaseCmd := &cobra.Command{
Use: "rebase",
Short: "Rebase an image onto a new base image",
Args: cobra.NoArgs,
Run: func(*cobra.Command, []string) {
origImg, err := crane.Pull(orig, options...)
origImg, err := crane.Pull(orig, *options...)
if err != nil {
log.Fatalf("pulling %s: %v", orig, err)
}

oldBaseImg, err := crane.Pull(oldBase, options...)
oldBaseImg, err := crane.Pull(oldBase, *options...)
if err != nil {
log.Fatalf("pulling %s: %v", oldBase, err)
}

newBaseImg, err := crane.Pull(newBase, options...)
newBaseImg, err := crane.Pull(newBase, *options...)
if err != nil {
log.Fatalf("pulling %s: %v", newBase, err)
}
Expand All @@ -54,7 +52,7 @@ func NewCmdRebase() *cobra.Command {
log.Fatalf("rebasing: %v", err)
}

if err := crane.Push(img, rebased, options...); err != nil {
if err := crane.Push(img, rebased, *options...); err != nil {
log.Fatalf("pushing %s: %v", rebased, err)
}

Expand Down
60 changes: 44 additions & 16 deletions cmd/crane/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,27 @@ import (
"github.com/spf13/cobra"
)

func init() {
Root.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Enable debug logs")
Root.PersistentFlags().BoolVar(&insecure, "insecure", false, "Allow image references to be fetched without TLS")
Root.PersistentFlags().Var(platform, "platform", "Specifies the platform in the form os/arch[/variant] (e.g. linux/amd64).")
}
const (
use = "crane"
short = "Crane is a tool for managing container images"
)

var (
verbose = false
insecure = false
platform = &platformValue{}
var Root = New(use, short, []crane.Option{})

// Crane options for this invocation.
options = []crane.Option{}
// New returns a top-level command for crane. This is mostly exposed
// to share code with gcrane.
func New(use, short string, options []crane.Option) *cobra.Command {
verbose := false
insecure := false
platform := &platformValue{}

// Root is the top-level cobra.Command for crane.
Root = &cobra.Command{
Use: "crane",
Short: "Crane is a tool for managing container images",
root := &cobra.Command{
Use: use,
Short: short,
Run: func(cmd *cobra.Command, _ []string) { cmd.Usage() },
DisableAutoGenTag: true,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// TODO(jonjohnsonjr): crane.Verbose option?
if verbose {
logs.Debug.SetOutput(os.Stderr)
}
Expand All @@ -64,7 +64,35 @@ var (
}
},
}
)

commands := []*cobra.Command{
NewCmdAppend(&options),
NewCmdBlob(&options),
NewCmdAuth(),
NewCmdCatalog(&options),
NewCmdConfig(&options),
NewCmdCopy(&options),
NewCmdDelete(&options),
NewCmdDigest(&options),
NewCmdExport(&options),
NewCmdList(&options),
NewCmdManifest(&options),
NewCmdPull(&options),
NewCmdPush(&options),
NewCmdRebase(&options),
NewCmdTag(&options),
NewCmdValidate(&options),
NewCmdVersion(),
}

root.AddCommand(commands...)

root.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Enable debug logs")
root.PersistentFlags().BoolVar(&insecure, "insecure", false, "Allow image references to be fetched without TLS")
root.PersistentFlags().Var(platform, "platform", "Specifies the platform in the form os/arch[/variant] (e.g. linux/amd64).")

return root
}

// headerTransport sets headers on outgoing requests.
type headerTransport struct {
Expand Down
Loading

0 comments on commit 0349253

Please sign in to comment.