2014-09-09 00:19:32 -04:00
|
|
|
package runconfig
|
|
|
|
|
|
|
|
import (
|
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
|
|
|
)
|
|
|
|
|
2015-07-25 05:11:45 -04:00
|
|
|
// ExecConfig is a small subset of the Config struct that hold the configuration
|
|
|
|
// for the exec feature of docker.
|
2014-09-09 00:19:32 -04:00
|
|
|
type ExecConfig struct {
|
2015-07-25 05:11:45 -04:00
|
|
|
User string // User that will run the command
|
|
|
|
Privileged bool // Is the container in privileged mode
|
|
|
|
Tty bool // Attach standard streams to a tty.
|
|
|
|
Container string // Name of the container (to execute in)
|
|
|
|
AttachStdin bool // Attach the standard input, makes possible user interaction
|
|
|
|
AttachStderr bool // Attach the standard output
|
|
|
|
AttachStdout bool // Attach the standard error
|
|
|
|
Detach bool // Execute in detach mode
|
|
|
|
Cmd []string // Execution commands and args
|
2014-09-09 00:19:32 -04:00
|
|
|
}
|
|
|
|
|
2015-07-25 05:11:45 -04:00
|
|
|
// ParseExec parses the specified args for the specified command and generates
|
|
|
|
// an ExecConfig from it.
|
|
|
|
// If the minimal number of specified args is not right or if specified args are
|
|
|
|
// not valid, it will return an error.
|
2014-09-09 00:19:32 -04:00
|
|
|
func ParseExec(cmd *flag.FlagSet, args []string) (*ExecConfig, error) {
|
|
|
|
var (
|
2015-06-19 02:01:50 -04:00
|
|
|
flStdin = cmd.Bool([]string{"i", "-interactive"}, false, "Keep STDIN open even if not attached")
|
|
|
|
flTty = cmd.Bool([]string{"t", "-tty"}, false, "Allocate a pseudo-TTY")
|
|
|
|
flDetach = cmd.Bool([]string{"d", "-detach"}, false, "Detached mode: run command in the background")
|
|
|
|
flUser = cmd.String([]string{"u", "-user"}, "", "Username or UID (format: <name|uid>[:<group|gid>])")
|
|
|
|
flPrivileged = cmd.Bool([]string{"-privileged"}, false, "Give extended privileges to the command")
|
|
|
|
execCmd []string
|
|
|
|
container string
|
2014-09-09 00:19:32 -04:00
|
|
|
)
|
2014-11-05 11:57:51 -05:00
|
|
|
cmd.Require(flag.Min, 2)
|
2015-03-28 21:22:46 -04:00
|
|
|
if err := cmd.ParseFlags(args, true); err != nil {
|
2014-09-09 00:19:32 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-12-01 12:16:49 -05:00
|
|
|
container = cmd.Arg(0)
|
2014-10-15 17:14:12 -04:00
|
|
|
parsedArgs := cmd.Args()
|
2014-12-01 12:16:49 -05:00
|
|
|
execCmd = parsedArgs[1:]
|
2014-09-09 00:19:32 -04:00
|
|
|
|
|
|
|
execConfig := &ExecConfig{
|
2015-06-19 02:01:50 -04:00
|
|
|
User: *flUser,
|
|
|
|
Privileged: *flPrivileged,
|
|
|
|
Tty: *flTty,
|
|
|
|
Cmd: execCmd,
|
|
|
|
Container: container,
|
|
|
|
Detach: *flDetach,
|
2014-09-09 00:19:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// If -d is not set, attach to everything by default
|
|
|
|
if !*flDetach {
|
|
|
|
execConfig.AttachStdout = true
|
|
|
|
execConfig.AttachStderr = true
|
|
|
|
if *flStdin {
|
|
|
|
execConfig.AttachStdin = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return execConfig, nil
|
|
|
|
}
|