Browse code

beam/examples/beamsh: support for background commands with '&' terminator

Docker-DCO-1.1-Signed-off-by: Solomon Hykes <solomon@docker.com> (github: shykes)

Solomon Hykes authored on 2014/03/28 10:26:42
Showing 2 changed files
... ...
@@ -136,9 +136,19 @@ func scriptString(script []*dockerscript.Command) string {
136 136
 func executeScript(client *net.UnixConn, script []*dockerscript.Command) error {
137 137
 	Debugf("executeScript(%s)\n", scriptString(script))
138 138
 	defer Debugf("executeScript(%s) DONE\n", scriptString(script))
139
+	var background sync.WaitGroup
140
+	defer background.Wait()
139 141
 	for _, cmd := range script {
140
-		if err := executeCommand(client, cmd); err != nil {
141
-			return err
142
+		if cmd.Background {
143
+			background.Add(1)
144
+			go func(client *net.UnixConn, cmd *dockerscript.Command) {
145
+				executeCommand(client, cmd)
146
+				background.Done()
147
+			}(client, cmd)
148
+		} else {
149
+			if err := executeCommand(client, cmd); err != nil {
150
+				return err
151
+			}
142 152
 		}
143 153
 	}
144 154
 	return nil
... ...
@@ -10,6 +10,7 @@ import (
10 10
 type Command struct {
11 11
 	Args []string
12 12
 	Children []*Command
13
+	Background bool
13 14
 }
14 15
 
15 16
 type Scanner struct {
... ...
@@ -72,7 +73,7 @@ func parseArgs(s *Scanner) ([]string, rune, error) {
72 72
 			return args, tok, nil
73 73
 		}
74 74
 		if !s.commentLine {
75
-			if text == "{" || text == "}" || text == "\n" || text == "\r" || text == ";" {
75
+			if text == "{" || text == "}" || text == "\n" || text == "\r" || text == ";" || text == "&" {
76 76
 				return args, tok, nil
77 77
 			}
78 78
 			args = append(args, text)
... ...
@@ -106,6 +107,8 @@ func parse(s *Scanner, opener string) (expr []*Command, err error) {
106 106
 			cmd.Children = children
107 107
 		} else if afterArgs == "}" && opener != "{" {
108 108
 			return nil, fmt.Errorf("unexpected end of block '}'")
109
+		} else if afterArgs == "&" {
110
+			cmd.Background = true
109 111
 		}
110 112
 		if len(cmd.Args) > 0 || len(cmd.Children) > 0 {
111 113
 			expr = append(expr, cmd)