/
get.go
108 lines (91 loc) · 2.56 KB
/
get.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2012-2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package service
import (
"errors"
"github.com/juju/cmd"
"launchpad.net/gnuflag"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/cmd/envcmd"
)
// GetCommand retrieves the configuration of a service.
type GetCommand struct {
envcmd.EnvCommandBase
ServiceName string
out cmd.Output
api GetServiceAPI
}
const getDoc = `
The command output includes the service and charm names, a detailed list of all config
settings for <service>, including the setting name, whether it uses the default value
or not ("default: true"), description (if set), type, and current value. Example:
$ juju service get wordpress
charm: wordpress
service: wordpress
settings:
engine:
default: true
description: 'Currently two ...'
type: string
value: nginx
tuning:
description: "This is the tuning level..."
type: string
value: optimized
NOTE: In the example above the descriptions and most other settings were omitted for
brevity. The "engine" setting was left at its default value ("nginx"), while the
"tuning" setting was set to "optimized" (the default value is "single").
`
func (c *GetCommand) Info() *cmd.Info {
return &cmd.Info{
Name: "get",
Args: "<service>",
Purpose: "get service configuration options",
Doc: getDoc,
}
}
func (c *GetCommand) SetFlags(f *gnuflag.FlagSet) {
// TODO(dfc) add json formatting ?
c.out.AddFlags(f, "yaml", map[string]cmd.Formatter{
"yaml": cmd.FormatYaml,
})
}
func (c *GetCommand) Init(args []string) error {
// TODO(dfc) add --schema-only
if len(args) == 0 {
return errors.New("no service name specified")
}
c.ServiceName = args[0]
return cmd.CheckEmpty(args[1:])
}
// GetServiceAPI defines the methods on the client API
// that the service get command calls.
type GetServiceAPI interface {
Close() error
ServiceGet(service string) (*params.ServiceGetResults, error)
}
func (c *GetCommand) getAPI() (GetServiceAPI, error) {
if c.api != nil {
return c.api, nil
}
return c.NewAPIClient()
}
// Run fetches the configuration of the service and formats
// the result as a YAML string.
func (c *GetCommand) Run(ctx *cmd.Context) error {
client, err := c.getAPI()
if err != nil {
return err
}
defer client.Close()
results, err := client.ServiceGet(c.ServiceName)
if err != nil {
return err
}
resultsMap := map[string]interface{}{
"service": results.Service,
"charm": results.Charm,
"settings": results.Config,
}
return c.out.Write(ctx, resultsMap)
}