Skip to content
This repository was archived by the owner on Feb 16, 2023. It is now read-only.

Commit 40bf721

Browse files
committed
Add env ls command
1 parent 1b26c30 commit 40bf721

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

internals/secrethub/env.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ func NewEnvCommand(io ui.IO, newClient newClientFunc) *EnvCommand {
2323
func (cmd *EnvCommand) Register(r command.Registerer) {
2424
clause := r.Command("env", "Manage environment variables.").Hidden() // The command is hidden, because it's still in beta.
2525
NewEnvReadCommand(cmd.io, cmd.newClient).Register(clause)
26+
NewEnvListCommand(cmd.io).Register(clause)
2627
}

internals/secrethub/env_ls.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package secrethub
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/secrethub/secrethub-cli/internals/cli/ui"
7+
"github.com/secrethub/secrethub-cli/internals/secrethub/command"
8+
)
9+
10+
// EnvListCommand is a command to list all environment variable keys set in the process of `secrethub run`.
11+
type EnvListCommand struct {
12+
io ui.IO
13+
environment *environment
14+
}
15+
16+
// NewEnvListCommand creates a new EnvListCommand.
17+
func NewEnvListCommand(io ui.IO) *EnvListCommand {
18+
return &EnvListCommand{
19+
io: io,
20+
environment: newEnvironment(io),
21+
}
22+
}
23+
24+
// Register adds a CommandClause and it's args and flags to a Registerer.
25+
func (cmd *EnvListCommand) Register(r command.Registerer) {
26+
clause := r.Command("ls", "Read the value of a single environment variable.")
27+
clause.Alias("list")
28+
29+
cmd.environment.register(clause)
30+
31+
command.BindAction(clause, cmd.Run)
32+
}
33+
34+
// Run handles the command with the options as specified in the command.
35+
func (cmd *EnvListCommand) Run() error {
36+
env, err := cmd.environment.env()
37+
if err != nil {
38+
return err
39+
}
40+
41+
for key, value := range env {
42+
// For now only environment variables in which a secret is loaded are printed.
43+
// TODO: Make this behavior configurable.
44+
if value.containsSecret() {
45+
fmt.Fprintln(cmd.io.Stdout(), key)
46+
}
47+
}
48+
49+
return nil
50+
}

0 commit comments

Comments
 (0)