File: context.go

package info (click to toggle)
golang-step-cli-utils 0.7.5%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 404 kB
  • sloc: makefile: 19
file content (558 lines) | stat: -rw-r--r-- 13,463 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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
package step

import (
	"encoding/json"
	"fmt"
	"os"
	"path/filepath"
	"reflect"
	"sort"
	"strings"

	"github.com/pkg/errors"
	"github.com/urfave/cli"
	"go.step.sm/cli-utils/errs"
	"go.step.sm/cli-utils/ui"
)

// IgnoreEnvVar is a value added to a flag EnvVar to avoid the use of
// environment variables or configuration files.
const IgnoreEnvVar = "STEP_IGNORE_ENV_VAR"

// Context represents a Step Path configuration context. A context is the
// combination of a profile and an authority.
type Context struct {
	Name      string `json:"-"`
	Profile   string `json:"profile"`
	Authority string `json:"authority"`
	config    map[string]interface{}
}

// Validate validates a context and returns an error if invalid.
func (c *Context) Validate() error {
	suffix := "; check your $STEPPATH/contexts.json file"
	if c == nil {
		return errors.Errorf("context cannot be nil%s", suffix)
	}
	if c.Authority == "" {
		return errors.Errorf("context cannot have an empty authority value%s", suffix)
	}
	if c.Profile == "" {
		return errors.Errorf("context cannot have an empty profile value%s", suffix)
	}
	return nil
}

// Path return the base path relative to the context.
func (c *Context) Path() string {
	return filepath.Join(BasePath(), "authorities", c.Authority)
}

// ProfilePath return the profile base path relative to the context.
func (c *Context) ProfilePath() string {
	return filepath.Join(BasePath(), "profiles", c.Profile)
}

// DefaultsFile returns the location of the defaults file for the context.
func (c *Context) DefaultsFile() string {
	return filepath.Join(c.Path(), "config", "defaults.json")
}

// ProfileDefaultsFile returns the location of the defaults file at the base
// of the profile path.
func (c *Context) ProfileDefaultsFile() string {
	return filepath.Join(c.ProfilePath(), "config", "defaults.json")
}

// Load loads the configuration for the given context.
func (c *Context) Load() error {
	c.config = map[string]interface{}{}
	for _, f := range []string{c.DefaultsFile(), c.ProfileDefaultsFile()} {
		b, err := os.ReadFile(f)
		if os.IsNotExist(err) {
			continue
		} else if err != nil {
			return errs.FileError(err, f)
		}

		values := make(map[string]interface{})
		if err := json.Unmarshal(b, &values); err != nil {
			return errors.Wrapf(err, "error parsing %s", f)
		}

		for k, v := range values {
			c.config[k] = v
		}
	}

	attributesBannedFromConfig := []string{
		"context",
		"profile",
		"authority",
	}
	for _, attr := range attributesBannedFromConfig {
		if _, ok := c.config[attr]; ok {
			ui.Printf("cannot set '%s' attribute in config files\n", attr)
			delete(c.config, attr)
		}
	}

	return nil
}

// ContextMap represents the map of available Contexts that is stored
// at the base of the Step Path.
type ContextMap map[string]*Context

type storedCurrent struct {
	Context string `json:"context"`
}

// CtxState is the type that manages context state for the cli.
type CtxState struct {
	current  *Context
	contexts ContextMap
	config   map[string]interface{}
}

var ctxState = &CtxState{}

// Init initializes the context map and current context state.
func (cs *CtxState) Init() error {
	if err := cs.initMap(); err != nil {
		return err
	}
	if err := cs.initCurrent(); err != nil {
		return err
	}
	if err := cs.load(); err != nil {
		return err
	}
	return nil
}

