|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/containerd/containerd" |
| 10 | + "github.com/urfave/cli" |
| 11 | +) |
| 12 | + |
| 13 | +var ( |
| 14 | + // SnapshotterFlags are cli flags specifying snapshotter names |
| 15 | + SnapshotterFlags = []cli.Flag{ |
| 16 | + cli.StringFlag{ |
| 17 | + Name: "snapshotter", |
| 18 | + Usage: "snapshotter name. Empty value stands for the daemon default value.", |
| 19 | + Value: containerd.DefaultSnapshotter, |
| 20 | + }, |
| 21 | + } |
| 22 | + |
| 23 | + // LabelFlag is a cli flag specifying labels |
| 24 | + LabelFlag = cli.StringSliceFlag{ |
| 25 | + Name: "label", |
| 26 | + Usage: "labels to attach to the image", |
| 27 | + } |
| 28 | + |
| 29 | + // RegistryFlags are cli flags specifying registry options |
| 30 | + RegistryFlags = []cli.Flag{ |
| 31 | + cli.BoolFlag{ |
| 32 | + Name: "skip-verify,k", |
| 33 | + Usage: "skip SSL certificate validation", |
| 34 | + }, |
| 35 | + cli.BoolFlag{ |
| 36 | + Name: "plain-http", |
| 37 | + Usage: "allow connections using plain HTTP", |
| 38 | + }, |
| 39 | + cli.StringFlag{ |
| 40 | + Name: "user,u", |
| 41 | + Usage: "user[:password] Registry user and password", |
| 42 | + }, |
| 43 | + cli.StringFlag{ |
| 44 | + Name: "refresh", |
| 45 | + Usage: "refresh token for authorization server", |
| 46 | + }, |
| 47 | + } |
| 48 | +) |
| 49 | + |
| 50 | +// ObjectWithLabelArgs returns the first arg and a LabelArgs object |
| 51 | +func ObjectWithLabelArgs(clicontext *cli.Context) (string, map[string]string) { |
| 52 | + var ( |
| 53 | + first = clicontext.Args().First() |
| 54 | + labelStrings = clicontext.Args().Tail() |
| 55 | + ) |
| 56 | + |
| 57 | + return first, LabelArgs(labelStrings) |
| 58 | +} |
| 59 | + |
| 60 | +// LabelArgs returns a map of label key,value pairs |
| 61 | +func LabelArgs(labelStrings []string) map[string]string { |
| 62 | + labels := make(map[string]string, len(labelStrings)) |
| 63 | + for _, label := range labelStrings { |
| 64 | + parts := strings.SplitN(label, "=", 2) |
| 65 | + key := parts[0] |
| 66 | + value := "true" |
| 67 | + if len(parts) > 1 { |
| 68 | + value = parts[1] |
| 69 | + } |
| 70 | + |
| 71 | + labels[key] = value |
| 72 | + } |
| 73 | + |
| 74 | + return labels |
| 75 | +} |
| 76 | + |
| 77 | +// PrintAsJSON prints input in JSON format |
| 78 | +func PrintAsJSON(x interface{}) { |
| 79 | + b, err := json.MarshalIndent(x, "", " ") |
| 80 | + if err != nil { |
| 81 | + fmt.Fprintf(os.Stderr, "can't marshal %+v as a JSON string: %v\n", x, err) |
| 82 | + } |
| 83 | + fmt.Println(string(b)) |
| 84 | +} |
0 commit comments