This repository has been archived by the owner on Feb 16, 2023. It is now read-only.
/
set.go
101 lines (81 loc) · 2.64 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
100
101
package secrethub
import (
"fmt"
"os"
"github.com/secrethub/secrethub-go/internals/api"
"github.com/secrethub/secrethub-cli/internals/cli"
"github.com/secrethub/secrethub-cli/internals/cli/ui"
"github.com/secrethub/secrethub-cli/internals/secretspec"
)
// Errors
var (
ErrFileNotFound = errMain.Code("file_not_found").ErrorPref("configuration file `%s` does not exist")
ErrCannotReadFile = errMain.Code("cannot_read_file").ErrorPref("cannot read file at %s: %v")
ErrSecretsNotCleared = errMain.Code("secrets_not_cleared").Error("exiting without having cleared all secrets")
ErrNoSourcesInSpec = errMain.Code("no_sources_in_spec").Error("cannot find any sources in the .yml spec file")
)
// SetCommand parses a secret spec file and presents secrets on the system.
type SetCommand struct {
in string
io ui.IO
newClient newClientFunc
}
// NewSetCommand creates a new SetCommand.
func NewSetCommand(io ui.IO, newClient newClientFunc) *SetCommand {
return &SetCommand{
io: io,
newClient: newClient,
}
}
// Register registers the command, arguments and flags on the provided Registerer.
func (cmd *SetCommand) Register(r cli.Registerer) {
clause := r.Command("set", "Set the secrets in your local environment. This reads and parses the secrets.yml file in the current working directory.").Hidden()
clause.Flags().StringVarP(&cmd.in, "in", "i", "secrets.yml", "The path to a secrets.yml file to read")
clause.BindAction(cmd.Run)
clause.BindArguments(nil)
}
// Run parses a secret spec file and presents secrets on the system.
func (cmd *SetCommand) Run() error {
presenter, err := secretspec.NewPresenter("", true, secretspec.DefaultParsers...)
if err != nil {
return err
}
_, err = os.Stat(cmd.in)
if os.IsNotExist(err) {
return ErrFileNotFound(cmd.in)
}
spec, err := os.ReadFile(cmd.in)
if err != nil {
return ErrCannotReadFile(cmd.in, err)
}
err = presenter.Parse(spec)
if err != nil {
return err
}
client, err := cmd.newClient()
if err != nil {
return err
}
paths := presenter.Sources()
if len(paths) == 0 {
return ErrNoSourcesInSpec
}
for _, c := range presenter.EmptyConsumables() {
fmt.Fprintf(cmd.io.Output(), "Warning: %s contains no secret declarations.\n", c)
}
secrets := make(map[string]api.SecretVersion)
for path := range paths {
secret, err := client.Secrets().Versions().GetWithData(path)
if err != nil {
return err
}
secrets[path] = *secret
}
fmt.Fprintln(cmd.io.Output(), "Setting secrets...")
err = presenter.Set(secrets)
if err != nil {
return err
}
fmt.Fprintln(cmd.io.Output(), "Set complete! The secrets are now available on your system.")
return nil
}