Skip to content

Commit

Permalink
Add --tarball to crane digest
Browse files Browse the repository at this point in the history
  • Loading branch information
jonjohnsonjr committed Jan 2, 2021
1 parent ec3c7a5 commit c838fd6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
27 changes: 22 additions & 5 deletions cmd/crane/cmd/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,33 @@ 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...)
if err != nil {
log.Fatalf("computing digest: %v", err)
if tarball != "" {
img, err := crane.LoadTag(tarball, args[0], *options...)
if err != nil {
log.Fatalf("loading image %q from %q: %v", tarball, args[0], err)
}
digest, err := img.Digest()
if err != nil {
log.Fatalf("computing digest: %v", err)
}
fmt.Println(digest.String())
} else {
digest, err := crane.Digest(args[0], *options...)
if err != nil {
log.Fatalf("computing digest: %v", err)
}
fmt.Println(digest)
}
fmt.Println(digest)
},
}

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

return cmd
}
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.

10 changes: 10 additions & 0 deletions pkg/crane/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ func Load(path string) (v1.Image, error) {
return tarball.ImageFromPath(path, nil)
}

// LoadTag reads a tag from the tarball at path as a v1.Image.
func LoadTag(path, tag string, opt ...Option) (v1.Image, error) {
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.
func Push(img v1.Image, dst string, opt ...Option) error {
o := makeOptions(opt...)
Expand Down

0 comments on commit c838fd6

Please sign in to comment.