Fix Go formatting in beam and dockerscript

Docker-DCO-1.1-Signed-off-by: Solomon Hykes <solomon@docker.com> (github: shykes)
This commit is contained in:
Solomon Hykes 2014-04-22 16:09:42 -07:00
parent 1df27fa300
commit c5b6f20d56
11 changed files with 56 additions and 71 deletions

View file

@ -1,8 +1,8 @@
package beam package beam
import ( import (
"testing"
"github.com/dotcloud/docker/pkg/beam/data" "github.com/dotcloud/docker/pkg/beam/data"
"testing"
) )
func TestSendConn(t *testing.T) { func TestSendConn(t *testing.T) {

View file

@ -2,8 +2,8 @@ package data
import ( import (
"fmt" "fmt"
"strings"
"strconv" "strconv"
"strings"
) )
func Encode(obj map[string][]string) string { func Encode(obj map[string][]string) string {
@ -93,10 +93,10 @@ func decodeString(msg string) (string, int, error) {
} else { } else {
length = int(l) length = int(l)
} }
if 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) 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] != ',' { if payload[length] != ',' {
return "", 0, fmt.Errorf("message is not comma-terminated") return "", 0, fmt.Errorf("message is not comma-terminated")
} }

View file

@ -92,10 +92,10 @@ func TestEncodeBinaryValue(t *testing.T) {
} }
func TestDecodeString(t *testing.T) { func TestDecodeString(t *testing.T) {
validEncodedStrings := []struct{ validEncodedStrings := []struct {
input string input string
output string output string
skip int skip int
}{ }{
{"3:foo,", "foo", 6}, {"3:foo,", "foo", 6},
{"5:hello,", "hello", 8}, {"5:hello,", "hello", 8},

BIN
beam/examples/beamsh/beamsh Executable file

Binary file not shown.

View file

@ -2,6 +2,7 @@ package main
import ( import (
"bufio" "bufio"
"flag"
"fmt" "fmt"
"github.com/dotcloud/docker/pkg/beam" "github.com/dotcloud/docker/pkg/beam"
"github.com/dotcloud/docker/pkg/beam/data" "github.com/dotcloud/docker/pkg/beam/data"
@ -14,7 +15,6 @@ import (
"path" "path"
"strings" "strings"
"sync" "sync"
"flag"
) )
var rootPlugins = []string{ var rootPlugins = []string{
@ -22,8 +22,8 @@ var rootPlugins = []string{
} }
var ( var (
flX bool flX bool
flPing bool flPing bool
introspect beam.ReceiveSender = beam.Devnull() introspect beam.ReceiveSender = beam.Devnull()
) )
@ -38,7 +38,7 @@ func main() {
fd3.Close() fd3.Close()
flag.BoolVar(&flX, "x", false, "print commands as they are being executed") flag.BoolVar(&flX, "x", false, "print commands as they are being executed")
flag.Parse() flag.Parse()
if flag.NArg() == 0{ if flag.NArg() == 0 {
if term.IsTerminal(0) { if term.IsTerminal(0) {
// No arguments, stdin is terminal --> interactive mode // No arguments, stdin is terminal --> interactive mode
input := bufio.NewScanner(os.Stdin) input := bufio.NewScanner(os.Stdin)
@ -168,7 +168,6 @@ func executeScript(out beam.Sender, script []*dockerscript.Command) error {
return nil return nil
} }
// 1) Find a handler for the command (if no handler, fail) // 1) Find a handler for the command (if no handler, fail)
// 2) Attach new in & out pair to the handler // 2) Attach new in & out pair to the handler
// 3) [in the background] Copy handler output to our own output // 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 return nil
} }
type Handler func([]string, io.Writer, io.Writer, beam.Receiver, beam.Sender) type Handler func([]string, io.Writer, io.Writer, beam.Receiver, beam.Sender)
func Handlers(sink beam.Sender) (*beam.UnixConn, error) { func Handlers(sink beam.Sender) (*beam.UnixConn, error) {
var tasks sync.WaitGroup var tasks sync.WaitGroup
pub, priv, err := beam.USocketPair() pub, priv, err := beam.USocketPair()
@ -329,11 +326,12 @@ func GetHandler(name string) Handler {
return nil return nil
} }
// VARIOUS HELPER FUNCTIONS: // VARIOUS HELPER FUNCTIONS:
func connToFile(conn net.Conn) (f *os.File, err error) { 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") return nil, fmt.Errorf("no file descriptor available")
} else { } else {
f, err = connWithFile.File() f, err = connWithFile.File()
@ -345,12 +343,12 @@ func connToFile(conn net.Conn) (f *os.File, err error) {
} }
type Msg struct { type Msg struct {
payload []byte payload []byte
attachment *os.File attachment *os.File
} }
func Logf(msg string, args ...interface{}) (int, error) { 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 = msg + "\n"
} }
msg = fmt.Sprintf("[%v] [%v] %s", os.Getpid(), path.Base(os.Args[0]), msg) 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...) Logf(msg, args...)
os.Exit(1) os.Exit(1)
} }
@ -386,7 +384,6 @@ func scriptString(script []*dockerscript.Command) string {
return fmt.Sprintf("'%s'", strings.Join(lines, "; ")) return fmt.Sprintf("'%s'", strings.Join(lines, "; "))
} }
func dialer(addr string) (chan net.Conn, error) { func dialer(addr string) (chan net.Conn, error) {
u, err := url.Parse(addr) u, err := url.Parse(addr)
if err != nil { if err != nil {
@ -400,7 +397,7 @@ func dialer(addr string) (chan net.Conn, error) {
if err != nil { if err != nil {
return return
} }
connections <-conn connections <- conn
} }
}() }()
return connections, nil return connections, nil
@ -424,14 +421,12 @@ func listener(addr string) (chan net.Conn, error) {
return return
} }
Logf("new connection\n") Logf("new connection\n")
connections<-conn connections <- conn
} }
}() }()
return connections, nil return connections, nil
} }
func SendToConn(connections chan net.Conn, src beam.Receiver) error { func SendToConn(connections chan net.Conn, src beam.Receiver) error {
var tasks sync.WaitGroup var tasks sync.WaitGroup
defer tasks.Wait() defer tasks.Wait()
@ -479,14 +474,13 @@ func SendToConn(connections chan net.Conn, src beam.Receiver) error {
return nil return nil
} }
func msgDesc(payload []byte, attachment *os.File) string { func msgDesc(payload []byte, attachment *os.File) string {
return beam.MsgDesc(payload, attachment) return beam.MsgDesc(payload, attachment)
} }
func ReceiveFromConn(connections chan net.Conn, dst beam.Sender) error { func ReceiveFromConn(connections chan net.Conn, dst beam.Sender) error {
for conn := range connections { for conn := range connections {
err := func () error { err := func() error {
Logf("parsing message from network...\n") Logf("parsing message from network...\n")
defer Logf("done parsing message from network\n") defer Logf("done parsing message from network\n")
buf := make([]byte, 4098) buf := make([]byte, 4098)
@ -534,7 +528,6 @@ func ReceiveFromConn(connections chan net.Conn, dst beam.Sender) error {
return nil return nil
} }
func bicopy(a, b io.ReadWriteCloser) { func bicopy(a, b io.ReadWriteCloser) {
var iotasks sync.WaitGroup var iotasks sync.WaitGroup
oneCopy := func(dst io.WriteCloser, src io.Reader) { oneCopy := func(dst io.WriteCloser, src io.Reader) {
@ -547,4 +540,3 @@ func bicopy(a, b io.ReadWriteCloser) {
go oneCopy(b, a) go oneCopy(b, a)
iotasks.Wait() iotasks.Wait()
} }

View file

@ -1,24 +1,23 @@
package main package main
import ( import (
"io" "bufio"
"os/exec" "fmt"
"github.com/dotcloud/docker/pkg/beam" "github.com/dotcloud/docker/pkg/beam"
"github.com/dotcloud/docker/pkg/beam/data" "github.com/dotcloud/docker/pkg/beam/data"
"github.com/dotcloud/docker/pkg/term" "github.com/dotcloud/docker/pkg/term"
"github.com/dotcloud/docker/utils" "github.com/dotcloud/docker/utils"
"text/template" "io"
"fmt"
"sync"
"os"
"strings"
"path"
"bufio"
"net" "net"
"net/url" "net/url"
"os"
"os/exec"
"path"
"strings"
"sync"
"text/template"
) )
func CmdLogger(args []string, stdout, stderr io.Writer, in beam.Receiver, out beam.Sender) { func CmdLogger(args []string, stdout, stderr io.Writer, in beam.Receiver, out beam.Sender) {
if err := os.MkdirAll("logs", 0700); err != nil { if err := os.MkdirAll("logs", 0700); err != nil {
fmt.Fprintf(stderr, "%v\n", err) 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() defer tasks.Wait()
var n int = 1 var n int = 1
r := beam.NewRouter(out) 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) tasks.Add(1)
go func(n int) { go func(n int) {
defer tasks.Done() 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) Logf("connecting to %s/%s\n", u.Scheme, u.Host)
conn, err := net.Dial(u.Scheme, u.Host) conn, err := net.Dial(u.Scheme, u.Host)
if err != nil { 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 return
} }
out.Send(data.Empty().Set("cmd", "msg", "connection established").Bytes(), nil) out.Send(data.Empty().Set("cmd", "msg", "connection established").Bytes(), nil)

View file

@ -1,19 +1,19 @@
package beam package beam
import ( import (
"io"
"fmt" "fmt"
"os"
"github.com/dotcloud/docker/pkg/beam/data" "github.com/dotcloud/docker/pkg/beam/data"
"io"
"os"
) )
type Router struct { type Router struct {
routes []*Route routes []*Route
sink Sender sink Sender
} }
func NewRouter(sink Sender) *Router { func NewRouter(sink Sender) *Router {
return &Router{sink:sink} return &Router{sink: sink}
} }
func (r *Router) Send(payload []byte, attachment *os.File) (err error) { func (r *Router) Send(payload []byte, attachment *os.File) (err error) {
@ -40,10 +40,8 @@ func (r *Router) NewRoute() *Route {
return route return route
} }
type Route struct { type Route struct {
rules []func([]byte, *os.File) bool rules []func([]byte, *os.File) bool
handler func([]byte, *os.File) error handler func([]byte, *os.File) error
} }
@ -70,7 +68,6 @@ func (r *Route) HasAttachment() *Route {
return r return r
} }
func (route *Route) Tee(dst Sender) *Route { func (route *Route) Tee(dst Sender) *Route {
inner := route.handler inner := route.handler
route.handler = func(payload []byte, attachment *os.File) error { route.handler = func(payload []byte, attachment *os.File) error {
@ -125,8 +122,7 @@ func (r *Route) KeyStartsWith(k string, beginning ...string) *Route {
return r 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 { r.rules = append(r.rules, func(payload []byte, attachment *os.File) bool {
values := data.Message(payload).Get(k) values := data.Message(payload).Get(k)
if len(values) != len(full) { if len(values) != len(full) {

View file

@ -3,13 +3,13 @@ package beam
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"testing"
"os" "os"
"sync" "sync"
"testing"
) )
type msg struct { type msg struct {
payload []byte payload []byte
attachment *os.File attachment *os.File
} }
@ -17,7 +17,6 @@ func (m msg) String() string {
return MsgDesc(m.payload, m.attachment) return MsgDesc(m.payload, m.attachment)
} }
type mockReceiver []msg type mockReceiver []msg
func (r *mockReceiver) Send(p []byte, a *os.File) error { func (r *mockReceiver) Send(p []byte, a *os.File) error {

View file

@ -1,11 +1,11 @@
package beam package beam
import ( import (
"bufio"
"fmt" "fmt"
"net" "net"
"os" "os"
"syscall" "syscall"
"bufio"
) )
func debugCheckpoint(msg string, args ...interface{}) { func debugCheckpoint(msg string, args ...interface{}) {
@ -13,7 +13,7 @@ func debugCheckpoint(msg string, args ...interface{}) {
return return
} }
os.Stdout.Sync() 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...) fmt.Fprintf(tty, msg, args...)
bufio.NewScanner(tty).Scan() bufio.NewScanner(tty).Scan()
tty.Close() tty.Close()
@ -170,7 +170,7 @@ func SocketPair() (a *os.File, b *os.File, err error) {
func USocketPair() (*UnixConn, *UnixConn, error) { func USocketPair() (*UnixConn, *UnixConn, error) {
debugCheckpoint("===DEBUG=== USocketPair(). Hit enter to confirm: ") 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() a, b, err := SocketPair()
if err != nil { if err != nil {
return nil, nil, err 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. // 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 // This creates a duplicate file descriptor. It's the caller's responsibility
// to close both. // 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) debugCheckpoint("===DEBUG=== FdConn([%d]) = (unknown fd). Hit enter to confirm: ", fd)
} }

View file

@ -1,15 +1,15 @@
package dockerscript package dockerscript
import ( import (
"github.com/dotcloud/docker/pkg/dockerscript/scanner"
"fmt" "fmt"
"github.com/dotcloud/docker/pkg/dockerscript/scanner"
"io" "io"
"strings" "strings"
) )
type Command struct { type Command struct {
Args []string Args []string
Children []*Command Children []*Command
Background bool Background bool
} }
@ -32,7 +32,7 @@ func Parse(src io.Reader) ([]*Command, error) {
func (cmd *Command) subString(depth int) string { func (cmd *Command) subString(depth int) string {
var prefix string var prefix string
for i:=0; i<depth; i++ { for i := 0; i < depth; i++ {
prefix += " " prefix += " "
} }
s := prefix + strings.Join(cmd.Args, ", ") s := prefix + strings.Join(cmd.Args, ", ")
@ -85,12 +85,12 @@ func parseArgs(s *Scanner) ([]string, rune, error) {
func parse(s *Scanner, opener string) (expr []*Command, err error) { func parse(s *Scanner, opener string) (expr []*Command, err error) {
/* /*
defer func() { defer func() {
fmt.Printf("parse() returned %d commands:\n", len(expr)) fmt.Printf("parse() returned %d commands:\n", len(expr))
for _, c := range expr { for _, c := range expr {
fmt.Printf("\t----> %s\n", c) fmt.Printf("\t----> %s\n", c)
} }
}() }()
*/ */
for { for {
args, tok, err := parseArgs(s) args, tok, err := parseArgs(s)

View file

@ -1,8 +1,8 @@
package scanner package scanner
import ( import (
"unicode"
"strings" "strings"
"unicode"
) )
// extra functions used to hijack the upstream text/scanner // extra functions used to hijack the upstream text/scanner
@ -19,4 +19,3 @@ func detectIdent(ch rune) bool {
} }
return false return false
} }