-
Notifications
You must be signed in to change notification settings - Fork 9
/
set.go
75 lines (63 loc) · 2.58 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
// 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"
"namespacelabs.dev/foundation/framework/secrets/localsecrets"
"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/schema"
)
func newSetCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "set --secret {package_name}:{name} [--from_file <path>] [server]",
Short: "Sets the specified secret value.",
Args: cobra.MaximumNArgs(1),
}
secretKey := cmd.Flags().String("secret", "", "The secret key, in {package_name}:{name} format.")
specificEnv := cmd.Flags().String("env", "", "If set, matches specified secret with the named environment (e.g. dev, or prod).")
keyID := cmd.Flags().String("key", "", "Use this specific key identity when creating a new bundle.")
fromFile := cmd.Flags().String("from_file", "", "Load the file contents as the secret value.")
rawtext := cmd.Flags().Bool("rawtext", false, "If set to true, the bundle is not encrypted (use for testing purposes only).")
_ = cmd.MarkFlagRequired("secret")
env := fncobra.EnvFromValue(cmd, specificEnv)
locs := fncobra.LocationsFromArgs(cmd, env)
loc, bundle := bundleFromArgs(cmd, env, locs, func(ctx context.Context) (*localsecrets.Bundle, error) {
return localsecrets.NewBundle(ctx, *keyID)
})
return fncobra.With(cmd, func(ctx context.Context) error {
key, err := parseKey(*secretKey)
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)
})
}