forked from snapcore/snapd
/
cmd_get.go
258 lines (217 loc) · 6.57 KB
/
cmd_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package main
import (
"encoding/json"
"fmt"
"sort"
"strings"
"github.com/jessevdk/go-flags"
"github.com/snapcore/snapd/i18n"
)
var shortGetHelp = i18n.G("Print configuration options")
var longGetHelp = i18n.G(`
The get command prints configuration options for the provided snap.
$ snap get snap-name username
frank
If multiple option names are provided, the corresponding values are returned:
$ snap get snap-name username password
Key Value
username frank
password ...
Nested values may be retrieved via a dotted path:
$ snap get snap-name author.name
frank
`)
type cmdGet struct {
clientMixin
Positional struct {
Snap installedSnapName `required:"yes"`
Keys []string
} `positional-args:"yes"`
Typed bool `short:"t"`
Document bool `short:"d"`
List bool `short:"l"`
}
func init() {
addCommand("get", shortGetHelp, longGetHelp, func() flags.Commander { return &cmdGet{} },
map[string]string{
// TRANSLATORS: This should not start with a lowercase letter.
"d": i18n.G("Always return document, even with single key"),
// TRANSLATORS: This should not start with a lowercase letter.
"l": i18n.G("Always return list, even with single key"),
// TRANSLATORS: This should not start with a lowercase letter.
"t": i18n.G("Strict typing with nulls and quoted strings"),
}, []argDesc{
{
name: "<snap>",
// TRANSLATORS: This should not start with a lowercase letter.
desc: i18n.G("The snap whose conf is being requested"),
},
{
// TRANSLATORS: This needs to begin with < and end with >
name: i18n.G("<key>"),
// TRANSLATORS: This should not start with a lowercase letter.
desc: i18n.G("Key of interest within the configuration"),
},
})
}
type ConfigValue struct {
Path string
Value interface{}
}
type byConfigPath []ConfigValue
func (s byConfigPath) Len() int { return len(s) }
func (s byConfigPath) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s byConfigPath) Less(i, j int) bool {
other := s[j].Path
for k, c := range s[i].Path {
if len(other) <= k {
return false
}
switch {
case c == rune(other[k]):
continue
case c == '.':
return true
case other[k] == '.' || c > rune(other[k]):
return false
}
return true
}
return true
}
func sortByPath(config []ConfigValue) {
sort.Sort(byConfigPath(config))
}
func flattenConfig(cfg map[string]interface{}, root bool) (values []ConfigValue) {
const docstr = "{...}"
for k, v := range cfg {
if input, ok := v.(map[string]interface{}); ok {
if root {
values = append(values, ConfigValue{k, docstr})
} else {
for kk, vv := range input {
p := k + "." + kk
if _, ok := vv.(map[string]interface{}); ok {
values = append(values, ConfigValue{p, docstr})
} else {
values = append(values, ConfigValue{p, vv})
}
}
}
} else {
values = append(values, ConfigValue{k, v})
}
}
sortByPath(values)
return values
}
func rootRequested(confKeys []string) bool {
return len(confKeys) == 0
}
// outputJson will be used when the user requested "document" output via
// the "-d" commandline switch.
func (c *cmdGet) outputJson(conf interface{}) error {
bytes, err := json.MarshalIndent(conf, "", "\t")
if err != nil {
return err
}
fmt.Fprintln(Stdout, string(bytes))
return nil
}
// outputList will be used when the user requested list output via the
// "-l" commandline switch.
func (x *cmdGet) outputList(conf map[string]interface{}) error {
if rootRequested(x.Positional.Keys) && len(conf) == 0 {
return fmt.Errorf("snap %q has no configuration", x.Positional.Snap)
}
w := tabWriter()
defer w.Flush()
fmt.Fprintf(w, "Key\tValue\n")
values := flattenConfig(conf, rootRequested(x.Positional.Keys))
for _, v := range values {
fmt.Fprintf(w, "%s\t%v\n", v.Path, v.Value)
}
return nil
}
// outputDefault will be used when no commandline switch to override the
// output where used. The output follows the following rules:
// - a single key with a string value is printed directly
// - multiple keys are printed as a list to the terminal (if there is one)
// or as json if there is no terminal
// - the option "typed" is honored
func (x *cmdGet) outputDefault(conf map[string]interface{}, snapName string, confKeys []string) error {
if rootRequested(confKeys) && len(conf) == 0 {
return fmt.Errorf("snap %q has no configuration", snapName)
}
var confToPrint interface{} = conf
if len(confKeys) == 1 {
// if single key was requested, then just output the
// value unless it's a map, in which case it will be
// printed as a list below.
if _, ok := conf[confKeys[0]].(map[string]interface{}); !ok {
confToPrint = conf[confKeys[0]]
}
}
// conf looks like a map
if cfg, ok := confToPrint.(map[string]interface{}); ok {
if isStdinTTY {
return x.outputList(cfg)
}
// TODO: remove this conditional and the warning below
// after a transition period.
fmt.Fprintf(Stderr, i18n.G(`WARNING: The output of 'snap get' will become a list with columns - use -d or -l to force the output format.\n`))
return x.outputJson(confToPrint)
}
if s, ok := confToPrint.(string); ok && !x.Typed {
fmt.Fprintln(Stdout, s)
return nil
}
if confToPrint != nil || x.Typed {
return x.outputJson(confToPrint)
}
fmt.Fprintln(Stdout, "")
return nil
}
func (x *cmdGet) Execute(args []string) error {
if len(args) > 0 {
// TRANSLATORS: the %s is the list of extra arguments
return fmt.Errorf(i18n.G("too many arguments: %s"), strings.Join(args, " "))
}
if x.Document && x.Typed {
return fmt.Errorf("cannot use -d and -t together")
}
if x.Document && x.List {
return fmt.Errorf("cannot use -d and -l together")
}
snapName := string(x.Positional.Snap)
confKeys := x.Positional.Keys
conf, err := x.client.Conf(snapName, confKeys)
if err != nil {
return err
}
switch {
case x.Document:
return x.outputJson(conf)
case x.List:
return x.outputList(conf)
default:
return x.outputDefault(conf, snapName, confKeys)
}
}