Debian Codegraph @

cmd/oci-image-tool/unpack.go

raw symbols SCIP
1 // Copyright 2016 The Linux Foundation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
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 // supported unpack types
26 var unpackTypes = []string{
27 image.TypeImageLayout,
28 image.TypeImage,
29 image.TypeImageZip,
30 }
31
32 type unpackCmd struct {
33 typ string // the type to unpack, can be empty string
34 refs []string
35 platform string
36 }
37
38 func unpackAction(context *cli.Context) error {
39 if len(context.Args()) != 2 {
40 return fmt.Errorf("both src and dest must be provided")
41 }
42
43 v := unpackCmd{
44 typ: context.String("type"),
45 refs: context.StringSlice("ref"),
46 platform: context.String("platform"),
47 }
48
49 if len(v.refs) == 0 {
50 return fmt.Errorf("ref must be provided")
51 }
52
53 for index, ref := range v.refs {
54 for i := index + 1; i < len(v.refs); i++ {
55 if ref == v.refs[i] {
56 fmt.Printf("WARNING: refs contains duplicate reference %q.\n", v.refs[i])
57 }
58 }
59 }
60
61 if v.typ == "" {
62 typ, err := image.Autodetect(context.Args()[0])
63 if err != nil {
64 return fmt.Errorf("%q: autodetection failed: %v", context.Args()[0], err)
65 }
66 v.typ = typ
67 }
68
69 var err error
70 switch v.typ {
71 case image.TypeImageLayout:
72 err = image.UnpackLayout(context.Args()[0], context.Args()[1], v.platform, v.refs)
73
74 case image.TypeImageZip:
75 err = image.UnpackZip(context.Args()[0], context.Args()[1], v.platform, v.refs)
76
77 case image.TypeImage:
78 err = image.UnpackFile(context.Args()[0], context.Args()[1], v.platform, v.refs)
79
80 default:
81 err = fmt.Errorf("cannot unpack %q", v.typ)
82 }
83
84 return err
85 }
86
87 var unpackCommand = cli.Command{
88 Name: "unpack",
89 Usage: "Unpack an image or image source layout",
90 Action: unpackAction,
91 Flags: []cli.Flag{
92 cli.StringFlag{
93 Name: "type",
94 Usage: fmt.Sprintf(
95 `Type of the file to unpack. If unset, oci-image-tool will try to auto-detect the type. One of "%s".`,
96 strings.Join(unpackTypes, ","),
97 ),
98 },
99 cli.StringSliceFlag{
100 Name: "ref",
101 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.",
102 },
103 cli.StringFlag{
104 Name: "platform",
105 Usage: "Specify the os and architecture of the manifest, format is OS:Architecture. Only applicable if reftype is index.",
106 },
107 },
108 }