func (cs *CtxState) initMap() error {
	contextsFile := ContextsFile()
	b, err := os.ReadFile(contextsFile)
	if os.IsNotExist(err) {
		return nil
	} else if err != nil {
		return errs.FileError(err, contextsFile)
	}
	cs.contexts = ContextMap{}
	if err := json.Unmarshal(b, &cs.contexts); err != nil {
		return errors.Wrap(err, "error unmarshaling context map")
	}
	for k, ctx := range cs.contexts {
		if err := ctx.Validate(); err != nil {
			return errors.Wrapf(err, "error in context '%s'", k)
		}
		ctx.Name = k
	}
	return nil
}

func (cs *CtxState) initCurrent() error {
	currentCtxFile := CurrentContextFile()
	b, err := os.ReadFile(currentCtxFile)
	if os.IsNotExist(err) {
		return nil
	} else if err != nil {
		return errs.FileError(err, currentCtxFile)
	}

	var sc storedCurrent
	if err := json.Unmarshal(b, &sc); err != nil {
		return errors.Wrap(err, "error unmarshaling current context")
	}

	return cs.SetCurrent(sc.Context)
}

func (cs *CtxState) load() error {
	if cs.Enabled() && cs.GetCurrent() != nil {
		return cs.GetCurrent().Load()
	}
	cs.LoadVintage("")
	return nil
}

// LoadVintage loads context configuration from the vintage (non-context) path.
func (cs *CtxState) LoadVintage(f string) error {
	if f == "" {
		f = DefaultsFile()
	}

	b, err := os.ReadFile(f)
	if os.IsNotExist(err) {
		return nil
	} else if err != nil {
		return errs.FileError(err, f)
	}

	cs.config = make(map[string]interface{})
	if err := json.Unmarshal(b, &cs.config); err != nil {
		return errors.Wrapf(err, "error parsing %s", f)
	}
	return nil
}

// GetConfig returns the current context configuration.
func (cs *CtxState) GetConfig() (map[string]interface{}, error) {
	if cs.Enabled() {
		cur := cs.GetCurrent()
		if cur == nil {
			return nil, errors.New("cannot get context configuration; no current context set")
		}
		return cur.config, nil
	}
	return cs.config, nil
}

// SetCurrent sets the current context or returns an error if a context
// with the given name does not exist.
func (cs *CtxState) SetCurrent(name string) error {
	var ok bool
	cs.current, ok = cs.contexts[name]
	if !ok {
		return errors.Errorf("could not load context '%s'", name)
	}
	if len(cs.current.config) == 0 {
		if err := cs.current.Load(); err != nil {
			return err
		}
	}
	return nil
}

type contextSelect struct {
	Name    string
	Context *Context
}

// PromptContext gets user input to select a context.
func (cs *CtxState) PromptContext() error {
	var items []*contextSelect
	for _, context := range cs.List() {
		items = append(items, &contextSelect{
			Name:    context.Name,
			Context: context,
		})
	}

	var ctxStr string
	if len(items) == 1 {
		if err := ui.PrintSelected("Context", items[0].Name); err != nil {
			return err
		}
		ctxStr = items[0].Name
	} else {
		i, _, err := ui.Select("Select a context for 'step':", items,
			ui.WithSelectTemplates(ui.NamedSelectTemplates("Context")))
		if err != nil {
			return err
		}
		ctxStr = items[i].Name
	}
	if err := cs.SetCurrent(ctxStr); err != nil {
		return err
	}
	return cs.SaveCurrent(ctxStr)
}

// Enabled returns true if one of the following is true:
//   - there is a current context configured
//   - the context map is (list of available contexts) is not empty.
func (cs *CtxState) Enabled() bool {
	return cs.current != nil || len(cs.contexts) > 0
}

// Contexts returns an object that enables context management.
func Contexts() *CtxState {
	return ctxState
}

