aboutsummaryrefslogtreecommitdiff
path: root/cmd/podman/exec.go
diff options
context:
space:
mode:
authorbaude <bbaude@redhat.com>2019-01-31 13:20:04 -0600
committerbaude <bbaude@redhat.com>2019-02-08 10:26:43 -0600
commit25a3923b61a5ca014318e6d957f68abd03947297 (patch)
tree2ccb4a0bd9bda70c1c258dcb1b8aca8961d9ad30 /cmd/podman/exec.go
parent962850c6e0dfcee926af31fc0ad24f1f6c26f8ac (diff)
downloadpodman-25a3923b61a5ca014318e6d957f68abd03947297.tar.gz
podman-25a3923b61a5ca014318e6d957f68abd03947297.tar.bz2
podman-25a3923b61a5ca014318e6d957f68abd03947297.zip
Migrate to cobra CLI
We intend to migrate to the cobra cli from urfave/cli because the project is more well maintained. There are also some technical reasons as well which extend into our remote client work. Signed-off-by: baude <bbaude@redhat.com>
Diffstat (limited to 'cmd/podman/exec.go')
-rw-r--r--cmd/podman/exec.go84
1 files changed, 39 insertions, 45 deletions
diff --git a/cmd/podman/exec.go b/cmd/podman/exec.go
index 073e72e64..b4f66bc03 100644
--- a/cmd/podman/exec.go
+++ b/cmd/podman/exec.go
@@ -2,82 +2,76 @@ package main
import (
"fmt"
+ "github.com/containers/libpod/cmd/podman/cliconfig"
+ "github.com/spf13/cobra"
"os"
"github.com/containers/libpod/cmd/podman/libpodruntime"
"github.com/containers/libpod/libpod"
"github.com/containers/libpod/pkg/rootless"
"github.com/pkg/errors"
- "github.com/urfave/cli"
)
var (
- execFlags = []cli.Flag{
- cli.StringSliceFlag{
- Name: "env, e",
- Usage: "Set environment variables",
- },
- cli.BoolFlag{
- Name: "privileged",
- Usage: "Give the process extended Linux capabilities inside the container. The default is false",
- },
- cli.BoolFlag{
- Name: "interactive, i",
- Usage: "Not supported. All exec commands are interactive by default.",
- },
- cli.BoolFlag{
- Name: "tty, t",
- Usage: "Allocate a pseudo-TTY. The default is false",
- },
- cli.StringFlag{
- Name: "user, u",
- Usage: "Sets the username or UID used and optionally the groupname or GID for the specified command",
- },
- LatestFlag,
- WorkDirFlag,
- }
+ execCommand cliconfig.ExecValues
+
execDescription = `
podman exec
Run a command in a running container
`
-
- execCommand = cli.Command{
- Name: "exec",
- Usage: "Run a process in a running container",
- Description: execDescription,
- Flags: sortFlags(execFlags),
- Action: execCmd,
- ArgsUsage: "CONTAINER-NAME",
- SkipArgReorder: true,
- UseShortOptionHandling: true,
- OnUsageError: usageErrorHandler,
+ _execCommand = &cobra.Command{
+ Use: "exec",
+ Short: "Run a process in a running container",
+ Long: execDescription,
+ RunE: func(cmd *cobra.Command, args []string) error {
+ execCommand.InputArgs = args
+ execCommand.GlobalFlags = MainGlobalOpts
+ return execCmd(&execCommand)
+ },
+ Example: "CONTAINER-NAME",
}
)
-func execCmd(c *cli.Context) error {
- args := c.Args()
+func init() {
+ execCommand.Command = _execCommand
+ flags := execCommand.Flags()
+ flags.SetInterspersed(false)
+ flags.StringSliceVarP(&execCommand.Env, "env", "e", []string{}, "Set environment variables")
+ flags.BoolVarP(&execCommand.Interfactive, "interactive", "i", false, "Not supported. All exec commands are interactive by default")
+ flags.BoolVarP(&execCommand.Latest, "latest", "l", false, "Act on the latest container podman is aware of")
+ flags.BoolVar(&execCommand.Privileged, "privileged", false, "Give the process extended Linux capabilities inside the container. The default is false")
+ flags.BoolVarP(&execCommand.Tty, "tty", "t", false, "Allocate a pseudo-TTY. The default is false")
+ flags.StringVarP(&execCommand.User, "user", "u", "", "Sets the username or UID used and optionally the groupname or GID for the specified command")
+
+ flags.StringVarP(&execCommand.Workdir, "workdir", "w", "", "Working directory inside the container")
+
+ rootCmd.AddCommand(execCommand.Command)
+}
+
+func execCmd(c *cliconfig.ExecValues) error {
+ args := c.InputArgs
var ctr *libpod.Container
var err error
argStart := 1
- if len(args) < 1 && !c.Bool("latest") {
+ if len(args) < 1 && !c.Latest {
return errors.Errorf("you must provide one container name or id")
}
- if len(args) < 2 && !c.Bool("latest") {
+ if len(args) < 2 && !c.Latest {
return errors.Errorf("you must provide a command to exec")
}
- if c.Bool("latest") {
+ if c.Latest {
argStart = 0
}
rootless.SetSkipStorageSetup(true)
cmd := args[argStart:]
- runtime, err := libpodruntime.GetRuntime(c)
+ runtime, err := libpodruntime.GetRuntime(&c.PodmanCommand)
if err != nil {
return errors.Wrapf(err, "error creating libpod runtime")
}
defer runtime.Shutdown(false)
- if c.Bool("latest") {
+ if c.Latest {
ctr, err = runtime.GetLatestContainer()
} else {
ctr, err = runtime.LookupContainer(args[0])
@@ -101,7 +95,7 @@ func execCmd(c *cli.Context) error {
// ENVIRONMENT VARIABLES
env := map[string]string{}
- if err := readKVStrings(env, []string{}, c.StringSlice("env")); err != nil {
+ if err := readKVStrings(env, []string{}, c.Env); err != nil {
return errors.Wrapf(err, "unable to process environment variables")
}
envs := []string{}
@@ -109,5 +103,5 @@ func execCmd(c *cli.Context) error {
envs = append(envs, fmt.Sprintf("%s=%s", k, v))
}
- return ctr.Exec(c.Bool("tty"), c.Bool("privileged"), envs, cmd, c.String("user"), c.String("workdir"))
+ return ctr.Exec(c.Tty, c.Privileged, envs, cmd, c.User, c.Workdir)
}