Files
docker-docs/utils/tree.go
Aanand Prasad f2d9a4a270 Rearrange transport code
- Core types/functions moved from 'beam' to 'libswarm' top-level
- Helper types/functions moved from 'beam' to 'util'

Signed-off-by: Aanand Prasad <aanand.prasad@gmail.com>
2014-07-01 11:21:40 +01:00

47 lines
1.0 KiB
Go

package utils
import (
"github.com/docker/libswarm"
"sort"
)
type Tree struct {
*libswarm.Server
children map[string]libswarm.Sender
}
func NewTree() *Tree {
t := &Tree{
Server: libswarm.NewServer(),
children: make(map[string]libswarm.Sender),
}
t.OnVerb(libswarm.Attach, libswarm.Handler(func(msg *libswarm.Message) error {
if len(msg.Args) == 0 || msg.Args[0] == "" {
msg.Ret.Send(&libswarm.Message{Verb: libswarm.Ack, Ret: t})
return nil
}
if child, exists := t.children[msg.Args[0]]; exists {
msg.Ret.Send(&libswarm.Message{Verb: libswarm.Ack, Ret: child})
return nil
}
libswarm.Obj(msg.Ret).Error("not found")
return nil
}))
t.OnVerb(libswarm.Ls, libswarm.Handler(func(msg *libswarm.Message) error {
names := make([]string, 0, len(t.children))
for name := range t.children {
names = append(names, name)
}
sort.Strings(names)
libswarm.Obj(msg.Ret).Set(names...)
return nil
}))
return t
}
func (t *Tree) Bind(name string, dst libswarm.Sender) *Tree {
t.children[name] = dst
return t
}