diff --git a/beam/beam_test.go b/beam/beam_test.go index 3c938d9..2822861 100644 --- a/beam/beam_test.go +++ b/beam/beam_test.go @@ -1,8 +1,8 @@ package beam import ( - "testing" "github.com/dotcloud/docker/pkg/beam/data" + "testing" ) func TestSendConn(t *testing.T) { diff --git a/beam/data/data.go b/beam/data/data.go index 5604259..e205fe4 100644 --- a/beam/data/data.go +++ b/beam/data/data.go @@ -2,8 +2,8 @@ package data import ( "fmt" - "strings" "strconv" + "strings" ) func Encode(obj map[string][]string) string { @@ -93,10 +93,10 @@ func decodeString(msg string) (string, int, error) { } else { length = int(l) } - if len(parts[1]) < length + 1 { - return "", 0, fmt.Errorf("message '%s' is %d bytes, expected at least %d", parts[1], len(parts[1]), length + 1) + if len(parts[1]) < length+1 { + return "", 0, fmt.Errorf("message '%s' is %d bytes, expected at least %d", parts[1], len(parts[1]), length+1) } - payload := parts[1][:length + 1] + payload := parts[1][:length+1] if payload[length] != ',' { return "", 0, fmt.Errorf("message is not comma-terminated") } diff --git a/beam/data/data_test.go b/beam/data/data_test.go index 6beb365..9059922 100644 --- a/beam/data/data_test.go +++ b/beam/data/data_test.go @@ -92,10 +92,10 @@ func TestEncodeBinaryValue(t *testing.T) { } func TestDecodeString(t *testing.T) { - validEncodedStrings := []struct{ - input string + validEncodedStrings := []struct { + input string output string - skip int + skip int }{ {"3:foo,", "foo", 6}, {"5:hello,", "hello", 8}, diff --git a/beam/examples/beamsh/beamsh b/beam/examples/beamsh/beamsh new file mode 100755 index 0000000..9bfe78e Binary files /dev/null and b/beam/examples/beamsh/beamsh differ diff --git a/beam/examples/beamsh/beamsh.go b/beam/examples/beamsh/beamsh.go index d8b4011..3f258de 100644 --- a/beam/examples/beamsh/beamsh.go +++ b/beam/examples/beamsh/beamsh.go @@ -2,6 +2,7 @@ package main import ( "bufio" + "flag" "fmt" "github.com/dotcloud/docker/pkg/beam" "github.com/dotcloud/docker/pkg/beam/data" @@ -14,7 +15,6 @@ import ( "path" "strings" "sync" - "flag" ) var rootPlugins = []string{ @@ -22,8 +22,8 @@ var rootPlugins = []string{ } var ( - flX bool - flPing bool + flX bool + flPing bool introspect beam.ReceiveSender = beam.Devnull() ) @@ -38,7 +38,7 @@ func main() { fd3.Close() flag.BoolVar(&flX, "x", false, "print commands as they are being executed") flag.Parse() - if flag.NArg() == 0{ + if flag.NArg() == 0 { if term.IsTerminal(0) { // No arguments, stdin is terminal --> interactive mode input := bufio.NewScanner(os.Stdin) @@ -168,7 +168,6 @@ func executeScript(out beam.Sender, script []*dockerscript.Command) error { return nil } - // 1) Find a handler for the command (if no handler, fail) // 2) Attach new in & out pair to the handler // 3) [in the background] Copy handler output to our own output @@ -217,10 +216,8 @@ func executeCommand(out beam.Sender, cmd *dockerscript.Command) error { return nil } - type Handler func([]string, io.Writer, io.Writer, beam.Receiver, beam.Sender) - func Handlers(sink beam.Sender) (*beam.UnixConn, error) { var tasks sync.WaitGroup pub, priv, err := beam.USocketPair() @@ -329,11 +326,12 @@ func GetHandler(name string) Handler { return nil } - // VARIOUS HELPER FUNCTIONS: func connToFile(conn net.Conn) (f *os.File, err error) { - if connWithFile, ok := conn.(interface { File() (*os.File, error) }); !ok { + if connWithFile, ok := conn.(interface { + File() (*os.File, error) + }); !ok { return nil, fmt.Errorf("no file descriptor available") } else { f, err = connWithFile.File() @@ -345,12 +343,12 @@ func connToFile(conn net.Conn) (f *os.File, err error) { } type Msg struct { - payload []byte - attachment *os.File + payload []byte + attachment *os.File } func Logf(msg string, args ...interface{}) (int, error) { - if len(msg) == 0 || msg[len(msg) - 1] != '\n' { + if len(msg) == 0 || msg[len(msg)-1] != '\n' { msg = msg + "\n" } msg = fmt.Sprintf("[%v] [%v] %s", os.Getpid(), path.Base(os.Args[0]), msg) @@ -363,7 +361,7 @@ func Debugf(msg string, args ...interface{}) { } } -func Fatalf(msg string, args ...interface{}) { +func Fatalf(msg string, args ...interface{}) { Logf(msg, args...) os.Exit(1) } @@ -386,7 +384,6 @@ func scriptString(script []*dockerscript.Command) string { return fmt.Sprintf("'%s'", strings.Join(lines, "; ")) } - func dialer(addr string) (chan net.Conn, error) { u, err := url.Parse(addr) if err != nil { @@ -400,7 +397,7 @@ func dialer(addr string) (chan net.Conn, error) { if err != nil { return } - connections <-conn + connections <- conn } }() return connections, nil @@ -424,14 +421,12 @@ func listener(addr string) (chan net.Conn, error) { return } Logf("new connection\n") - connections<-conn + connections <- conn } }() return connections, nil } - - func SendToConn(connections chan net.Conn, src beam.Receiver) error { var tasks sync.WaitGroup defer tasks.Wait() @@ -479,14 +474,13 @@ func SendToConn(connections chan net.Conn, src beam.Receiver) error { return nil } - func msgDesc(payload []byte, attachment *os.File) string { return beam.MsgDesc(payload, attachment) } func ReceiveFromConn(connections chan net.Conn, dst beam.Sender) error { for conn := range connections { - err := func () error { + err := func() error { Logf("parsing message from network...\n") defer Logf("done parsing message from network\n") buf := make([]byte, 4098) @@ -534,7 +528,6 @@ func ReceiveFromConn(connections chan net.Conn, dst beam.Sender) error { return nil } - func bicopy(a, b io.ReadWriteCloser) { var iotasks sync.WaitGroup oneCopy := func(dst io.WriteCloser, src io.Reader) { @@ -547,4 +540,3 @@ func bicopy(a, b io.ReadWriteCloser) { go oneCopy(b, a) iotasks.Wait() } - diff --git a/beam/examples/beamsh/builtins.go b/beam/examples/beamsh/builtins.go index 08b8a62..cc94d2b 100644 --- a/beam/examples/beamsh/builtins.go +++ b/beam/examples/beamsh/builtins.go @@ -1,24 +1,23 @@ package main import ( - "io" - "os/exec" + "bufio" + "fmt" "github.com/dotcloud/docker/pkg/beam" "github.com/dotcloud/docker/pkg/beam/data" "github.com/dotcloud/docker/pkg/term" "github.com/dotcloud/docker/utils" - "text/template" - "fmt" - "sync" - "os" - "strings" - "path" - "bufio" + "io" "net" "net/url" + "os" + "os/exec" + "path" + "strings" + "sync" + "text/template" ) - func CmdLogger(args []string, stdout, stderr io.Writer, in beam.Receiver, out beam.Sender) { if err := os.MkdirAll("logs", 0700); err != nil { fmt.Fprintf(stderr, "%v\n", err) @@ -28,7 +27,7 @@ func CmdLogger(args []string, stdout, stderr io.Writer, in beam.Receiver, out be defer tasks.Wait() var n int = 1 r := beam.NewRouter(out) - r.NewRoute().HasAttachment().KeyStartsWith("cmd", "log").Handler(func (payload []byte, attachment *os.File) error { + r.NewRoute().HasAttachment().KeyStartsWith("cmd", "log").Handler(func(payload []byte, attachment *os.File) error { tasks.Add(1) go func(n int) { defer tasks.Done() @@ -398,7 +397,7 @@ func CmdConnect(args []string, stdout, stderr io.Writer, in beam.Receiver, out b Logf("connecting to %s/%s\n", u.Scheme, u.Host) conn, err := net.Dial(u.Scheme, u.Host) if err != nil { - out.Send(data.Empty().Set("cmd", "msg", "connect error: " + err.Error()).Bytes(), nil) + out.Send(data.Empty().Set("cmd", "msg", "connect error: "+err.Error()).Bytes(), nil) return } out.Send(data.Empty().Set("cmd", "msg", "connection established").Bytes(), nil) diff --git a/beam/router.go b/beam/router.go index abdfe61..fc41a89 100644 --- a/beam/router.go +++ b/beam/router.go @@ -1,19 +1,19 @@ package beam import ( - "io" "fmt" - "os" "github.com/dotcloud/docker/pkg/beam/data" + "io" + "os" ) type Router struct { routes []*Route - sink Sender + sink Sender } func NewRouter(sink Sender) *Router { - return &Router{sink:sink} + return &Router{sink: sink} } func (r *Router) Send(payload []byte, attachment *os.File) (err error) { @@ -40,10 +40,8 @@ func (r *Router) NewRoute() *Route { return route } - - type Route struct { - rules []func([]byte, *os.File) bool + rules []func([]byte, *os.File) bool handler func([]byte, *os.File) error } @@ -70,7 +68,6 @@ func (r *Route) HasAttachment() *Route { return r } - func (route *Route) Tee(dst Sender) *Route { inner := route.handler route.handler = func(payload []byte, attachment *os.File) error { @@ -125,8 +122,7 @@ func (r *Route) KeyStartsWith(k string, beginning ...string) *Route { return r } - -func (r *Route) KeyEquals(k string, full...string) *Route { +func (r *Route) KeyEquals(k string, full ...string) *Route { r.rules = append(r.rules, func(payload []byte, attachment *os.File) bool { values := data.Message(payload).Get(k) if len(values) != len(full) { diff --git a/beam/router_test.go b/beam/router_test.go index f4c0cb1..f7f7bf1 100644 --- a/beam/router_test.go +++ b/beam/router_test.go @@ -3,13 +3,13 @@ package beam import ( "fmt" "io/ioutil" - "testing" "os" "sync" + "testing" ) type msg struct { - payload []byte + payload []byte attachment *os.File } @@ -17,7 +17,6 @@ func (m msg) String() string { return MsgDesc(m.payload, m.attachment) } - type mockReceiver []msg func (r *mockReceiver) Send(p []byte, a *os.File) error { diff --git a/beam/unix.go b/beam/unix.go index b138934..b480c47 100644 --- a/beam/unix.go +++ b/beam/unix.go @@ -1,11 +1,11 @@ package beam import ( + "bufio" "fmt" "net" "os" "syscall" - "bufio" ) func debugCheckpoint(msg string, args ...interface{}) { @@ -13,7 +13,7 @@ func debugCheckpoint(msg string, args ...interface{}) { return } os.Stdout.Sync() - tty,_ := os.OpenFile("/dev/tty", os.O_RDWR, 0700) + tty, _ := os.OpenFile("/dev/tty", os.O_RDWR, 0700) fmt.Fprintf(tty, msg, args...) bufio.NewScanner(tty).Scan() tty.Close() @@ -170,7 +170,7 @@ func SocketPair() (a *os.File, b *os.File, err error) { func USocketPair() (*UnixConn, *UnixConn, error) { debugCheckpoint("===DEBUG=== USocketPair(). Hit enter to confirm: ") - defer debugCheckpoint ("===DEBUG=== USocketPair() returned. Hit enter to confirm ") + defer debugCheckpoint("===DEBUG=== USocketPair() returned. Hit enter to confirm ") a, b, err := SocketPair() if err != nil { return nil, nil, err @@ -193,7 +193,7 @@ func USocketPair() (*UnixConn, *UnixConn, error) { // returns an error if the file descriptor does not point to a unix socket. // This creates a duplicate file descriptor. It's the caller's responsibility // to close both. -func FdConn(fd int) (n*net.UnixConn, err error) { +func FdConn(fd int) (n *net.UnixConn, err error) { { debugCheckpoint("===DEBUG=== FdConn([%d]) = (unknown fd). Hit enter to confirm: ", fd) } diff --git a/dockerscript/dockerscript.go b/dockerscript/dockerscript.go index 582f1fd..e7ec5d1 100644 --- a/dockerscript/dockerscript.go +++ b/dockerscript/dockerscript.go @@ -1,15 +1,15 @@ package dockerscript import ( - "github.com/dotcloud/docker/pkg/dockerscript/scanner" "fmt" + "github.com/dotcloud/docker/pkg/dockerscript/scanner" "io" "strings" ) type Command struct { - Args []string - Children []*Command + Args []string + Children []*Command Background bool } @@ -32,7 +32,7 @@ func Parse(src io.Reader) ([]*Command, error) { func (cmd *Command) subString(depth int) string { var prefix string - for i:=0; i %s\n", c) - } - }() + defer func() { + fmt.Printf("parse() returned %d commands:\n", len(expr)) + for _, c := range expr { + fmt.Printf("\t----> %s\n", c) + } + }() */ for { args, tok, err := parseArgs(s) diff --git a/dockerscript/scanner/extra.go b/dockerscript/scanner/extra.go index c3e88b9..05c17e2 100644 --- a/dockerscript/scanner/extra.go +++ b/dockerscript/scanner/extra.go @@ -1,8 +1,8 @@ package scanner import ( - "unicode" "strings" + "unicode" ) // extra functions used to hijack the upstream text/scanner @@ -19,4 +19,3 @@ func detectIdent(ch rune) bool { } return false } -