Move NewClient and AppContext to commands pkg

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2017-10-25 11:10:00 -04:00
parent 8509569329
commit 4743f1fc4c
33 changed files with 118 additions and 90 deletions

View File

@ -5,6 +5,7 @@ import (
"github.com/containerd/containerd/archive" "github.com/containerd/containerd/archive"
"github.com/containerd/containerd/archive/compression" "github.com/containerd/containerd/archive/compression"
"github.com/containerd/containerd/cmd/ctr/commands"
"github.com/containerd/containerd/log" "github.com/containerd/containerd/log"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -18,7 +19,7 @@ var applyCommand = cli.Command{
var ( var (
dir = context.Args().First() dir = context.Args().First()
) )
ctx, cancel := appContext(context) ctx, cancel := commands.AppContext(context)
defer cancel() defer cancel()
log.G(ctx).Info("applying layer from stdin") log.G(ctx).Info("applying layer from stdin")

View File

@ -5,6 +5,7 @@ import (
"github.com/containerd/console" "github.com/containerd/console"
"github.com/containerd/containerd" "github.com/containerd/containerd"
"github.com/containerd/containerd/cmd/ctr/commands"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -14,7 +15,7 @@ var taskAttachCommand = cli.Command{
Usage: "attach to the IO of a running container", Usage: "attach to the IO of a running container",
ArgsUsage: "CONTAINER", ArgsUsage: "CONTAINER",
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"github.com/containerd/containerd" "github.com/containerd/containerd"
"github.com/containerd/containerd/cmd/ctr/commands"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -23,7 +24,7 @@ var taskCheckpointCommand = cli.Command{
if id == "" { if id == "" {
return errors.New("container id must be provided") return errors.New("container id must be provided")
} }
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }

View File

@ -0,0 +1,40 @@
package commands
import (
gocontext "context"
"github.com/containerd/containerd"
"github.com/containerd/containerd/namespaces"
"github.com/urfave/cli"
)
// AppContext returns the context for a command. Should only be called once per
// command, near the start.
//
// This will ensure the namespace is picked up and set the timeout, if one is
// defined.
func AppContext(context *cli.Context) (gocontext.Context, gocontext.CancelFunc) {
var (
ctx = gocontext.Background()
timeout = context.GlobalDuration("timeout")
namespace = context.GlobalString("namespace")
cancel gocontext.CancelFunc
)
ctx = namespaces.WithNamespace(ctx, namespace)
if timeout > 0 {
ctx, cancel = gocontext.WithTimeout(ctx, timeout)
} else {
ctx, cancel = gocontext.WithCancel(ctx)
}
return ctx, cancel
}
// NewClient returns a new containerd client
func NewClient(context *cli.Context) (*containerd.Client, gocontext.Context, gocontext.CancelFunc, error) {
client, err := containerd.New(context.GlobalString("address"))
if err != nil {
return nil, nil, nil, err
}
ctx, cancel := AppContext(context)
return client, ctx, cancel, nil
}

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"github.com/containerd/containerd" "github.com/containerd/containerd"
"github.com/containerd/containerd/cmd/ctr/commands"
"github.com/containerd/containerd/log" "github.com/containerd/containerd/log"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -23,7 +24,7 @@ var containersDeleteCommand = cli.Command{
}, },
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
var exitErr error var exitErr error
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }

View File

@ -5,6 +5,7 @@ import (
"os" "os"
"text/tabwriter" "text/tabwriter"
"github.com/containerd/containerd/cmd/ctr/commands"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -29,7 +30,7 @@ var containersCommand = cli.Command{
filters = context.Args() filters = context.Args()
quiet = context.Bool("quiet") quiet = context.Bool("quiet")
) )
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }

View File

@ -46,7 +46,7 @@ var (
if err != nil { if err != nil {
return err return err
} }
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }
@ -90,7 +90,7 @@ var (
if ref == "" { if ref == "" {
return errors.New("must specify a transaction reference") return errors.New("must specify a transaction reference")
} }
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }
@ -124,7 +124,7 @@ var (
}, },
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
match := context.Args().First() match := context.Args().First()
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }
@ -164,7 +164,7 @@ var (
quiet = context.Bool("quiet") quiet = context.Bool("quiet")
args = []string(context.Args()) args = []string(context.Args())
) )
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }
@ -214,7 +214,7 @@ var (
Flags: []cli.Flag{}, Flags: []cli.Flag{},
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
object, labels := commands.ObjectWithLabelArgs(context) object, labels := commands.ObjectWithLabelArgs(context)
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }
@ -289,7 +289,7 @@ var (
if err != nil { if err != nil {
return err return err
} }
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }
@ -338,7 +338,7 @@ var (
args = []string(context.Args()) args = []string(context.Args())
exitError error exitError error
) )
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
eventsapi "github.com/containerd/containerd/api/services/events/v1" eventsapi "github.com/containerd/containerd/api/services/events/v1"
"github.com/containerd/containerd/cmd/ctr/commands"
"github.com/containerd/typeurl" "github.com/containerd/typeurl"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -13,7 +14,7 @@ var eventsCommand = cli.Command{
Name: "events", Name: "events",
Usage: "display containerd events", Usage: "display containerd events",
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }

