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
Родитель 1316007e54
Коммит 6b2e963ce0
3 изменённых файлов: 21 добавлений и 20 удалений

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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