Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crane export: Support reading an image from a directory. #1993

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions cmd/crane/cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ import (

// NewCmdExport creates a new cobra.Command for the export subcommand.
func NewCmdExport(options *[]crane.Option) *cobra.Command {
return &cobra.Command{

imageIsLocalFile := false

exportCmd := &cobra.Command{
Use: "export IMAGE|- TARBALL|-",
Short: "Export filesystem of a container image as a tarball",
Example: ` # Write tarball to stdout
Expand All @@ -53,7 +56,12 @@ func NewCmdExport(options *[]crane.Option) *cobra.Command {
defer f.Close()

var img v1.Image
if src == "-" {
if imageIsLocalFile {
img, err = loadImageTarballOrDir(src)
if err != nil {
return fmt.Errorf("reading tarball from %q: %w", src, err)
}
} else if src == "-" {
tmpfile, err := os.CreateTemp("", "crane")
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -90,6 +98,8 @@ func NewCmdExport(options *[]crane.Option) *cobra.Command {
return crane.Export(img, f)
},
}
exportCmd.Flags().BoolVar(&imageIsLocalFile, "local-image", false, "If false, loads the image from a remote source; otherwise, loads the image from a local tarball file or directory.")
return exportCmd
}

func openFile(s string) (*os.File, error) {
Expand Down
12 changes: 12 additions & 0 deletions cmd/crane/cmd/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,15 @@ func loadImage(path string, index bool) (partial.WithRawManifest, error) {

return nil, fmt.Errorf("layout contains non-image (mediaType: %q), consider --index", desc.MediaType)
}

func loadImageTarballOrDir(path string) (v1.Image, error) {
img, err := loadImage(path, false)
if err != nil {
return nil, err
}
if img, ok := img.(v1.Image); ok {
return img, nil
}

return nil, fmt.Errorf("directory doesn't specify an image")
}
Loading