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

Commit

Permalink
Merge pull request #255 from secrethub/feature/refactor-run-secret-so…
Browse files Browse the repository at this point in the history
…urcing

Add `env read` and `env ls` commands
  • Loading branch information
SimonBarendse authored Mar 2, 2020
2 parents e2fe650 + fdb7455 commit 1d29286
Show file tree
Hide file tree
Showing 11 changed files with 946 additions and 643 deletions.
1 change: 1 addition & 0 deletions internals/secrethub/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ func (app *App) registerCommands() {
NewAccountCommand(app.io, app.clientFactory.NewClient, app.credentialStore).Register(app.cli)
NewCredentialCommand(app.io, app.clientFactory, app.credentialStore).Register(app.cli)
NewConfigCommand(app.io, app.credentialStore).Register(app.cli)
NewEnvCommand(app.io, app.clientFactory.NewClient).Register(app.cli)

// Commands
NewInitCommand(app.io, app.clientFactory.NewUnauthenticatedClient, app.clientFactory.NewClientWithCredentials, app.credentialStore).Register(app.cli)
Expand Down
28 changes: 28 additions & 0 deletions internals/secrethub/env.go
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)
}
51 changes: 51 additions & 0 deletions internals/secrethub/env_ls.go
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
}
60 changes: 60 additions & 0 deletions internals/secrethub/env_read.go
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
}
Loading

0 comments on commit 1d29286

Please sign in to comment.