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 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #255 from secrethub/feature/refactor-run-secret-so…
…urcing Add `env read` and `env ls` commands
- Loading branch information
Showing
11 changed files
with
946 additions
and
643 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package secrethub | ||
|
||
import ( | ||
"github.com/secrethub/secrethub-cli/internals/cli/ui" | ||
"github.com/secrethub/secrethub-cli/internals/secrethub/command" | ||
) | ||
|
||
// EnvCommand handles operations regarding environment variables. | ||
type EnvCommand struct { | ||
io ui.IO | ||
newClient newClientFunc | ||
} | ||
|
||
// NewEnvCommand creates a new EnvCommand. | ||
func NewEnvCommand(io ui.IO, newClient newClientFunc) *EnvCommand { | ||
return &EnvCommand{ | ||
io: io, | ||
newClient: newClient, | ||
} | ||
} | ||
|
||
// Register registers the command and its sub-commands on the provided Registerer. | ||
func (cmd *EnvCommand) Register(r command.Registerer) { | ||
clause := r.Command("env", "[BETA] Manage environment variables.").Hidden() | ||
clause.HelpLong("This command is hidden because it is still in beta. Future versions may break.") | ||
NewEnvReadCommand(cmd.io, cmd.newClient).Register(clause) | ||
NewEnvListCommand(cmd.io).Register(clause) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package secrethub | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/secrethub/secrethub-cli/internals/cli/ui" | ||
"github.com/secrethub/secrethub-cli/internals/secrethub/command" | ||
) | ||
|
||
// EnvListCommand is a command to list all environment variable keys set in the process of `secrethub run`. | ||
type EnvListCommand struct { | ||
io ui.IO | ||
environment *environment | ||
} | ||
|
||
// NewEnvListCommand creates a new EnvListCommand. | ||
func NewEnvListCommand(io ui.IO) *EnvListCommand { | ||
return &EnvListCommand{ | ||
io: io, | ||
environment: newEnvironment(io), | ||
} | ||
} | ||
|
||
// Register adds a CommandClause and it's args and flags to a Registerer. | ||
func (cmd *EnvListCommand) Register(r command.Registerer) { | ||
clause := r.Command("ls", "[BETA] List environment variable names that will be populated with secrets.") | ||
clause.HelpLong("This command is hidden because it is still in beta. Future versions may break.") | ||
clause.Alias("list") | ||
|
||
cmd.environment.register(clause) | ||
|
||
command.BindAction(clause, cmd.Run) | ||
} | ||
|
||
// Run executes the command. | ||
func (cmd *EnvListCommand) Run() error { | ||
env, err := cmd.environment.env() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for key, value := range env { | ||
// For now only environment variables in which a secret is loaded are printed. | ||
// TODO: Make this behavior configurable. | ||
if value.containsSecret() { | ||
fmt.Fprintln(cmd.io.Stdout(), key) | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package secrethub | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/secrethub/secrethub-cli/internals/cli/ui" | ||
"github.com/secrethub/secrethub-cli/internals/secrethub/command" | ||
) | ||
|
||
// EnvReadCommand is a command to read the value of a single environment variable. | ||
type EnvReadCommand struct { | ||
io ui.IO | ||
newClient newClientFunc | ||
environment *environment | ||
key string | ||
} | ||
|
||
// NewEnvReadCommand creates a new EnvReadCommand. | ||
func NewEnvReadCommand(io ui.IO, newClient newClientFunc) *EnvReadCommand { | ||
return &EnvReadCommand{ | ||
io: io, | ||
newClient: newClient, | ||
environment: newEnvironment(io), | ||
} | ||
} | ||
|
||
// Register adds a CommandClause and it's args and flags to a Registerer. | ||
func (cmd *EnvReadCommand) Register(r command.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.") | ||
clause.Arg("key", "the key of the environment variable to read").StringVar(&cmd.key) | ||
|
||
cmd.environment.register(clause) | ||
|
||
command.BindAction(clause, cmd.Run) | ||
} | ||
|
||
// Run executes the command. | ||
func (cmd *EnvReadCommand) Run() error { | ||
env, err := cmd.environment.env() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
value, found := env[cmd.key] | ||
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.Stdout(), res) | ||
|
||
return nil | ||
} |
Oops, something went wrong.