Skip to content

Commit

Permalink
Add --tarball to crane digest (#896)
Browse files Browse the repository at this point in the history
If specified, tag can be omitted for single-image tarballs.
  • Loading branch information
jonjohnsonjr committed Apr 16, 2021
1 parent 7e30746 commit 549ee62
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 9 deletions.
45 changes: 40 additions & 5 deletions cmd/crane/cmd/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,51 @@ import (

// NewCmdDigest creates a new cobra.Command for the digest subcommand.
func NewCmdDigest(options *[]crane.Option) *cobra.Command {
return &cobra.Command{
var tarball string
cmd := &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...)
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if tarball == "" && len(args) == 0 {
cmd.Help()
log.Fatalf("image reference required without --tarball")
}

digest, err := getDigest(tarball, args, options)
if err != nil {
log.Fatalf("computing digest: %v", err)
log.Fatal(err)
}
fmt.Println(digest)
},
}

cmd.Flags().StringVar(&tarball, "tarball", "", "(Optional) path to tarball containing the image")

return cmd
}

func getDigest(tarball string, args []string, options *[]crane.Option) (string, error) {
if tarball != "" {
return getTarballDigest(tarball, args, options)
}

return crane.Digest(args[0], *options...)
}

func getTarballDigest(tarball string, args []string, options *[]crane.Option) (string, error) {
tag := ""
if len(args) > 0 {
tag = args[0]
}

img, err := crane.LoadTag(tarball, tag, *options...)
if err != nil {
return "", fmt.Errorf("loading image from %q: %v", tarball, err)
}
digest, err := img.Digest()
if err != nil {
return "", fmt.Errorf("computing digest: %v", err)
}
return digest.String(), nil
}
3 changes: 2 additions & 1 deletion cmd/crane/doc/crane_digest.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkg/crane/crane_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,8 @@ func TestBadInputs(t *testing.T) {
{"Catalog(invalid)", e(crane.Catalog(invalid))},
{"Catalog(404)", e(crane.Catalog(u.Host))},
{"PullLayer(invalid)", e(crane.PullLayer(invalid))},
{"LoadTag(_, invalid)", e(crane.LoadTag("", invalid))},
{"LoadTag(invalid, 404)", e(crane.LoadTag(invalid, valid404))},
} {
if tc.err == nil {
t.Errorf("%s: expected err, got nil", tc.desc)
Expand Down
20 changes: 17 additions & 3 deletions pkg/crane/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,23 @@ import (
)

// Load reads the tarball at path as a v1.Image.
func Load(path string) (v1.Image, error) {
// TODO: Allow tag?
return tarball.ImageFromPath(path, nil)
func Load(path string, opt ...Option) (v1.Image, error) {
return LoadTag(path, "")
}

// LoadTag reads a tag from the tarball at path as a v1.Image.
// If tag is "", will attempt to read the tarball as a single image.
func LoadTag(path, tag string, opt ...Option) (v1.Image, error) {
if tag == "" {
return tarball.ImageFromPath(path, nil)
}

o := makeOptions(opt...)
t, err := name.NewTag(tag, o.name...)
if err != nil {
return nil, fmt.Errorf("parsing tag %q: %v", tag, err)
}
return tarball.ImageFromPath(path, &t)
}

// Push pushes the v1.Image img to a registry as dst.
Expand Down

0 comments on commit 549ee62

Please sign in to comment.