This repository has been archived by the owner on Feb 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
env_read.go
62 lines (49 loc) · 1.55 KB
/
env_read.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
package secrethub
import (
"fmt"
"github.com/secrethub/secrethub-cli/internals/cli"
"github.com/secrethub/secrethub-cli/internals/cli/ui"
)
// EnvReadCommand is a command to read the value of a single environment variable.
type EnvReadCommand struct {
io ui.IO
newClient newClientFunc
environment *environment
key cli.StringValue
}
// NewEnvReadCommand creates a new EnvReadCommand.
func NewEnvReadCommand(io ui.IO, newClient newClientFunc) *EnvReadCommand {
return &EnvReadCommand{
io: io,
newClient: newClient,
environment: newEnvironment(io, newClient),
}
}
// Register adds a CommandClause and it's args and flags to a Registerer.
func (cmd *EnvReadCommand) Register(r cli.Registerer) {
clause := r.Command("read", "[BETA] Read the value of a single environment variable.")
clause.HelpLong("This command is hidden because it is still in beta. Future versions may break.")
cmd.environment.register(clause)
clause.BindAction(cmd.Run)
clause.BindArguments([]cli.Argument{
{Value: &cmd.key, Name: "key", Required: false, Description: "the key of the environment variable to read."},
})
}
// Run executes the command.
func (cmd *EnvReadCommand) Run() error {
env, err := cmd.environment.env()
if err != nil {
return err
}
value, found := env[cmd.key.Value]
if !found {
return fmt.Errorf("no environment variable with that key is set")
}
secretReader := newSecretReader(cmd.newClient)
res, err := value.resolve(secretReader)
if err != nil {
return err
}
fmt.Fprintln(cmd.io.Output(), res)
return nil
}