File: cmd_get.go

package info (click to toggle)
snapd 2.73-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 81,460 kB
  • sloc: sh: 16,736; ansic: 16,652; python: 11,215; makefile: 1,966; exp: 190; awk: 58; xml: 22
file content (374 lines) | stat: -rw-r--r-- 10,160 bytes parent folder | download | duplicates (2)
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// -*- 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"
	"errors"
	"fmt"
	"sort"
	"strings"

	"github.com/jessevdk/go-flags"

	"github.com/snapcore/snapd/client"
	"github.com/snapcore/snapd/features"
	"github.com/snapcore/snapd/i18n"
	"github.com/snapcore/snapd/jsonutil"
)

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
`)

var longConfdbGetHelp = i18n.G(`
If the first argument passed into get is a confdb identifier matching the
format <account-id>/<schema>/<view>, get will use the confdb API. In this
case, the command returns the data retrieved from the requested view paths.
Use --default to provide a default value to be used if no value is stored.
`)

type cmdGet struct {
	mustWaitMixin
	Positional struct {
		Snap installedSnapName `required:"yes"`
		Keys []string
	} `positional-args:"yes"`

	Typed    bool   `short:"t"`
	Document bool   `short:"d"`
	List     bool   `short:"l"`
	Default  string `long:"default" unquote:"false"`
}

func init() {
	if err := validateConfdbFeatureFlag(); err == nil {
		longGetHelp += longConfdbGetHelp
	}

	addCommand("get", shortGetHelp, longGetHelp, func() flags.Commander {
		// a confdb transaction shouldn't be cancelled mid-way since we need to be
		// consistent when running hooks (i.e., not run for some but not others)
		return &cmdGet{mustWaitMixin: mustWaitMixin{skipAbort: true}}
	},
		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"),
			// TRANSLATORS: This should not start with a lowercase letter.
			"default": i18n.G("A strictly typed default value to be used when none is found"),
		}, []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 any
}

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]any, root bool) (values []ConfigValue) {
	const docstr = "{...}"
	for k, v := range cfg {
		if input, ok := v.(map[string]any); ok {
			if root {
				values = append(values, ConfigValue{k, docstr})
			} else {
				for kk, vv := range input {
					p := k + "." + kk
					if _, ok := vv.(map[string]any); 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 any) 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]any) 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]any, snapName string, confKeys []string) error {
	if rootRequested(confKeys) && len(conf) == 0 {
		return fmt.Errorf("snap %q has no configuration", snapName)
	}

	var confToPrint any = 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]any); !ok {
			confToPrint = conf[confKeys[0]]
		}
	}

	// conf looks like a map
	if cfg, ok := confToPrint.(map[string]any); ok {
		if isStdoutTTY {
			return x.outputList(cfg)
		}

		// TODO: remove this conditional and the warning below
		// after a transition period.
		fmt.Fprint(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

	var conf map[string]any
	var err error
	if isConfdbViewID(snapName) {
		// first argument is a confdbViewID, use the confdb API
		conf, err = x.getConfdb(snapName, confKeys)
	} else {
		if x.Default != "" {
			return fmt.Errorf(`cannot use --default in non-confdb read`)
		}

		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)
	}
}

func (x *cmdGet) getConfdb(confdbViewID string, confKeys []string) (map[string]any, error) {
	if err := validateConfdbFeatureFlag(); err != nil {
		return nil, err
	}

	if err := validateConfdbViewID(confdbViewID); err != nil {
		return nil, err
	}

	if x.Default != "" && len(confKeys) > 1 {
		// TODO: what if some keys are fulfilled and others aren't? Do we fill in
		// just the ones that are missing or none?
		return nil, fmt.Errorf("cannot use --default with more than one confdb request")
	}

	chgID, err := x.client.ConfdbGetViaView(confdbViewID, confKeys)
	if err != nil {
		return nil, err
	}

	chg, err := x.wait(chgID)
	if err != nil {
		// no noWait check, doesn't make sense with confdb read
		return nil, err
	}

	var conf map[string]any
	err = chg.Get("values", &conf)
	if err != nil {
		if !errors.Is(err, client.ErrNoData) {
			return nil, err
		}

		var errData map[string]any
		if err := chg.Get("error", &errData); err != nil {
			return nil, err
		}

		if errData["kind"] == string(client.ErrorKindConfigNoSuchOption) && x.Default != "" {
			// we don't allow --default with multiple keys so we know there's only one
			return x.buildDefaultOutput(confKeys[0])
		}

		errMsg, ok := errData["message"]
		if !ok {
			return nil, fmt.Errorf(`internal error: expected "message" field under "error" in change result`)
		}

		return nil, errors.New(errMsg.(string))
	}

	return conf, nil
}

func (x *cmdGet) buildDefaultOutput(request string) (map[string]any, error) {
	var defaultVal any
	if err := jsonutil.DecodeWithNumber(strings.NewReader(x.Default), &defaultVal); err != nil {
		var merr *json.SyntaxError
		if !errors.As(err, &merr) {
			// shouldn't happen as other errors are due to programmer error
			return nil, fmt.Errorf("internal error: cannot unmarshal --default value: %v", err)
		}

		if x.Typed {
			return nil, fmt.Errorf("cannot unmarshal default value as strictly typed")
		}

		// the value isn't typed, use it as is
		defaultVal = x.Default
	}

	return map[string]any{request: defaultVal}, nil
}

func validateConfdbFeatureFlag() error {
	if !features.Confdb.IsEnabled() {
		_, confName := features.Confdb.ConfigOption()
		return fmt.Errorf(`the "confdb" feature is disabled: set '%s' to true`, confName)
	}
	return nil
}