Refactor the flag management for main

Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
This commit is contained in:
Michael Crosby 2014-02-20 18:38:28 -08:00
parent 1316007e54
commit 6b2e963ce0
3 changed files with 21 additions and 20 deletions

View file

@ -44,18 +44,14 @@ func Exec(container *libcontainer.Container, tty bool, args []string) (int, erro
system.UsetCloseOnExec(r.Fd()) system.UsetCloseOnExec(r.Fd())
command := createCommand(container, console, r.Fd(), args) command := createCommand(container, console, r.Fd(), args)
if !tty { if !tty {
inPipe, err = command.StdinPipe() if inPipe, err = command.StdinPipe(); err != nil {
if err != nil {
return -1, err return -1, err
} }
outPipe, err = command.StdoutPipe() if outPipe, err = command.StdoutPipe(); err != nil {
if err != nil {
return -1, err return -1, err
} }
errPipe, err = command.StderrPipe() if errPipe, err = command.StderrPipe(); err != nil {
if err != nil {
return -1, err return -1, err
} }
} }
@ -63,7 +59,6 @@ func Exec(container *libcontainer.Container, tty bool, args []string) (int, erro
if err := command.Start(); err != nil { if err := command.Start(); err != nil {
return -1, err return -1, err
} }
if err := writePidFile(command); err != nil { if err := writePidFile(command); err != nil {
command.Process.Kill() command.Process.Kill()
return -1, err return -1, err
@ -94,6 +89,7 @@ func Exec(container *libcontainer.Container, tty bool, args []string) (int, erro
if tty { if tty {
go io.Copy(os.Stdout, master) go io.Copy(os.Stdout, master)
go io.Copy(master, os.Stdin) go io.Copy(master, os.Stdin)
state, err := setupWindow(master) state, err := setupWindow(master)
if err != nil { if err != nil {
command.Process.Kill() command.Process.Kill()
@ -114,7 +110,6 @@ func Exec(container *libcontainer.Container, tty bool, args []string) (int, erro
return -1, err return -1, err
} }
} }
return command.ProcessState.Sys().(syscall.WaitStatus).ExitStatus(), nil return command.ProcessState.Sys().(syscall.WaitStatus).ExitStatus(), nil
} }

View file

@ -173,7 +173,6 @@ func setupVethNetwork(config *libcontainer.Network, tempVethName string) error {
// has been created and setup // has been created and setup
func getVethName(pipe io.ReadCloser) (string, error) { func getVethName(pipe io.ReadCloser) (string, error) {
defer pipe.Close() defer pipe.Close()
data, err := ioutil.ReadAll(pipe) data, err := ioutil.ReadAll(pipe)
if err != nil { if err != nil {
return "", fmt.Errorf("error reading from stdin %s", err) return "", fmt.Errorf("error reading from stdin %s", err)

View file

@ -12,27 +12,34 @@ import (
"strconv" "strconv"
) )
var (
console string
tty bool
pipeFd int
)
var ( var (
ErrUnsupported = errors.New("Unsupported method") ErrUnsupported = errors.New("Unsupported method")
ErrWrongArguments = errors.New("Wrong argument count") ErrWrongArguments = errors.New("Wrong argument count")
) )
func main() { func init() {
var ( flag.StringVar(&console, "console", "", "console (pty slave) path")
console = flag.String("console", "", "Console (pty slave) name") flag.BoolVar(&tty, "tty", false, "create a tty")
tty = flag.Bool("tty", false, "Create a tty") flag.IntVar(&pipeFd, "pipe", 0, "sync pipe fd")
pipeFd = flag.Int("pipe", 0, "sync pipe fd")
)
flag.Parse()
flag.Parse()
}
func main() {
container, err := loadContainer() container, err := loadContainer()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
if flag.NArg() < 1 { if flag.NArg() < 1 {
log.Fatal(ErrWrongArguments) log.Fatal(ErrWrongArguments)
} }
switch flag.Arg(0) { switch flag.Arg(0) {
case "exec": // this is executed outside of the namespace in the cwd case "exec": // this is executed outside of the namespace in the cwd
var exitCode int var exitCode int
@ -45,7 +52,7 @@ func main() {
if nspid > 0 { if nspid > 0 {
exitCode, err = nsinit.ExecIn(container, nspid, flag.Args()[1:]) exitCode, err = nsinit.ExecIn(container, nspid, flag.Args()[1:])
} else { } else {
exitCode, err = nsinit.Exec(container, *tty, flag.Args()[1:]) exitCode, err = nsinit.Exec(container, tty, flag.Args()[1:])
} }
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -55,7 +62,7 @@ func main() {
if flag.NArg() < 2 { if flag.NArg() < 2 {
log.Fatal(ErrWrongArguments) log.Fatal(ErrWrongArguments)
} }
if err := nsinit.Init(container, *console, os.NewFile(uintptr(*pipeFd), "pipe"), flag.Args()[1:]); err != nil { if err := nsinit.Init(container, console, os.NewFile(uintptr(pipeFd), "pipe"), flag.Args()[1:]); err != nil {
log.Fatal(err) log.Fatal(err)
} }
default: default: