
Instead of requiring callers to read the struct fields to check for an error, provide the exit results via a function instead which is more natural. Signed-off-by: Brian Goff <cpuguy83@gmail.com>
89 lines
1.6 KiB
Go
89 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"github.com/containerd/console"
|
|
"github.com/opencontainers/go-digest"
|
|
"github.com/pkg/errors"
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/urfave/cli"
|
|
)
|
|
|
|
var taskStartCommand = cli.Command{
|
|
Name: "start",
|
|
Usage: "start a container that have been created",
|
|
ArgsUsage: "CONTAINER",
|
|
Action: func(context *cli.Context) error {
|
|
var (
|
|
err error
|
|
|
|
ctx, cancel = appContext(context)
|
|
id = context.Args().Get(0)
|
|
)
|
|
|
|
defer cancel()
|
|
|
|
if id == "" {
|
|
return errors.New("container id must be provided")
|
|
}
|
|
client, err := newClient(context)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
container, err := client.LoadContainer(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
spec, err := container.Spec()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
tty := spec.Process.Terminal
|
|
|
|
task, err := newTask(ctx, container, digest.Digest(""), tty)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer task.Delete(ctx)
|
|
|
|
statusC, err := task.Wait(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var con console.Console
|
|
if tty {
|
|
con = console.Current()
|
|
defer con.Reset()
|
|
if err := con.SetRaw(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if err := task.Start(ctx); err != nil {
|
|
return err
|
|
}
|
|
if tty {
|
|
if err := handleConsoleResize(ctx, task, con); err != nil {
|
|
logrus.WithError(err).Error("console resize")
|
|
}
|
|
} else {
|
|
sigc := forwardAllSignals(ctx, task)
|
|
defer stopCatch(sigc)
|
|
}
|
|
|
|
status := <-statusC
|
|
code, _, err := status.Result()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if _, err := task.Delete(ctx); err != nil {
|
|
return err
|
|
}
|
|
if code != 0 {
|
|
return cli.NewExitError("", int(code))
|
|
}
|
|
return nil
|
|
},
|
|
}
|