// Add adds a new context to the context map. If current context is not
// set then store the new context as the current context for future commands.
func (cs *CtxState) Add(ctx *Context) error {
	if err := ctx.Validate(); err != nil {
		return errors.Wrapf(err, "error adding context")
	}
	if cs.contexts == nil {
		cs.contexts = map[string]*Context{ctx.Name: ctx}
	} else {
		cs.contexts[ctx.Name] = ctx
	}

	b, err := json.MarshalIndent(cs.contexts, "", "    ")
	if err != nil {
		return err
	}

	cf := ContextsFile()
	if err := os.MkdirAll(filepath.Dir(cf), 0700); err != nil {
		return errs.FileError(err, cf)
	}
	if err := os.WriteFile(cf, b, 0600); err != nil {
		return errs.FileError(err, cf)
	}

	if cs.current == nil {
		if err := cs.SaveCurrent(ctx.Name); err != nil {
			return err
		}
	}
	return nil
}

// GetCurrent returns the current context.
func (cs *CtxState) GetCurrent() *Context {
	return cs.current
}

// Get returns the context with the given name.
func (cs *CtxState) Get(name string) (ctx *Context, ok bool) {
	ctx, ok = cs.contexts[name]
	return
}

// Remove removes a context from the context state.
func (cs *CtxState) Remove(name string) error {
	if _, ok := cs.contexts[name]; !ok {
		return errors.Errorf("context '%s' not found", name)
	}
	if cs.current != nil && cs.current.Name == name {
		return errors.New("cannot remove current context; use 'step context select' to switch contexts")
	}

	delete(cs.contexts, name)

	b, err := json.MarshalIndent(cs.contexts, "", "    ")
	if err != nil {
		return err
	}

	if err := os.WriteFile(ContextsFile(), b, 0600); err != nil {
		return err
	}
	return nil
}

// List returns a list of all contexts.
func (cs *CtxState) List() []*Context {
	l := make([]*Context, 0, len(cs.contexts))

	for _, v := range cs.contexts {
		l = append(l, v)
	}
	return l
}

// ListAlphabetical returns a case-insensitive, alphabetically
// sorted list of all contexts.
func (cs *CtxState) ListAlphabetical() []*Context {
	l := cs.List()

	sort.Slice(l, func(i, j int) bool {
		return strings.ToLower(l[i].Name) < strings.ToLower(l[j].Name)
	})

	return l
}

// SaveCurrent stores the given context name as the selected default context for
// future commands.
func (cs *CtxState) SaveCurrent(name string) error {
	if _, ok := Contexts().Get(name); !ok {
		return errors.Errorf("context '%s' not found", name)
	}

	b, err := json.Marshal(storedCurrent{Context: name})
	if err != nil {
		return err
	}
	if err = os.WriteFile(CurrentContextFile(), b, 0644); err != nil {
		return errs.FileError(err, CurrentContextFile())
	}
	return nil
}

// Apply the current context configuration to the command line environment.
func (cs *CtxState) Apply(ctx *cli.Context) error {
	cfg, err := cs.GetConfig()
	if err != nil {
		return err
	}
	for _, f := range ctx.Command.Flags {
		// Skip if EnvVar == IgnoreEnvVar
		if getFlagEnvVar(f) == IgnoreEnvVar {
			continue
		}

		for _, name := range strings.Split(f.GetName(), ",") {
			name = strings.TrimSpace(name)
			if ctx.IsSet(name) {
				break
			}
			// Set the flag for the first key that matches.
			if v, ok := cfg[name]; ok {
				ctx.Set(name, fmt.Sprintf("%v", v))
				break
			}
		}
	}
	return nil
}

// getEnvVar generates the environment variable for the given flag name.
func getEnvVar(name string) string {
	parts := strings.Split(name, ",")
	name = strings.TrimSpace(parts[0])
	name = strings.ReplaceAll(name, "-", "_")
	return "STEP_" + strings.ToUpper(name)
}

// getFlagEnvVar returns the value of the EnvVar field of a flag.
func getFlagEnvVar(f cli.Flag) string {
	v := reflect.ValueOf(f)
	if v.Kind() == reflect.Ptr {
		v = v.Elem()
	}
	if v.Kind() == reflect.Struct {
		envVar := v.FieldByName("EnvVar")
		if envVar.IsValid() {
			return envVar.String()
		}
	}
	return ""
}

