| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package main |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
"strings" |
| 20 |
|
| 21 |
"github.com/opencontainers/image-tools/image" |
| 22 |
"github.com/urfave/cli" |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
var bundleTypes = []string{ |
| 27 |
image.TypeImageLayout, |
| 28 |
image.TypeImage, |
| 29 |
image.TypeImageZip, |
| 30 |
} |
| 31 |
|
| 32 |
type bundleCmd struct { |
| 33 |
typ string |
| 34 |
refs []string |
| 35 |
root string |
| 36 |
platform string |
| 37 |
} |
| 38 |
|
| 39 |
func createAction(context *cli.Context) error { |
| 40 |
if len(context.Args()) != 2 { |
| 41 |
return fmt.Errorf("both src and dest must be provided") |
| 42 |
} |
| 43 |
|
| 44 |
v := bundleCmd{ |
| 45 |
typ: context.String("type"), |
| 46 |
refs: context.StringSlice("ref"), |
| 47 |
root: context.String("rootfs"), |
| 48 |
platform: context.String("platform"), |
| 49 |
} |
| 50 |
|
| 51 |
if len(v.refs) == 0 { |
| 52 |
return fmt.Errorf("ref must be provided") |
| 53 |
} |
| 54 |
|
| 55 |
for index, ref := range v.refs { |
| 56 |
for i := index + 1; i < len(v.refs); i++ { |
| 57 |
if ref == v.refs[i] { |
| 58 |
fmt.Printf("WARNING: refs contains duplicate reference %q.\n", v.refs[i]) |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
if v.typ == "" { |
| 64 |
typ, err := image.Autodetect(context.Args()[0]) |
| 65 |
if err != nil { |
| 66 |
return fmt.Errorf("%q: autodetection failed: %v", context.Args()[0], err) |
| 67 |
} |
| 68 |
v.typ = typ |
| 69 |
} |
| 70 |
|
| 71 |
var err error |
| 72 |
switch v.typ { |
| 73 |
case image.TypeImageLayout: |
| 74 |
err = image.CreateRuntimeBundleLayout(context.Args()[0], context.Args()[1], v.root, v.platform, v.refs) |
| 75 |
|
| 76 |
case image.TypeImageZip: |
| 77 |
err = image.CreateRuntimeBundleZip(context.Args()[0], context.Args()[1], v.root, v.platform, v.refs) |
| 78 |
|
| 79 |
case image.TypeImage: |
| 80 |
err = image.CreateRuntimeBundleFile(context.Args()[0], context.Args()[1], v.root, v.platform, v.refs) |
| 81 |
|
| 82 |
default: |
| 83 |
err = fmt.Errorf("cannot create %q", v.typ) |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
return err |
| 88 |
} |
| 89 |
|
| 90 |
var createCommand = cli.Command{ |
| 91 |
Name: "create", |
| 92 |
Usage: "Create an OCI image runtime bundle", |
| 93 |
Action: createAction, |
| 94 |
Flags: []cli.Flag{ |
| 95 |
cli.StringFlag{ |
| 96 |
Name: "type", |
| 97 |
Usage: fmt.Sprintf( |
| 98 |
`Type of the file to unpack. If unset, oci-image-tool will try to auto-detect the type. One of "%s".`, |
| 99 |
strings.Join(bundleTypes, ","), |
| 100 |
), |
| 101 |
}, |
| 102 |
cli.StringSliceFlag{ |
| 103 |
Name: "ref", |
| 104 |
Usage: "A set of ref specify the search criteria for the validated reference, format is A=B. Only support 'name', 'platform.os' and 'digest' three cases.", |
| 105 |
}, |
| 106 |
cli.StringFlag{ |
| 107 |
Name: "rootfs", |
| 108 |
Value: "rootfs", |
| 109 |
Usage: "A directory representing the root filesystem of the container in the OCI runtime bundle. It is strongly recommended to keep the default value.", |
| 110 |
}, |
| 111 |
cli.StringFlag{ |
| 112 |
Name: "platform", |
| 113 |
Usage: "Specify the os and architecture of the manifest, format is OS:Architecture. Only applicable if reftype is index.", |
| 114 |
}, |
| 115 |
}, |
| 116 |
} |