-
Notifications
You must be signed in to change notification settings - Fork 9
/
set.go
99 lines (87 loc) · 3.26 KB
/
set.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package secrets
import (
"context"
"fmt"
"os"
"github.com/charmbracelet/lipgloss"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"namespacelabs.dev/foundation/internal/cli/fncobra"
"namespacelabs.dev/foundation/internal/console/tui"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/parsing"
"namespacelabs.dev/foundation/internal/secrets"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/cfg"
)
func newSetCmd() *cobra.Command {
var (
locLoadingEnv cfg.Context
secretKey, keyID, fromFile, specificEnv string
rawtext bool
locs fncobra.Locations
)
return fncobra.
Cmd(&cobra.Command{
Use: "set {path/to/server} --secret {package_name}:{name} [--from_file <path>]",
Short: "Sets the specified secret value.",
Args: cobra.MaximumNArgs(1),
}).
WithFlags(func(flags *pflag.FlagSet) {
flags.StringVar(&secretKey, "secret", "", "The secret key, in {package_name}:{name} format.")
flags.StringVar(&keyID, "key", "", "Use this specific key identity when creating a new bundle.")
flags.StringVar(&fromFile, "from_file", "", "Load the file contents as the secret value.")
flags.BoolVar(&rawtext, "rawtext", rawtext, "If set to true, the bundle is not encrypted (use for testing purposes only).")
flags.StringVar(&specificEnv, "env", "", "If set, only sets the specified secret for the named environment (e.g. dev, or prod).")
_ = cobra.MarkFlagRequired(flags, "secret")
}).
With(
fncobra.HardcodeEnv(&locLoadingEnv, "dev"),
fncobra.ParseLocations(&locs, &locLoadingEnv, fncobra.ParseLocationsOpts{RequireSingle: true})).
Do(func(ctx context.Context) error {
envStr := specificEnv
if envStr == "" {
// Need some env for package loading.
envStr = "dev"
}
env, err := cfg.LoadContext(locs.Root, envStr)
if err != nil {
return err
}
loc, bundle, err := loadBundleFromArgs(ctx, env, locs.Locs[0], func(ctx context.Context) (*secrets.Bundle, error) {
return secrets.NewBundle(ctx, keyID)
})
if err != nil {
return err
}
key, err := parseKey(secretKey, string(loc.loc.PackageName))
if err != nil {
return err
}
key.EnvironmentName = specificEnv
if _, err := parsing.NewPackageLoader(env).LoadByName(ctx, schema.PackageName(key.PackageName)); err != nil {
return err
}
var value []byte
if fromFile != "" {
value, err = os.ReadFile(fromFile)
if err != nil {
return fnerrors.BadInputError("%s: failed to load: %w", fromFile, err)
}
} else {
valueStr, err := tui.Ask(ctx, "Set a new secret value", fmt.Sprintf("Package: %s\nKey: %q\n\n%s", key.PackageName, key.Key, lipgloss.NewStyle().Faint(true).Render("Note: for multi-line input, use the --from_file flag.")), "Value")
if err != nil {
return err
}
if valueStr == "" {
return fnerrors.New("no value provided, skipping")
}
value = []byte(valueStr)
}
bundle.Set(key, value)
return writeBundle(ctx, loc, bundle, !rawtext)
})
}