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