This repository has been archived by the owner on Jan 9, 2020. It is now read-only.
forked from juju/juju
/
get.go
92 lines (77 loc) · 2.17 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
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package commands
import (
"errors"
"github.com/juju/cmd"
"launchpad.net/gnuflag"
"github.com/juju/juju/cmd/envcmd"
)
// GetCommand retrieves the configuration of a service.
type GetCommand struct {
envcmd.EnvCommandBase
ServiceName string
out cmd.Output
}
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 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:])
}
// 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.NewAPIClient()
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)
}