Skip to content

Commit

Permalink
Use cobra's RunE whenever possible (#988)
Browse files Browse the repository at this point in the history
  • Loading branch information
imjasonh committed Apr 21, 2021
1 parent daa5add commit 7cfaa51
Show file tree
Hide file tree
Showing 21 changed files with 93 additions and 135 deletions.
14 changes: 7 additions & 7 deletions cmd/crane/cmd/append.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package cmd

import (
"log"
"fmt"

"github.com/google/go-containerregistry/pkg/crane"
"github.com/google/go-containerregistry/pkg/logs"
Expand All @@ -33,35 +33,35 @@ func NewCmdAppend(options *[]crane.Option) *cobra.Command {
Use: "append",
Short: "Append contents of a tarball to a remote image",
Args: cobra.NoArgs,
Run: func(_ *cobra.Command, args []string) {
RunE: func(_ *cobra.Command, args []string) error {
var base v1.Image
var err error

if baseRef == "" {
logs.Warn.Printf("base unspecified, using empty image")
base = empty.Image

} else {
base, err = crane.Pull(baseRef, *options...)
if err != nil {
log.Fatalf("pulling %s: %v", baseRef, err)
return fmt.Errorf("pulling %s: %v", baseRef, err)
}
}

img, err := crane.Append(base, newLayers...)
if err != nil {
log.Fatalf("appending %v: %v", newLayers, err)
return fmt.Errorf("appending %v: %v", newLayers, err)
}

if outFile != "" {
if err := crane.Save(img, newTag, outFile); err != nil {
log.Fatalf("writing output %q: %v", outFile, err)
return fmt.Errorf("writing output %q: %v", outFile, err)
}
} else {
if err := crane.Push(img, newTag, *options...); err != nil {
log.Fatalf("pushing image %s: %v", newTag, err)
return fmt.Errorf("pushing image %s: %v", newTag, err)
}
}
return nil
},
}
appendCmd.Flags().StringVarP(&baseRef, "base", "b", "", "Name of base image to append to")
Expand Down
22 changes: 9 additions & 13 deletions cmd/crane/cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,18 @@ func NewCmdAuthGet(argv ...string) *cobra.Command {
Short: "Implements a credential helper",
Example: eg,
Args: cobra.NoArgs,
Run: func(_ *cobra.Command, args []string) {
RunE: func(_ *cobra.Command, args []string) error {
b, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatal(err)
return err
}
reg, err := name.NewRegistry(strings.TrimSpace(string(b)))
if err != nil {
log.Fatal(err)
return err
}
authorizer, err := authn.DefaultKeychain.Resolve(reg)
if err != nil {
log.Fatal(err)
return err
}

// If we don't find any credentials, there's a magic error to return:
Expand All @@ -103,15 +103,13 @@ func NewCmdAuthGet(argv ...string) *cobra.Command {

auth, err := authorizer.Authorization()
if err != nil {
log.Fatal(err)
return err
}

// Convert back to a form that credential helpers can parse so that this
// can act as a meta credential helper.
creds := toCreds(auth)
if err := json.NewEncoder(os.Stdout).Encode(creds); err != nil {
log.Fatal(err)
}
return json.NewEncoder(os.Stdout).Encode(creds)
},
}
}
Expand All @@ -132,17 +130,15 @@ func NewCmdAuthLogin(argv ...string) *cobra.Command {
Short: "Log in to a registry",
Example: eg,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
reg, err := name.NewRegistry(args[0])
if err != nil {
log.Fatal(err)
return err
}

opts.serverAddress = reg.Name()

if err := login(opts); err != nil {
log.Fatal(err)
}
return login(opts)
},
}

Expand Down
11 changes: 6 additions & 5 deletions cmd/crane/cmd/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
package cmd

import (
"fmt"
"io"
"log"

"github.com/google/go-containerregistry/pkg/crane"
"github.com/spf13/cobra"
Expand All @@ -29,19 +29,20 @@ func NewCmdBlob(options *[]crane.Option) *cobra.Command {
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) {
RunE: func(cmd *cobra.Command, args []string) error {
src := args[0]
layer, err := crane.PullLayer(src, *options...)
if err != nil {
log.Fatalf("pulling layer %s: %v", src, err)
return fmt.Errorf("pulling layer %s: %v", src, err)
}
blob, err := layer.Compressed()
if err != nil {
log.Fatalf("fetching blob %s: %v", src, err)
return fmt.Errorf("fetching blob %s: %v", src, err)
}
if _, err := io.Copy(cmd.OutOrStdout(), blob); err != nil {
log.Fatalf("copying blob %s: %v", src, err)
return fmt.Errorf("copying blob %s: %v", src, err)
}
return nil
},
}
}
6 changes: 3 additions & 3 deletions cmd/crane/cmd/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package cmd

import (
"fmt"
"log"

"github.com/google/go-containerregistry/pkg/crane"
"github.com/spf13/cobra"
Expand All @@ -28,16 +27,17 @@ func NewCmdCatalog(options *[]crane.Option) *cobra.Command {
Use: "catalog",
Short: "List the repos in a registry",
Args: cobra.ExactArgs(1),
Run: func(_ *cobra.Command, args []string) {
RunE: func(_ *cobra.Command, args []string) error {
reg := args[0]
repos, err := crane.Catalog(reg, *options...)
if err != nil {
log.Fatalf("reading repos for %s: %v", reg, err)
return fmt.Errorf("reading repos for %s: %v", reg, err)
}

for _, repo := range repos {
fmt.Println(repo)
}
return nil
},
}
}
6 changes: 3 additions & 3 deletions cmd/crane/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package cmd

import (
"fmt"
"log"

"github.com/google/go-containerregistry/pkg/crane"
"github.com/spf13/cobra"
Expand All @@ -28,12 +27,13 @@ func NewCmdConfig(options *[]crane.Option) *cobra.Command {
Use: "config IMAGE",
Short: "Get the config of an image",
Args: cobra.ExactArgs(1),
Run: func(_ *cobra.Command, args []string) {
RunE: func(_ *cobra.Command, args []string) error {
cfg, err := crane.Config(args[0], *options...)
if err != nil {
log.Fatalf("fetching config: %v", err)
return fmt.Errorf("fetching config: %v", err)
}
fmt.Print(string(cfg))
return nil
},
}
}
8 changes: 2 additions & 6 deletions cmd/crane/cmd/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package cmd

import (
"log"

"github.com/google/go-containerregistry/pkg/crane"
"github.com/spf13/cobra"
)
Expand All @@ -28,11 +26,9 @@ func NewCmdCopy(options *[]crane.Option) *cobra.Command {
Aliases: []string{"cp"},
Short: "Efficiently copy a remote image from src to dst",
Args: cobra.ExactArgs(2),
Run: func(_ *cobra.Command, args []string) {
RunE: func(_ *cobra.Command, args []string) error {
src, dst := args[0], args[1]
if err := crane.Copy(src, dst, *options...); err != nil {
log.Fatal(err)
}
return crane.Copy(src, dst, *options...)
},
}
}
8 changes: 2 additions & 6 deletions cmd/crane/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package cmd

import (
"log"

"github.com/google/go-containerregistry/pkg/crane"
"github.com/spf13/cobra"
)
Expand All @@ -27,11 +25,9 @@ func NewCmdDelete(options *[]crane.Option) *cobra.Command {
Use: "delete IMAGE",
Short: "Delete an image reference from its registry",
Args: cobra.ExactArgs(1),
Run: func(_ *cobra.Command, args []string) {
RunE: func(_ *cobra.Command, args []string) error {
ref := args[0]
if err := crane.Delete(ref, *options...); err != nil {
log.Fatalf("deleting %s: %v", ref, err)
}
return crane.Delete(ref, *options...)
},
}
}
9 changes: 5 additions & 4 deletions cmd/crane/cmd/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
package cmd

import (
"errors"
"fmt"
"log"

"github.com/google/go-containerregistry/pkg/crane"
"github.com/spf13/cobra"
Expand All @@ -29,17 +29,18 @@ func NewCmdDigest(options *[]crane.Option) *cobra.Command {
Use: "digest IMAGE",
Short: "Get the digest of an image",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
if tarball == "" && len(args) == 0 {
cmd.Help()
log.Fatalf("image reference required without --tarball")
return errors.New("image reference required without --tarball")
}

digest, err := getDigest(tarball, args, options)
if err != nil {
log.Fatal(err)
return err
}
fmt.Println(digest)
return nil
},
}

Expand Down
12 changes: 5 additions & 7 deletions cmd/crane/cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package cmd

import (
"log"
"fmt"
"os"

"github.com/google/go-containerregistry/pkg/crane"
Expand All @@ -33,23 +33,21 @@ func NewCmdExport(options *[]crane.Option) *cobra.Command {
# Write tarball to file
crane export ubuntu ubuntu.tar`,
Args: cobra.ExactArgs(2),
Run: func(_ *cobra.Command, args []string) {
RunE: func(_ *cobra.Command, args []string) error {
src, dst := args[0], args[1]

f, err := openFile(dst)
if err != nil {
log.Fatalf("failed to open %s: %v", dst, err)
return fmt.Errorf("failed to open %s: %v", dst, err)
}
defer f.Close()

img, err := crane.Pull(src, *options...)
if err != nil {
log.Fatal(err)
return fmt.Errorf("pulling %s: %v", src, err)
}

if err := crane.Export(img, f); err != nil {
log.Fatalf("exporting %s: %v", src, err)
}
return crane.Export(img, f)
},
}
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/crane/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package cmd

import (
"fmt"
"log"

"github.com/google/go-containerregistry/pkg/crane"
"github.com/spf13/cobra"
Expand All @@ -28,16 +27,17 @@ func NewCmdList(options *[]crane.Option) *cobra.Command {
Use: "ls REPO",
Short: "List the tags in a repo",
Args: cobra.ExactArgs(1),
Run: func(_ *cobra.Command, args []string) {
RunE: func(_ *cobra.Command, args []string) error {
repo := args[0]
tags, err := crane.ListTags(repo, *options...)
if err != nil {
log.Fatalf("reading tags for %s: %v", repo, err)
return fmt.Errorf("reading tags for %s: %v", repo, err)
}

for _, tag := range tags {
fmt.Println(tag)
}
return nil
},
}
}
6 changes: 3 additions & 3 deletions cmd/crane/cmd/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package cmd

import (
"fmt"
"log"

"github.com/google/go-containerregistry/pkg/crane"
"github.com/spf13/cobra"
Expand All @@ -28,13 +27,14 @@ func NewCmdManifest(options *[]crane.Option) *cobra.Command {
Use: "manifest IMAGE",
Short: "Get the manifest of an image",
Args: cobra.ExactArgs(1),
Run: func(_ *cobra.Command, args []string) {
RunE: func(_ *cobra.Command, args []string) error {
src := args[0]
manifest, err := crane.Manifest(src, *options...)
if err != nil {
log.Fatalf("fetching manifest %s: %v", src, err)
return fmt.Errorf("fetching manifest %s: %v", src, err)
}
fmt.Print(string(manifest))
return nil
},
}
}
8 changes: 2 additions & 6 deletions cmd/crane/cmd/optimize.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package cmd

import (
"log"

"github.com/google/go-containerregistry/pkg/crane"
"github.com/spf13/cobra"
)
Expand All @@ -31,11 +29,9 @@ func NewCmdOptimize(options *[]crane.Option) *cobra.Command {
Aliases: []string{"opt"},
Short: "Optimize a remote container image from src to dst",
Args: cobra.ExactArgs(2),
Run: func(_ *cobra.Command, args []string) {
RunE: func(_ *cobra.Command, args []string) error {
src, dst := args[0], args[1]
if err := crane.Optimize(src, dst, files, *options...); err != nil {
log.Fatal(err)
}
return crane.Optimize(src, dst, files, *options...)
},
}

Expand Down
Loading

0 comments on commit 7cfaa51

Please sign in to comment.