Adding --format/-f option to `inspect` subcommand

Signed-off-by: Dave Henderson <Dave.Henderson@ca.ibm.com>
This commit is contained in:
Dave Henderson 2015-03-30 19:03:23 -04:00
parent ee41dc36df
commit c93a17481c
3 changed files with 143 additions and 7 deletions

View File

@ -223,6 +223,13 @@ var Commands = []cli.Command{
Usage: "Inspect information about a machine", Usage: "Inspect information about a machine",
Description: "Argument is a machine name. Will use the active machine if none is provided.", Description: "Argument is a machine name. Will use the active machine if none is provided.",
Action: cmdInspect, Action: cmdInspect,
Flags: []cli.Flag{
cli.StringFlag{
Name: "format, f",
Usage: "Format the output using the given go template.",
Value: "",
},
},
}, },
{ {
Name: "ip", Name: "ip",

View File

@ -3,17 +3,44 @@ package commands
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"os"
"text/template"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
) )
func cmdInspect(c *cli.Context) { var funcMap = template.FuncMap{
prettyJSON, err := json.MarshalIndent(getHost(c), "", " ") "json": func(v interface{}) string {
if err != nil { a, _ := json.Marshal(v)
log.Fatal(err) return string(a)
} },
"prettyJSON": func(v interface{}) string {
fmt.Println(string(prettyJSON)) a, _ := json.MarshalIndent(v, "", " ")
return string(a)
},
}
func cmdInspect(c *cli.Context) {
tmplString := c.String("format")
if tmplString != "" {
var tmpl *template.Template
var err error
if tmpl, err = template.New("").Funcs(funcMap).Parse(tmplString); err != nil {
log.Fatalf("Template parsing error: %v\n", err)
}
if err := tmpl.Execute(os.Stderr, getHost(c)); err != nil {
log.Fatal(err)
}
os.Stderr.Write([]byte{'\n'})
} else {
prettyJSON, err := json.MarshalIndent(getHost(c), "", " ")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(prettyJSON))
}
} }

View File

@ -1 +1,103 @@
package commands package commands
import (
"bytes"
"encoding/json"
"flag"
"io"
"os"
"strings"
"testing"
"github.com/codegangsta/cli"
"github.com/docker/machine/libmachine"
"github.com/docker/machine/libmachine/auth"
"github.com/docker/machine/libmachine/engine"
"github.com/docker/machine/libmachine/swarm"
"github.com/stretchr/testify/assert"
)
func TestCmdInspectFormat(t *testing.T) {
actual, host := runInspectCommand(t, []string{})
expected, _ := json.MarshalIndent(host, "", " ")
assert.Equal(t, string(expected), actual)
actual, _ = runInspectCommand(t, []string{"--format", "{{.DriverName}}"})
assert.Equal(t, "none", actual)
actual, _ = runInspectCommand(t, []string{"--format", "{{json .DriverName}}"})
assert.Equal(t, "\"none\"", actual)
actual, _ = runInspectCommand(t, []string{"--format", "{{prettyJSON .Driver}}"})
assert.Equal(t, "{\n \"URL\": \"unix:///var/run/docker.sock\"\n}", actual)
}
func runInspectCommand(t *testing.T, args []string) (string, *libmachine.Host) {
stdout := os.Stdout
stderr := os.Stderr
shell := os.Getenv("SHELL")
r, w, _ := os.Pipe()
os.Stdout = w
os.Stderr = w
os.Setenv("MACHINE_STORAGE_PATH", TestStoreDir)
os.Setenv("SHELL", "/bin/bash")
defer func() {
os.Setenv("MACHINE_STORAGE_PATH", "")
os.Setenv("SHELL", shell)
os.Stdout = stdout
os.Stderr = stderr
}()
if err := clearHosts(); err != nil {
t.Fatal(err)
}
store, sErr := getTestStore()
if sErr != nil {
t.Fatal(sErr)
}
mcn, err := libmachine.New(store)
if err != nil {
t.Fatal(err)
}
hostOptions := &libmachine.HostOptions{
EngineOptions: &engine.EngineOptions{},
SwarmOptions: &swarm.SwarmOptions{
Master: false,
Discovery: "",
Address: "",
Host: "",
},
AuthOptions: &auth.AuthOptions{},
}
flags := getTestDriverFlags()
_, err = mcn.Create("test-a", "none", hostOptions, flags)
if err != nil {
t.Fatal(err)
}
outStr := make(chan string)
go func() {
var testOutput bytes.Buffer
io.Copy(&testOutput, r)
outStr <- testOutput.String()
}()
set := flag.NewFlagSet("inspect", 0)
set.String("format", "", "")
set.Parse(args)
c := cli.NewContext(nil, set, set)
cmdInspect(c)
w.Close()
out := <-outStr
return strings.TrimSpace(out), getHost(c)
}