// SetEnvVar sets the the EnvVar element to each flag recursively.
func SetEnvVar(c *cli.Command) {
	if c == nil {
		return
	}

	// Enable getting the flags from a json file
	if c.Before == nil && c.Action != nil {
		c.Before = getConfigVars
	}

	// Enable getting the flags from environment variables
	for i := range c.Flags {
		envVar := getEnvVar(c.Flags[i].GetName())
		switch f := c.Flags[i].(type) {
		case cli.BoolFlag:
			if f.EnvVar == "" {
				f.EnvVar = envVar
				c.Flags[i] = f
			}
		case cli.BoolTFlag:
			if f.EnvVar == "" {
				f.EnvVar = envVar
				c.Flags[i] = f
			}
		case cli.DurationFlag:
			if f.EnvVar == "" {
				f.EnvVar = envVar
				c.Flags[i] = f
			}
		case cli.Float64Flag:
			if f.EnvVar == "" {
				f.EnvVar = envVar
				c.Flags[i] = f
			}
		case cli.GenericFlag:
			if f.EnvVar == "" {
				f.EnvVar = envVar
				c.Flags[i] = f
			}
		case cli.Int64Flag:
			if f.EnvVar == "" {
				f.EnvVar = envVar
				c.Flags[i] = f
			}
		case cli.IntFlag:
			if f.EnvVar == "" {
				f.EnvVar = envVar
				c.Flags[i] = f
			}
		case cli.IntSliceFlag:
			if f.EnvVar == "" {
				f.EnvVar = envVar
				c.Flags[i] = f
			}
		case cli.Int64SliceFlag:
			if f.EnvVar == "" {
				f.EnvVar = envVar
				c.Flags[i] = f
			}
		case cli.StringFlag:
			if f.EnvVar == "" {
				f.EnvVar = envVar
				c.Flags[i] = f
			}
		case cli.StringSliceFlag:
			if f.EnvVar == "" {
				f.EnvVar = envVar
				c.Flags[i] = f
			}
		case cli.Uint64Flag:
			if f.EnvVar == "" {
				f.EnvVar = envVar
				c.Flags[i] = f
			}
		case cli.UintFlag:
			if f.EnvVar == "" {
				f.EnvVar = envVar
				c.Flags[i] = f
			}
		}
	}

	for i := range c.Subcommands {
		SetEnvVar(&c.Subcommands[i])
	}
}

// GetConfigVars load the defaults.json file and sets the flags if they are not
// already set or the EnvVar is set to IgnoreEnvVar.
//
// TODO(mariano): right now it only supports parameters at first level.
func getConfigVars(ctx *cli.Context) (err error) {
	if ctx.Bool("no-context") {
		return nil
	}

	cs := Contexts()

	// Load the the current context into memory:
	//  - If contexts enabled then make sure a current context is selected
	//    and loaded.
	//  - If vintage context then check if overwritten by --config flag.
	if cs.Enabled() {
		if ctx.IsSet("context") {
			err = cs.SetCurrent(ctx.String("context"))
		} else if cs.Enabled() && cs.GetCurrent() == nil {
			err = cs.PromptContext()
		}
		if err != nil {
			return err
		}
	} else if ctx.GlobalString("config") != "" {
		// Re-load the vintage context configuration if `--config` flag supplied.
		cs.LoadVintage(ctx.GlobalString("config"))
	}

	// TODO: a mock detail because of "add detail/assignee to this TODO/FIXME/BUG comment" lint issue
	fullCommandName := strings.ToLower(strings.TrimSpace(ctx.Command.FullName()))
	if strings.EqualFold(fullCommandName, "ca bootstrap-helper") {
		return nil
	}

	if err := cs.Apply(ctx); err != nil {
		return err
	}

	return nil
}