mirror of https://github.com/docker/docs.git
Merge pull request #4314 from shykes/engine-commands
Engine: builtin command 'commands' returns a list of registered commands
This commit is contained in:
commit
56584a92f4
|
@ -9,6 +9,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -29,6 +30,10 @@ func Register(name string, handler Handler) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func unregister(name string) {
|
||||||
|
delete(globalHandlers, name)
|
||||||
|
}
|
||||||
|
|
||||||
// The Engine is the core of Docker.
|
// The Engine is the core of Docker.
|
||||||
// It acts as a store for *containers*, and allows manipulation of these
|
// It acts as a store for *containers*, and allows manipulation of these
|
||||||
// containers by executing *jobs*.
|
// containers by executing *jobs*.
|
||||||
|
@ -106,6 +111,12 @@ func New(root string) (*Engine, error) {
|
||||||
Stderr: os.Stderr,
|
Stderr: os.Stderr,
|
||||||
Stdin: os.Stdin,
|
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
|
// Copy existing global handlers
|
||||||
for k, v := range globalHandlers {
|
for k, v := range globalHandlers {
|
||||||
eng.handlers[k] = v
|
eng.handlers[k] = v
|
||||||
|
@ -117,6 +128,17 @@ func (eng *Engine) String() string {
|
||||||
return fmt.Sprintf("%s|%s", eng.Root(), eng.id[:8])
|
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.
|
// Job creates a new job which can later be executed.
|
||||||
// This function mimics `Command` from the standard os/exec package.
|
// This function mimics `Command` from the standard os/exec package.
|
||||||
func (eng *Engine) Job(name string, args ...string) *Job {
|
func (eng *Engine) Job(name string, args ...string) *Job {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package engine
|
package engine
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
@ -17,6 +18,8 @@ func TestRegister(t *testing.T) {
|
||||||
if err := Register("dummy1", nil); err == nil {
|
if err := Register("dummy1", nil); err == nil {
|
||||||
t.Fatalf("Expecting error, got none")
|
t.Fatalf("Expecting error, got none")
|
||||||
}
|
}
|
||||||
|
// Register is global so let's cleanup to avoid conflicts
|
||||||
|
defer unregister("dummy1")
|
||||||
|
|
||||||
eng := newTestEngine(t)
|
eng := newTestEngine(t)
|
||||||
|
|
||||||
|
@ -33,6 +36,7 @@ func TestRegister(t *testing.T) {
|
||||||
if err := eng.Register("dummy2", nil); err == nil {
|
if err := eng.Register("dummy2", nil); err == nil {
|
||||||
t.Fatalf("Expecting error, got none")
|
t.Fatalf("Expecting error, got none")
|
||||||
}
|
}
|
||||||
|
defer unregister("dummy2")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestJob(t *testing.T) {
|
func TestJob(t *testing.T) {
|
||||||
|
@ -49,6 +53,7 @@ func TestJob(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
eng.Register("dummy2", h)
|
eng.Register("dummy2", h)
|
||||||
|
defer unregister("dummy2")
|
||||||
job2 := eng.Job("dummy2", "--level=awesome")
|
job2 := eng.Job("dummy2", "--level=awesome")
|
||||||
|
|
||||||
if job2.handler == nil {
|
if job2.handler == nil {
|
||||||
|
@ -60,6 +65,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) {
|
func TestEngineRoot(t *testing.T) {
|
||||||
tmp, err := ioutil.TempDir("", "docker-test-TestEngineCreateDir")
|
tmp, err := ioutil.TempDir("", "docker-test-TestEngineCreateDir")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue