1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Engine: builtin command 'commands' returns a list of registered commands

Docker-DCO-1.1-Signed-off-by: Solomon Hykes <solomon@docker.com> (github: shykes)
This commit is contained in:
Solomon Hykes 2014-02-15 15:06:21 -08:00
parent 62b21daded
commit cd846ecb60
2 changed files with 37 additions and 0 deletions

View file

@ -9,6 +9,7 @@ import (
"os"
"path/filepath"
"runtime"
"sort"
"strings"
)
@ -110,6 +111,12 @@ func New(root string) (*Engine, error) {
Stderr: os.Stderr,
Stdin: os.Stdin,
}
eng.Register("commands", func(job *Job) Status {
for _, name := range eng.commands() {
job.Printf("%s\n", name)
}
return StatusOK
})
// Copy existing global handlers
for k, v := range globalHandlers {
eng.handlers[k] = v
@ -121,6 +128,17 @@ func (eng *Engine) String() string {
return fmt.Sprintf("%s|%s", eng.Root(), eng.id[:8])
}
// Commands returns a list of all currently registered commands,
// sorted alphabetically.
func (eng *Engine) commands() []string {
names := make([]string, 0, len(eng.handlers))
for name := range eng.handlers {
names = append(names, name)
}
sort.Strings(names)
return names
}
// Job creates a new job which can later be executed.
// This function mimics `Command` from the standard os/exec package.
func (eng *Engine) Job(name string, args ...string) *Job {

View file

@ -1,6 +1,7 @@
package engine
import (
"bytes"
"io/ioutil"
"os"
"path"
@ -63,6 +64,24 @@ func TestJob(t *testing.T) {
}
}
func TestEngineCommands(t *testing.T) {
eng := newTestEngine(t)
defer os.RemoveAll(eng.Root())
handler := func(job *Job) Status { return StatusOK }
eng.Register("foo", handler)
eng.Register("bar", handler)
eng.Register("echo", handler)
eng.Register("die", handler)
var output bytes.Buffer
commands := eng.Job("commands")
commands.Stdout.Add(&output)
commands.Run()
expected := "bar\ncommands\ndie\necho\nfoo\n"
if result := output.String(); result != expected {
t.Fatalf("Unexpected output:\nExpected = %v\nResult = %v\n", expected, result)
}
}
func TestEngineRoot(t *testing.T) {
tmp, err := ioutil.TempDir("", "docker-test-TestEngineCreateDir")
if err != nil {