beam/examples/beamsh: run commands in an implicit context of pre-loaded 'plugins'

Docker-DCO-1.1-Signed-off-by: Solomon Hykes <solomon@docker.com> (github: shykes)
This commit is contained in:
Solomon Hykes 2014-03-28 00:01:35 -07:00
parent e99e1288cc
commit 2198d2423c

View file

@ -19,12 +19,12 @@ import (
"sync" "sync"
) )
var rootPlugins = []string{
"devnull",
"stdio",
}
func main() { func main() {
devnull, err := Devnull()
if err != nil {
Fatal(err)
}
defer devnull.Close()
if len(os.Args) == 1 { if len(os.Args) == 1 {
if term.IsTerminal(0) { if term.IsTerminal(0) {
input := bufio.NewScanner(os.Stdin) input := bufio.NewScanner(os.Stdin)
@ -40,7 +40,7 @@ func main() {
fmt.Fprintf(os.Stderr, "error: %v\n", err) fmt.Fprintf(os.Stderr, "error: %v\n", err)
continue continue
} }
if err := executeScript(devnull, cmd); err != nil { if err := executeRootScript(cmd); err != nil {
Fatal(err) Fatal(err)
} }
} }
@ -55,7 +55,7 @@ func main() {
if err != nil { if err != nil {
Fatal("parse error: %v\n", err) Fatal("parse error: %v\n", err)
} }
if err := executeScript(devnull, script); err != nil { if err := executeRootScript(script); err != nil {
Fatal(err) Fatal(err)
} }
} }
@ -69,7 +69,7 @@ func main() {
if err != nil { if err != nil {
Fatal("parse error: %v\n", err) Fatal("parse error: %v\n", err)
} }
if err := executeScript(devnull, script); err != nil { if err := executeRootScript(script); err != nil {
Fatal(err) Fatal(err)
} }
} }
@ -133,6 +133,29 @@ func scriptString(script []*dockerscript.Command) string {
return fmt.Sprintf("'%s'", strings.Join(lines, "; ")) return fmt.Sprintf("'%s'", strings.Join(lines, "; "))
} }
func executeRootScript(script []*dockerscript.Command) error {
if len(rootPlugins) > 0 {
var (
rootCmd *dockerscript.Command
lastCmd *dockerscript.Command
)
for _, plugin := range rootPlugins {
pluginCmd := &dockerscript.Command{
Args: []string{plugin},
}
if rootCmd == nil {
rootCmd = pluginCmd
} else {
lastCmd.Children = []*dockerscript.Command{pluginCmd}
}
lastCmd = pluginCmd
}
lastCmd.Children = script
script = []*dockerscript.Command{rootCmd}
}
return executeScript(nil, script)
}
func executeScript(client *net.UnixConn, script []*dockerscript.Command) error { func executeScript(client *net.UnixConn, script []*dockerscript.Command) error {
Debugf("executeScript(%s)\n", scriptString(script)) Debugf("executeScript(%s)\n", scriptString(script))
defer Debugf("executeScript(%s) DONE\n", scriptString(script)) defer Debugf("executeScript(%s) DONE\n", scriptString(script))
@ -357,13 +380,15 @@ func executeCommand(client *net.UnixConn, cmd *dockerscript.Command) error {
tasks.Done() tasks.Done()
}() }()
go func() { go func() {
Debugf("[%s] copy start...\n", strings.Join(cmd.Args, " ")) if client != nil {
n, err := beamCopy(client, outPub) Debugf("[%s] copy start...\n", strings.Join(cmd.Args, " "))
if err != nil { n, err := beamCopy(client, outPub)
Fatal(err) if err != nil {
Fatal(err)
}
Debugf("[%s] copied %d messages\n", strings.Join(cmd.Args, " "), n)
Debugf("[%s] copy done\n", strings.Join(cmd.Args, " "))
} }
Debugf("[%s] copied %d messages\n", strings.Join(cmd.Args, " "), n)
Debugf("[%s] copy done\n", strings.Join(cmd.Args, " "))
tasks.Done() tasks.Done()
}() }()
// depth-first execution of children commands // depth-first execution of children commands