X Tutup
Skip to content

Commit 80de000

Browse files
authored
Merge pull request containerd#1679 from jessvalarezo/ctr-refactor-first-pass
ctr: add commands package with shared utility functions
2 parents 04659d9 + a19a203 commit 80de000

File tree

19 files changed

+123
-128
lines changed

19 files changed

+123
-128
lines changed

cmd/ctr/commands/commands.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
}

cmd/ctr/content.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"text/tabwriter"
1111
"time"
1212

13+
"github.com/containerd/containerd/cmd/ctr/commands"
1314
"github.com/containerd/containerd/content"
1415
"github.com/containerd/containerd/errdefs"
1516
"github.com/containerd/containerd/log"
@@ -212,7 +213,7 @@ var (
212213
Description: `Labels blobs in the content store`,
213214
Flags: []cli.Flag{},
214215
Action: func(context *cli.Context) error {
215-
object, labels := objectWithLabelArgs(context)
216+
object, labels := commands.ObjectWithLabelArgs(context)
216217
client, ctx, cancel, err := newClient(context)
217218
if err != nil {
218219
return err

cmd/ctr/fetch.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"time"
1111

1212
"github.com/containerd/containerd"
13+
"github.com/containerd/containerd/cmd/ctr/commands"
1314
"github.com/containerd/containerd/content"
1415
"github.com/containerd/containerd/errdefs"
1516
"github.com/containerd/containerd/images"
@@ -39,7 +40,7 @@ not use this implementation as a guide. The end goal should be having metadata,
3940
content and snapshots ready for a direct use via the 'ctr run'.
4041
4142
Most of this is experimental and there are few leaps to make this work.`,
42-
Flags: append(registryFlags, labelFlag),
43+
Flags: append(commands.RegistryFlags, commands.LabelFlag),
4344
Action: func(clicontext *cli.Context) error {
4445
var (
4546
ref = clicontext.Args().First()
@@ -82,7 +83,7 @@ func fetch(ref string, cliContext *cli.Context) (containerd.Image, error) {
8283
})
8384

8485
log.G(pctx).WithField("image", ref).Debug("fetching")
85-
labels := labelArgs(cliContext.StringSlice("label"))
86+
labels := commands.LabelArgs(cliContext.StringSlice("label"))
8687
img, err := client.Pull(pctx, ref,
8788
containerd.WithPullLabels(labels),
8889
containerd.WithResolver(resolver),

cmd/ctr/fetchobject.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"io"
55
"os"
66

7+
"github.com/containerd/containerd/cmd/ctr/commands"
78
"github.com/containerd/containerd/log"
89
"github.com/urfave/cli"
910
)
@@ -17,7 +18,7 @@ var fetchObjectCommand = cli.Command{
1718
Usage: "retrieve objects from a remote",
1819
ArgsUsage: "[flags] <remote> <object> [<hint>, ...]",
1920
Description: `Fetch objects by identifier from a remote.`,
20-
Flags: registryFlags,
21+
Flags: commands.RegistryFlags,
2122
Action: func(context *cli.Context) error {
2223
var (
2324
ref = context.Args().First()

cmd/ctr/images.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88
"text/tabwriter"
99

10+
"github.com/containerd/containerd/cmd/ctr/commands"
1011
"github.com/containerd/containerd/errdefs"
1112
"github.com/containerd/containerd/images"
1213
"github.com/containerd/containerd/log"
@@ -127,7 +128,7 @@ var imagesSetLabelsCommand = cli.Command{
127128
Action: func(context *cli.Context) error {
128129
var (
129130
replaceAll = context.Bool("replace-all")
130-
name, labels = objectWithLabelArgs(context)
131+
name, labels = commands.ObjectWithLabelArgs(context)
131132
)
132133
client, ctx, cancel, err := newClient(context)
133134
if err != nil {

cmd/ctr/import.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77

88
"github.com/containerd/containerd"
9+
"github.com/containerd/containerd/cmd/ctr/commands"
910
"github.com/containerd/containerd/log"
1011
"github.com/urfave/cli"
1112
)
@@ -21,14 +22,14 @@ var imagesImportCommand = cli.Command{
2122
Value: "",
2223
Usage: "reference object e.g. tag@digest (default: use the object specified in ref)",
2324
},
24-
labelFlag,
25+
commands.LabelFlag,
2526
},
2627
Action: func(context *cli.Context) error {
2728
var (
2829
ref = context.Args().First()
2930
in = context.Args().Get(1)
3031
refObject = context.String("ref-object")
31-
labels = labelArgs(context.StringSlice("label"))
32+
labels = commands.LabelArgs(context.StringSlice("label"))
3233
)
3334
client, ctx, cancel, err := newClient(context)
3435
if err != nil {

cmd/ctr/info.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"github.com/containerd/containerd/cmd/ctr/commands"
45
"github.com/pkg/errors"
56
"github.com/urfave/cli"
67
)
@@ -27,7 +28,7 @@ var containerInfoCommand = cli.Command{
2728
if err != nil {
2829
return err
2930
}
30-
printAsJSON(info)
31+
commands.PrintAsJSON(info)
3132

3233
return nil
3334
},

cmd/ctr/labels.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"strings"
77

8+
"github.com/containerd/containerd/cmd/ctr/commands"
89
"github.com/urfave/cli"
910
)
1011

@@ -15,7 +16,7 @@ var containersSetLabelsCommand = cli.Command{
1516
Description: "Set and clear labels for a container.",
1617
Flags: []cli.Flag{},
1718
Action: func(context *cli.Context) error {
18-
containerID, labels := objectWithLabelArgs(context)
19+
containerID, labels := commands.ObjectWithLabelArgs(context)
1920
if containerID == "" {
2021
return errors.New("please specify a container")
2122
}

cmd/ctr/namespaces.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88
"text/tabwriter"
99

10+
"github.com/containerd/containerd/cmd/ctr/commands"
1011
"github.com/containerd/containerd/errdefs"
1112
"github.com/containerd/containerd/log"
1213
"github.com/pkg/errors"
@@ -30,7 +31,7 @@ var namespacesCreateCommand = cli.Command{
3031
ArgsUsage: "[flags] <name> [<key>=<value]",
3132
Description: "Create a new namespace. It must be unique.",
3233
Action: func(context *cli.Context) error {
33-
namespace, labels := objectWithLabelArgs(context)
34+
namespace, labels := commands.ObjectWithLabelArgs(context)
3435
if namespace == "" {
3536
return errors.New("please specify a namespace")
3637
}
@@ -51,7 +52,7 @@ var namespacesSetLabelsCommand = cli.Command{
5152
Description: "Set and clear labels for a namespace.",
5253
Flags: []cli.Flag{},
5354
Action: func(context *cli.Context) error {
54-
namespace, labels := objectWithLabelArgs(context)
55+
namespace, labels := commands.ObjectWithLabelArgs(context)
5556
if namespace == "" {
5657
return errors.New("please specify a namespace")
5758
}

cmd/ctr/pull.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55

6+
"github.com/containerd/containerd/cmd/ctr/commands"
67
"github.com/containerd/containerd/log"
78
"github.com/urfave/cli"
89
)
@@ -20,7 +21,7 @@ command. As part of this process, we do the following:
2021
2. Prepare the snapshot filesystem with the pulled resources.
2122
3. Register metadata for the image.
2223
`,
23-
Flags: append(registryFlags, append(snapshotterFlags, labelFlag)...),
24+
Flags: append(commands.RegistryFlags, append(commands.SnapshotterFlags, commands.LabelFlag)...),
2425
Action: func(context *cli.Context) error {
2526
var (
2627
ref = context.Args().First()

0 commit comments

Comments
 (0)
X Tutup