Skip to content

Commit 8c77d9a

Browse files
authored
Merge pull request containerd#1683 from crosbymichael/ctr-moving
[ctr] move version and plugin commands
2 parents 083b10b + ae995bc commit 8c77d9a

34 files changed

+134
-109
lines changed

cmd/ctr/apply.go

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

66
"github.com/containerd/containerd/archive"
77
"github.com/containerd/containerd/archive/compression"
8+
"github.com/containerd/containerd/cmd/ctr/commands"
89
"github.com/containerd/containerd/log"
910
"github.com/urfave/cli"
1011
)
@@ -18,7 +19,7 @@ var applyCommand = cli.Command{
1819
var (
1920
dir = context.Args().First()
2021
)
21-
ctx, cancel := appContext(context)
22+
ctx, cancel := commands.AppContext(context)
2223
defer cancel()
2324

2425
log.G(ctx).Info("applying layer from stdin")

cmd/ctr/attach.go

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

66
"github.com/containerd/console"
77
"github.com/containerd/containerd"
8+
"github.com/containerd/containerd/cmd/ctr/commands"
89
"github.com/sirupsen/logrus"
910
"github.com/urfave/cli"
1011
)
@@ -14,7 +15,7 @@ var taskAttachCommand = cli.Command{
1415
Usage: "attach to the IO of a running container",
1516
ArgsUsage: "CONTAINER",
1617
Action: func(context *cli.Context) error {
17-
client, ctx, cancel, err := newClient(context)
18+
client, ctx, cancel, err := commands.NewClient(context)
1819
if err != nil {
1920
return err
2021
}

cmd/ctr/checkpoint.go

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

66
"github.com/containerd/containerd"
7+
"github.com/containerd/containerd/cmd/ctr/commands"
78
"github.com/pkg/errors"
89
"github.com/urfave/cli"
910
)
@@ -23,7 +24,7 @@ var taskCheckpointCommand = cli.Command{
2324
if id == "" {
2425
return errors.New("container id must be provided")
2526
}
26-
client, ctx, cancel, err := newClient(context)
27+
client, ctx, cancel, err := commands.NewClient(context)
2728
if err != nil {
2829
return err
2930
}

cmd/ctr/commands/client.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package commands
2+
3+
import (
4+
gocontext "context"
5+
6+
"github.com/containerd/containerd"
7+
"github.com/containerd/containerd/namespaces"
8+
"github.com/urfave/cli"
9+
)
10+
11+
// AppContext returns the context for a command. Should only be called once per
12+
// command, near the start.
13+
//
14+
// This will ensure the namespace is picked up and set the timeout, if one is
15+
// defined.
16+
func AppContext(context *cli.Context) (gocontext.Context, gocontext.CancelFunc) {
17+
var (
18+
ctx = gocontext.Background()
19+
timeout = context.GlobalDuration("timeout")
20+
namespace = context.GlobalString("namespace")
21+
cancel gocontext.CancelFunc
22+
)
23+
ctx = namespaces.WithNamespace(ctx, namespace)
24+
if timeout > 0 {
25+
ctx, cancel = gocontext.WithTimeout(ctx, timeout)
26+
} else {
27+
ctx, cancel = gocontext.WithCancel(ctx)
28+
}
29+
return ctx, cancel
30+
}
31+
32+
// NewClient returns a new containerd client
33+
func NewClient(context *cli.Context) (*containerd.Client, gocontext.Context, gocontext.CancelFunc, error) {
34+
client, err := containerd.New(context.GlobalString("address"))
35+
if err != nil {
36+
return nil, nil, nil, err
37+
}
38+
ctx, cancel := AppContext(context)
39+
return client, ctx, cancel, nil
40+
}
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package plugins
22

33
import (
44
"fmt"
@@ -9,22 +9,24 @@ import (
99

1010
introspection "github.com/containerd/containerd/api/services/introspection/v1"
1111
"github.com/containerd/containerd/api/types"
12+
"github.com/containerd/containerd/cmd/ctr/commands"
1213
"github.com/containerd/containerd/platforms"
13-
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
14+
"github.com/opencontainers/image-spec/specs-go/v1"
1415
"github.com/urfave/cli"
1516
"google.golang.org/grpc/codes"
1617
)
1718

18-
var pluginsCommand = cli.Command{
19+
// Command is a cli command that outputs plugin information
20+
var Command = cli.Command{
1921
Name: "plugins",
20-
Usage: "Provides information about containerd plugins",
22+
Usage: "provides information about containerd plugins",
2123
Flags: []cli.Flag{
2224
cli.BoolFlag{
23-
Name: "quiet, q",
25+
Name: "quiet,q",
2426
Usage: "print only the plugin ids",
2527
},
2628
cli.BoolFlag{
27-
Name: "detailed, d",
29+
Name: "detailed,d",
2830
Usage: "print detailed information about each plugin",
2931
},
3032
},
@@ -33,7 +35,7 @@ var pluginsCommand = cli.Command{
3335
quiet = context.Bool("quiet")
3436
detailed = context.Bool("detailed")
3537
)
36-
client, ctx, cancel, err := newClient(context)
38+
client, ctx, cancel, err := commands.NewClient(context)
3739
if err != nil {
3840
return err
3941
}
@@ -51,7 +53,6 @@ var pluginsCommand = cli.Command{
5153
}
5254
return nil
5355
}
54-
5556
w := tabwriter.NewWriter(os.Stdout, 4, 8, 4, ' ', 0)
5657
if detailed {
5758
first := true
@@ -104,7 +105,6 @@ var pluginsCommand = cli.Command{
104105
if len(plugin.Platforms) > 0 {
105106
platformColumn = prettyPlatforms(plugin.Platforms)
106107
}
107-
108108
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t\n",
109109
plugin.Type,
110110
plugin.ID,
@@ -121,7 +121,7 @@ var pluginsCommand = cli.Command{
121121
func prettyPlatforms(pspb []types.Platform) string {
122122
psm := map[string]struct{}{}
123123
for _, p := range pspb {
124-
psm[platforms.Format(ocispec.Platform{
124+
psm[platforms.Format(v1.Platform{
125125
OS: p.OS,
126126
Architecture: p.Architecture,
127127
Variant: p.Variant,
@@ -132,6 +132,5 @@ func prettyPlatforms(pspb []types.Platform) string {
132132
ps = append(ps, p)
133133
}
134134
sort.Stable(sort.StringSlice(ps))
135-
136135
return strings.Join(ps, ",")
137136
}
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
package main
1+
package version
22

33
import (
44
"fmt"
55
"os"
66

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

11-
var versionCommand = cli.Command{
12+
// Command is a cli ommand to output the client and containerd server version
13+
var Command = cli.Command{
1214
Name: "version",
13-
Usage: "print the version",
15+
Usage: "print the client and server versions",
1416
Action: func(context *cli.Context) error {
15-
if context.NArg() > 0 {
16-
return fmt.Errorf("no argument expected")
17-
}
1817
fmt.Println("Client:")
1918
fmt.Printf(" Version: %s\n", version.Version)
2019
fmt.Printf(" Revision: %s\n", version.Revision)
2120
fmt.Println("")
22-
client, ctx, cancel, err := newClient(context)
21+
client, ctx, cancel, err := commands.NewClient(context)
2322
if err != nil {
2423
return err
2524
}
@@ -28,7 +27,6 @@ var versionCommand = cli.Command{
2827
if err != nil {
2928
return err
3029
}
31-
3230
fmt.Println("Server:")
3331
fmt.Printf(" Version: %s\n", v.Version)
3432
fmt.Printf(" Revision: %s\n", v.Revision)

cmd/ctr/container_delete.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
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
)
@@ -23,7 +24,7 @@ var containersDeleteCommand = cli.Command{
2324
},
2425
Action: func(context *cli.Context) error {
2526
var exitErr error
26-
client, ctx, cancel, err := newClient(context)
27+
client, ctx, cancel, err := commands.NewClient(context)
2728
if err != nil {
2829
return err
2930
}

cmd/ctr/containers.go

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

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

@@ -29,7 +30,7 @@ var containersCommand = cli.Command{
2930
filters = context.Args()
3031
quiet = context.Bool("quiet")
3132
)
32-
client, ctx, cancel, err := newClient(context)
33+
client, ctx, cancel, err := commands.NewClient(context)
3334
if err != nil {
3435
return err
3536
}

cmd/ctr/content.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ var (
4646
if err != nil {
4747
return err
4848
}
49-
client, ctx, cancel, err := newClient(context)
49+
client, ctx, cancel, err := commands.NewClient(context)
5050
if err != nil {
5151
return err
5252
}
@@ -90,7 +90,7 @@ var (
9090
if ref == "" {
9191
return errors.New("must specify a transaction reference")
9292
}
93-
client, ctx, cancel, err := newClient(context)
93+
client, ctx, cancel, err := commands.NewClient(context)
9494
if err != nil {
9595
return err
9696
}
@@ -124,7 +124,7 @@ var (
124124
},
125125
Action: func(context *cli.Context) error {
126126
match := context.Args().First()
127-
client, ctx, cancel, err := newClient(context)
127+
client, ctx, cancel, err := commands.NewClient(context)
128128
if err != nil {
129129
return err
130130
}
@@ -164,7 +164,7 @@ var (
164164
quiet = context.Bool("quiet")
165165
args = []string(context.Args())
166166
)
167-
client, ctx, cancel, err := newClient(context)
167+
client, ctx, cancel, err := commands.NewClient(context)
168168
if err != nil {
169169
return err
170170
}
@@ -214,7 +214,7 @@ var (
214214
Flags: []cli.Flag{},
215215
Action: func(context *cli.Context) error {
216216
object, labels := commands.ObjectWithLabelArgs(context)
217-
client, ctx, cancel, err := newClient(context)
217+
client, ctx, cancel, err := commands.NewClient(context)
218218
if err != nil {
219219
return err
220220
}
@@ -289,7 +289,7 @@ var (
289289
if err != nil {
290290
return err
291291
}
292-
client, ctx, cancel, err := newClient(context)
292+
client, ctx, cancel, err := commands.NewClient(context)
293293
if err != nil {
294294
return err
295295
}
@@ -338,7 +338,7 @@ var (
338338
args = []string(context.Args())
339339
exitError error
340340
)
341-
client, ctx, cancel, err := newClient(context)
341+
client, ctx, cancel, err := commands.NewClient(context)
342342
if err != nil {
343343
return err
344344
}

cmd/ctr/events.go

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

77
eventsapi "github.com/containerd/containerd/api/services/events/v1"
8+
"github.com/containerd/containerd/cmd/ctr/commands"
89
"github.com/containerd/typeurl"
910
"github.com/urfave/cli"
1011
)
@@ -13,7 +14,7 @@ var eventsCommand = cli.Command{
1314
Name: "events",
1415
Usage: "display containerd events",
1516
Action: func(context *cli.Context) error {
16-
client, ctx, cancel, err := newClient(context)
17+
client, ctx, cancel, err := commands.NewClient(context)
1718
if err != nil {
1819
return err
1920
}

0 commit comments

Comments
 (0)