View File

@ -5,6 +5,7 @@ import (
"github.com/containerd/console" "github.com/containerd/console"
"github.com/containerd/containerd" "github.com/containerd/containerd"
"github.com/containerd/containerd/cmd/ctr/commands"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -36,7 +37,7 @@ var taskExecCommand = cli.Command{
if id == "" { if id == "" {
return errors.New("container id must be provided") return errors.New("container id must be provided")
} }
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }

View File

@ -4,6 +4,7 @@ import (
"io" "io"
"os" "os"
"github.com/containerd/containerd/cmd/ctr/commands"
"github.com/containerd/containerd/reference" "github.com/containerd/containerd/reference"
digest "github.com/opencontainers/go-digest" digest "github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1" ocispec "github.com/opencontainers/image-spec/specs-go/v1"
@ -38,7 +39,7 @@ var imagesExportCommand = cli.Command{
local = context.Args().Get(1) local = context.Args().Get(1)
desc ocispec.Descriptor desc ocispec.Descriptor
) )
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }

View File

@ -51,7 +51,7 @@ Most of this is experimental and there are few leaps to make this work.`,
} }
func fetch(ref string, cliContext *cli.Context) (containerd.Image, error) { func fetch(ref string, cliContext *cli.Context) (containerd.Image, error) {
client, ctx, cancel, err := newClient(cliContext) client, ctx, cancel, err := commands.NewClient(cliContext)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -23,7 +23,7 @@ var fetchObjectCommand = cli.Command{
var ( var (
ref = context.Args().First() ref = context.Args().First()
) )
ctx, cancel := appContext(context) ctx, cancel := commands.AppContext(context)
defer cancel() defer cancel()
resolver, err := getResolver(ctx, context) resolver, err := getResolver(ctx, context)

View File

@ -47,7 +47,7 @@ var imagesListCommand = cli.Command{
filters = context.Args() filters = context.Args()
quiet = context.Bool("quiet") quiet = context.Bool("quiet")
) )
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }
@ -130,7 +130,7 @@ var imagesSetLabelsCommand = cli.Command{
replaceAll = context.Bool("replace-all") replaceAll = context.Bool("replace-all")
name, labels = commands.ObjectWithLabelArgs(context) name, labels = commands.ObjectWithLabelArgs(context)
) )
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }
@ -183,7 +183,7 @@ var imagesCheckCommand = cli.Command{
var ( var (
exitErr error exitErr error
) )
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }
@ -263,7 +263,7 @@ var imageRemoveCommand = cli.Command{
Description: `Remove one or more images by reference.`, Description: `Remove one or more images by reference.`,
Flags: []cli.Flag{}, Flags: []cli.Flag{},
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }

View File

@ -31,7 +31,7 @@ var imagesImportCommand = cli.Command{
refObject = context.String("ref-object") refObject = context.String("ref-object")
labels = commands.LabelArgs(context.StringSlice("label")) labels = commands.LabelArgs(context.StringSlice("label"))
) )
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }

View File

@ -15,7 +15,7 @@ var containerInfoCommand = cli.Command{
if id == "" { if id == "" {
return errors.New("container id must be provided") return errors.New("container id must be provided")
} }
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"github.com/containerd/containerd/cmd/ctr/commands"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -41,7 +42,7 @@ var taskKillCommand = cli.Command{
if pid > 0 && all { if pid > 0 && all {
return errors.New("enter a pid or all; not both") return errors.New("enter a pid or all; not both")
} }
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }

View File

@ -20,7 +20,7 @@ var containersSetLabelsCommand = cli.Command{
if containerID == "" { if containerID == "" {
return errors.New("please specify a container") return errors.New("please specify a container")
} }
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }

View File

@ -35,7 +35,7 @@ var namespacesCreateCommand = cli.Command{
if namespace == "" { if namespace == "" {
return errors.New("please specify a namespace") return errors.New("please specify a namespace")
} }
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }
@ -56,7 +56,7 @@ var namespacesSetLabelsCommand = cli.Command{
if namespace == "" { if namespace == "" {
return errors.New("please specify a namespace") return errors.New("please specify a namespace")
} }
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }
@ -85,7 +85,7 @@ var namespacesListCommand = cli.Command{
}, },
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
quiet := context.Bool("quiet") quiet := context.Bool("quiet")
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }
@ -131,7 +131,7 @@ var namespacesRemoveCommand = cli.Command{
Description: "Remove one or more namespaces. For now, the namespace must be empty.", Description: "Remove one or more namespaces. For now, the namespace must be empty.",
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
var exitErr error var exitErr error
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }

View File

@ -1,13 +1,16 @@
package main package main
import "github.com/urfave/cli" import (
"github.com/containerd/containerd/cmd/ctr/commands"
"github.com/urfave/cli"
)
var taskPauseCommand = cli.Command{ var taskPauseCommand = cli.Command{
Name: "pause", Name: "pause",
Usage: "pause an existing container", Usage: "pause an existing container",
ArgsUsage: "CONTAINER", ArgsUsage: "CONTAINER",
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }

View File

@ -9,6 +9,7 @@ import (
introspection "github.com/containerd/containerd/api/services/introspection/v1" introspection "github.com/containerd/containerd/api/services/introspection/v1"
"github.com/containerd/containerd/api/types" "github.com/containerd/containerd/api/types"
"github.com/containerd/containerd/cmd/ctr/commands"
"github.com/containerd/containerd/platforms" "github.com/containerd/containerd/platforms"
ocispec "github.com/opencontainers/image-spec/specs-go/v1" ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/urfave/cli" "github.com/urfave/cli"
@ -33,7 +34,7 @@ var pluginsCommand = cli.Command{
quiet = context.Bool("quiet") quiet = context.Bool("quiet")
detailed = context.Bool("detailed") detailed = context.Bool("detailed")
) )
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }

View File

@ -5,6 +5,7 @@ import (
"os" "os"
"text/tabwriter" "text/tabwriter"
"github.com/containerd/containerd/cmd/ctr/commands"
"github.com/containerd/containerd/windows/hcsshimtypes" "github.com/containerd/containerd/windows/hcsshimtypes"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/urfave/cli" "github.com/urfave/cli"
@ -19,7 +20,7 @@ var taskPsCommand = cli.Command{
if id == "" { if id == "" {
return errors.New("container id must be provided") return errors.New("container id must be provided")
} }
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }

View File

@ -27,7 +27,7 @@ command. As part of this process, we do the following:
ref = context.Args().First() ref = context.Args().First()
) )
ctx, cancel := appContext(context) ctx, cancel := commands.AppContext(context)
defer cancel() defer cancel()
img, err := fetch(ref, context) img, err := fetch(ref, context)

View File

@ -52,7 +52,7 @@ var pushCommand = cli.Command{
local = context.Args().Get(1) local = context.Args().Get(1)
desc ocispec.Descriptor desc ocispec.Descriptor
) )
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }

View File

@ -27,7 +27,7 @@ var pushObjectCommand = cli.Command{
if err != nil { if err != nil {
return err return err
} }
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }

View File

@ -1,13 +1,16 @@
package main package main
import "github.com/urfave/cli" import (
"github.com/containerd/containerd/cmd/ctr/commands"
"github.com/urfave/cli"
)
var taskResumeCommand = cli.Command{ var taskResumeCommand = cli.Command{
Name: "resume", Name: "resume",
Usage: "resume a paused container", Usage: "resume a paused container",
ArgsUsage: "CONTAINER", ArgsUsage: "CONTAINER",
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }

View File

@ -28,7 +28,7 @@ var rootfsUnpackCommand = cli.Command{
if err != nil { if err != nil {
return err return err
} }
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }

View File

@ -118,7 +118,7 @@ var runCommand = cli.Command{
if id == "" { if id == "" {
return errors.New("container id must be provided") return errors.New("container id must be provided")
} }
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }

View File

@ -38,7 +38,7 @@ var listSnapshotCommand = cli.Command{
Aliases: []string{"ls"}, Aliases: []string{"ls"},
Usage: "list snapshots", Usage: "list snapshots",
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }
@ -83,7 +83,7 @@ var usageSnapshotCommand = cli.Command{
return progress.Bytes(s).String() return progress.Bytes(s).String()
} }
} }
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }
@ -124,7 +124,7 @@ var removeSnapshotCommand = cli.Command{
ArgsUsage: "<key> [<key>, ...]", ArgsUsage: "<key> [<key>, ...]",
Usage: "remove snapshots", Usage: "remove snapshots",
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }
@ -160,7 +160,7 @@ var prepareSnapshotCommand = cli.Command{
key = context.Args().Get(0) key = context.Args().Get(0)
parent = context.Args().Get(1) parent = context.Args().Get(1)
) )
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }
@ -199,7 +199,7 @@ var viewSnapshotCommand = cli.Command{
key = context.Args().Get(0) key = context.Args().Get(0)
parent = context.Args().Get(1) parent = context.Args().Get(1)
) )
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }
@ -232,7 +232,7 @@ var mountSnapshotCommand = cli.Command{
target = context.Args().Get(0) target = context.Args().Get(0)
key = context.Args().Get(1) key = context.Args().Get(1)
) )
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }
@ -261,7 +261,7 @@ var commitSnapshotCommand = cli.Command{
key = context.Args().Get(0) key = context.Args().Get(0)
active = context.Args().Get(1) active = context.Args().Get(1)
) )
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }
@ -275,7 +275,7 @@ var treeSnapshotCommand = cli.Command{
Name: "tree", Name: "tree",
Usage: "display tree view of snapshot branches", Usage: "display tree view of snapshot branches",
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }
@ -315,7 +315,7 @@ var infoSnapshotCommand = cli.Command{
} }
key := context.Args().Get(0) key := context.Args().Get(0)
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }
@ -340,7 +340,7 @@ var labelSnapshotCommand = cli.Command{
Flags: []cli.Flag{}, Flags: []cli.Flag{},
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
key, labels := commands.ObjectWithLabelArgs(context) key, labels := commands.ObjectWithLabelArgs(context)
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }

View File

@ -2,6 +2,7 @@ package main
import ( import (
"github.com/containerd/console" "github.com/containerd/console"
"github.com/containerd/containerd/cmd/ctr/commands"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/urfave/cli" "github.com/urfave/cli"
@ -25,7 +26,7 @@ var taskStartCommand = cli.Command{
if id == "" { if id == "" {
return errors.New("container id must be provided") return errors.New("container id must be provided")
} }
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }

View File

@ -6,6 +6,7 @@ import (
"text/tabwriter" "text/tabwriter"
tasks "github.com/containerd/containerd/api/services/tasks/v1" tasks "github.com/containerd/containerd/api/services/tasks/v1"
"github.com/containerd/containerd/cmd/ctr/commands"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -32,7 +33,7 @@ var tasksCommand = cli.Command{
}, },
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
quiet := context.Bool("quiet") quiet := context.Bool("quiet")
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }

View File

@ -1,13 +1,16 @@
package main package main
import "github.com/urfave/cli" import (
"github.com/containerd/containerd/cmd/ctr/commands"
"github.com/urfave/cli"
)
var taskDeleteCommand = cli.Command{ var taskDeleteCommand = cli.Command{
Name: "delete", Name: "delete",
Usage: "delete a task", Usage: "delete a task",
ArgsUsage: "CONTAINER", ArgsUsage: "CONTAINER",
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }

View File

@ -16,8 +16,6 @@ import (
"time" "time"
"github.com/containerd/console" "github.com/containerd/console"
"github.com/containerd/containerd"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/remotes" "github.com/containerd/containerd/remotes"
"github.com/containerd/containerd/remotes/docker" "github.com/containerd/containerd/remotes/docker"
specs "github.com/opencontainers/runtime-spec/specs-go" specs "github.com/opencontainers/runtime-spec/specs-go"
@ -26,39 +24,6 @@ import (
"github.com/urfave/cli" "github.com/urfave/cli"
) )
// appContext returns the context for a command. Should only be called once per
// command, near the start.
//
// This will ensure the namespace is picked up and set the timeout, if one is
// defined.
func appContext(clicontext *cli.Context) (gocontext.Context, gocontext.CancelFunc) {
var (
ctx = gocontext.Background()
timeout = clicontext.GlobalDuration("timeout")
namespace = clicontext.GlobalString("namespace")
cancel gocontext.CancelFunc
)
ctx = namespaces.WithNamespace(ctx, namespace)
if timeout > 0 {
ctx, cancel = gocontext.WithTimeout(ctx, timeout)
} else {
ctx, cancel = gocontext.WithCancel(ctx)
}
return ctx, cancel
}
func newClient(context *cli.Context) (*containerd.Client, gocontext.Context, gocontext.CancelFunc, error) {
client, err := containerd.New(context.GlobalString("address"))
if err != nil {
return nil, nil, nil, err
}
ctx, cancel := appContext(context)
return client, ctx, cancel, nil
}
func passwordPrompt() (string, error) { func passwordPrompt() (string, error) {
c := console.Current() c := console.Current()
defer c.Reset() defer c.Reset()

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"os" "os"
"github.com/containerd/containerd/cmd/ctr/commands"
"github.com/containerd/containerd/version" "github.com/containerd/containerd/version"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -19,7 +20,7 @@ var versionCommand = cli.Command{
fmt.Printf(" Version: %s\n", version.Version) fmt.Printf(" Version: %s\n", version.Version)
fmt.Printf(" Revision: %s\n", version.Revision) fmt.Printf(" Revision: %s\n", version.Revision)
fmt.Println("") fmt.Println("")
client, ctx, cancel, err := newClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
return err return err
} }