Browse code

Refactor the flag management for main Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)

Michael Crosby authored on 2014/02/21 11:38:28
Showing 3 changed files
... ...
@@ -44,18 +44,14 @@ func Exec(container *libcontainer.Container, tty bool, args []string) (int, erro
44 44
 	system.UsetCloseOnExec(r.Fd())
45 45
 
46 46
 	command := createCommand(container, console, r.Fd(), args)
47
-
48 47
 	if !tty {
49
-		inPipe, err = command.StdinPipe()
50
-		if err != nil {
48
+		if inPipe, err = command.StdinPipe(); err != nil {
51 49
 			return -1, err
52 50
 		}
53
-		outPipe, err = command.StdoutPipe()
54
-		if err != nil {
51
+		if outPipe, err = command.StdoutPipe(); err != nil {
55 52
 			return -1, err
56 53
 		}
57
-		errPipe, err = command.StderrPipe()
58
-		if err != nil {
54
+		if errPipe, err = command.StderrPipe(); err != nil {
59 55
 			return -1, err
60 56
 		}
61 57
 	}
... ...
@@ -63,7 +59,6 @@ func Exec(container *libcontainer.Container, tty bool, args []string) (int, erro
63 63
 	if err := command.Start(); err != nil {
64 64
 		return -1, err
65 65
 	}
66
-
67 66
 	if err := writePidFile(command); err != nil {
68 67
 		command.Process.Kill()
69 68
 		return -1, err
... ...
@@ -94,6 +89,7 @@ func Exec(container *libcontainer.Container, tty bool, args []string) (int, erro
94 94
 	if tty {
95 95
 		go io.Copy(os.Stdout, master)
96 96
 		go io.Copy(master, os.Stdin)
97
+
97 98
 		state, err := setupWindow(master)
98 99
 		if err != nil {
99 100
 			command.Process.Kill()
... ...
@@ -114,7 +110,6 @@ func Exec(container *libcontainer.Container, tty bool, args []string) (int, erro
114 114
 			return -1, err
115 115
 		}
116 116
 	}
117
-
118 117
 	return command.ProcessState.Sys().(syscall.WaitStatus).ExitStatus(), nil
119 118
 }
120 119
 
... ...
@@ -173,7 +173,6 @@ func setupVethNetwork(config *libcontainer.Network, tempVethName string) error {
173 173
 // has been created and setup
174 174
 func getVethName(pipe io.ReadCloser) (string, error) {
175 175
 	defer pipe.Close()
176
-
177 176
 	data, err := ioutil.ReadAll(pipe)
178 177
 	if err != nil {
179 178
 		return "", fmt.Errorf("error reading from stdin %s", err)
... ...
@@ -13,26 +13,33 @@ import (
13 13
 )
14 14
 
15 15
 var (
16
+	console string
17
+	tty     bool
18
+	pipeFd  int
19
+)
20
+
21
+var (
16 22
 	ErrUnsupported    = errors.New("Unsupported method")
17 23
 	ErrWrongArguments = errors.New("Wrong argument count")
18 24
 )
19 25
 
20
-func main() {
21
-	var (
22
-		console = flag.String("console", "", "Console (pty slave) name")
23
-		tty     = flag.Bool("tty", false, "Create a tty")
24
-		pipeFd  = flag.Int("pipe", 0, "sync pipe fd")
25
-	)
26
+func init() {
27
+	flag.StringVar(&console, "console", "", "console (pty slave) path")
28
+	flag.BoolVar(&tty, "tty", false, "create a tty")
29
+	flag.IntVar(&pipeFd, "pipe", 0, "sync pipe fd")
30
+
26 31
 	flag.Parse()
32
+}
27 33
 
34
+func main() {
28 35
 	container, err := loadContainer()
29 36
 	if err != nil {
30 37
 		log.Fatal(err)
31 38
 	}
32
-
33 39
 	if flag.NArg() < 1 {
34 40
 		log.Fatal(ErrWrongArguments)
35 41
 	}
42
+
36 43
 	switch flag.Arg(0) {
37 44
 	case "exec": // this is executed outside of the namespace in the cwd
38 45
 		var exitCode int
... ...
@@ -45,7 +52,7 @@ func main() {
45 45
 		if nspid > 0 {
46 46
 			exitCode, err = nsinit.ExecIn(container, nspid, flag.Args()[1:])
47 47
 		} else {
48
-			exitCode, err = nsinit.Exec(container, *tty, flag.Args()[1:])
48
+			exitCode, err = nsinit.Exec(container, tty, flag.Args()[1:])
49 49
 		}
50 50
 		if err != nil {
51 51
 			log.Fatal(err)
... ...
@@ -55,7 +62,7 @@ func main() {
55 55
 		if flag.NArg() < 2 {
56 56
 			log.Fatal(ErrWrongArguments)
57 57
 		}
58
-		if err := nsinit.Init(container, *console, os.NewFile(uintptr(*pipeFd), "pipe"), flag.Args()[1:]); err != nil {
58
+		if err := nsinit.Init(container, console, os.NewFile(uintptr(pipeFd), "pipe"), flag.Args()[1:]); err != nil {
59 59
 			log.Fatal(err)
60 60
 		}
61 61
 	default: