Left: | ||
Right: |
OLD | NEW |
---|---|
1 package main | 1 package main |
2 | 2 |
3 import ( | 3 import ( |
4 "errors" | 4 "errors" |
5 "reflect" | |
6 | 5 |
7 "launchpad.net/gnuflag" | 6 "launchpad.net/gnuflag" |
8 "launchpad.net/juju-core/charm" | |
9 "launchpad.net/juju-core/cmd" | 7 "launchpad.net/juju-core/cmd" |
10 "launchpad.net/juju-core/juju" | 8 "launchpad.net/juju-core/juju" |
9 "launchpad.net/juju-core/state/statecmd" | |
11 ) | 10 ) |
12 | 11 |
13 // GetCommand retrieves the configuration of a service. | 12 // GetCommand retrieves the configuration of a service. |
14 type GetCommand struct { | 13 type GetCommand struct { |
15 EnvName string | 14 EnvName string |
16 ServiceName string | 15 ServiceName string |
17 out cmd.Output | 16 out cmd.Output |
18 } | 17 } |
19 | 18 |
20 func (c *GetCommand) Info() *cmd.Info { | 19 func (c *GetCommand) Info() *cmd.Info { |
(...skipping 18 matching lines...) Expand all Loading... | |
39 } | 38 } |
40 | 39 |
41 // Run fetches the configuration of the service and formats | 40 // Run fetches the configuration of the service and formats |
42 // the result as a YAML string. | 41 // the result as a YAML string. |
43 func (c *GetCommand) Run(ctx *cmd.Context) error { | 42 func (c *GetCommand) Run(ctx *cmd.Context) error { |
44 conn, err := juju.NewConnFromName(c.EnvName) | 43 conn, err := juju.NewConnFromName(c.EnvName) |
45 if err != nil { | 44 if err != nil { |
46 return err | 45 return err |
47 } | 46 } |
48 defer conn.Close() | 47 defer conn.Close() |
49 » svc, err := conn.State.Service(c.ServiceName) | 48 |
49 » params := statecmd.ServiceGetParams{ | |
50 » » ServiceName: c.ServiceName, | |
51 » } | |
52 » var results statecmd.ServiceGetResults | |
dfc
2013/02/21 01:51:31
See comment in state/statecmd/get.go
bac
2013/02/21 15:32:54
Done.
| |
53 | |
54 » err = statecmd.ServiceGet(conn.State, params, &results) | |
dfc
2013/02/21 01:51:31
if err := statecmd.ServiceGet(conn.State, params,
dimitern
2013/02/21 13:26:16
+1
bac
2013/02/21 15:32:54
Can't use that form now that I'm catching two retu
| |
50 if err != nil { | 55 if err != nil { |
51 return err | 56 return err |
52 } | 57 } |
53 » svcfg, err := svc.Config() | 58 |
54 » if err != nil { | 59 » resultsMap := map[string]interface{}{ |
55 » » return err | 60 » » "service": results.Service, |
61 » » "charm": results.Charm, | |
62 » » "settings": results.Settings, | |
56 } | 63 } |
57 » charm, _, err := svc.Charm() | 64 » return c.out.Write(ctx, resultsMap) |
58 » if err != nil { | |
59 » » return err | |
60 » } | |
61 » chcfg := charm.Config().Options | |
62 | |
63 » config := merge(svcfg.Map(), chcfg) | |
64 | |
65 » result := map[string]interface{}{ | |
66 » » "service": svc.Name(), | |
67 » » "charm": charm.Meta().Name, | |
68 » » "settings": config, | |
69 » } | |
70 » return c.out.Write(ctx, result) | |
71 } | 65 } |
72 | |
73 // merge service settings and charm schema | |
74 func merge(svcfg map[string]interface{}, chcfg map[string]charm.Option) map[stri ng]interface{} { | |
75 r := make(map[string]interface{}) | |
76 for k, v := range chcfg { | |
77 m := map[string]interface{}{ | |
78 "description": v.Description, | |
79 "type": v.Type, | |
80 } | |
81 if s, ok := svcfg[k]; ok { | |
82 m["value"] = s | |
83 } else { | |
84 // breaks compatibility with py/juju | |
85 m["value"] = nil | |
86 } | |
87 if v.Default != nil { | |
88 if reflect.DeepEqual(v.Default, svcfg[k]) { | |
89 m["default"] = true | |
90 } | |
91 } | |
92 r[k] = m | |
93 } | |
94 return r | |
95 } | |
OLD | NEW |