Skip to content
This repository was archived by the owner on Dec 17, 2021. It is now read-only.

Commit 8db2e4b

Browse files
committed
commands/inspect: Add
1 parent 55e8b5c commit 8db2e4b

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

commands/inspect.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package commands
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"os"
7+
"text/template"
8+
9+
"github.com/docker/machine/libmachine"
10+
)
11+
12+
var funcMap = template.FuncMap{
13+
"json": func(v interface{}) string {
14+
a, _ := json.Marshal(v)
15+
return string(a)
16+
},
17+
"prettyjson": func(v interface{}) string {
18+
a, _ := json.MarshalIndent(v, "", " ")
19+
return string(a)
20+
},
21+
}
22+
23+
func cmdInspect(c CommandLine, api libmachine.API) error {
24+
if len(c.Args()) > 1 {
25+
c.ShowHelp()
26+
return ErrExpectedOneMachine
27+
}
28+
29+
target, err := targetHost(c, api)
30+
if err != nil {
31+
return err
32+
}
33+
34+
host, err := api.Load(target)
35+
if err != nil {
36+
return err
37+
}
38+
39+
tmplString := c.String("format")
40+
if tmplString != "" {
41+
var tmpl *template.Template
42+
var err error
43+
if tmpl, err = template.New("").Funcs(funcMap).Parse(tmplString); err != nil {
44+
return fmt.Errorf("template parsing error: %v", err)
45+
}
46+
47+
jsonHost, err := json.Marshal(host)
48+
if err != nil {
49+
return err
50+
}
51+
52+
obj := make(map[string]interface{})
53+
if err := json.Unmarshal(jsonHost, &obj); err != nil {
54+
return err
55+
}
56+
57+
if err := tmpl.Execute(os.Stdout, obj); err != nil {
58+
return err
59+
}
60+
61+
os.Stdout.Write([]byte{'\n'})
62+
} else {
63+
prettyJSON, err := json.MarshalIndent(host, "", " ")
64+
if err != nil {
65+
return err
66+
}
67+
68+
fmt.Println(string(prettyJSON))
69+
}
70+
71+
return nil
72+
}

commands/inspect_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package commands
2+
3+
import (
4+
"testing"
5+
6+
"github.com/docker/machine/commands/commandstest"
7+
"github.com/docker/machine/libmachine"
8+
"github.com/docker/machine/libmachine/libmachinetest"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestCmdInspect(t *testing.T) {
13+
testCases := []struct {
14+
commandLine CommandLine
15+
api libmachine.API
16+
expectedErr error
17+
}{
18+
{
19+
commandLine: &commandstest.FakeCommandLine{
20+
CliArgs: []string{"foo", "bar"},
21+
},
22+
api: &libmachinetest.FakeAPI{},
23+
expectedErr: ErrExpectedOneMachine,
24+
},
25+
}
26+
27+
for _, tc := range testCases {
28+
err := cmdInspect(tc.commandLine, tc.api)
29+
assert.Equal(t, tc.expectedErr, err)
30+
}
31+
}

0 commit comments

Comments
 (0)