Skip to content

Commit

Permalink
Initial port of secrets to new CLI command chain
Browse files Browse the repository at this point in the history
  • Loading branch information
jsierles committed Jul 15, 2022
1 parent a6e61fa commit d0f460c
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
27 changes: 27 additions & 0 deletions internal/command/secrets/secrets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package secrets

import (
"github.com/superfly/flyctl/internal/command"

"github.com/spf13/cobra"
)

func New() *cobra.Command {

const (
long = `Secrets are provided to applications at runtime as ENV variables. Names are
case sensitive and stored as-is, so ensure names are appropriate for
the application and vm environment.
`

short = "Manage application secrets with the set and unset commands."
)

secrets := command.New("secrets", short, long, nil)

secrets.AddCommand(
newSet(),
)

return secrets
}
78 changes: 78 additions & 0 deletions internal/command/secrets/set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package secrets

import (
"context"
"errors"
"flag"
"fmt"

"github.com/spf13/cobra"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/helpers"
"github.com/superfly/flyctl/internal/app"
"github.com/superfly/flyctl/internal/cmdutil"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/watch"
"github.com/superfly/flyctl/iostreams"
)

func newSet() (cmd *cobra.Command) {
const (
long = `"Set one or more encrypted secrets for an application",`
short = long
usage = "set [flags] NAME=VALUE NAME=VALUE ..."
)

cmd = command.New(usage, short, long, runSet, command.RequireSession)

cmd.Args = cobra.MinimumNArgs(1)

return cmd
}

func runSet(ctx context.Context) (err error) {
client := client.FromContext(ctx).API()
appName := app.NameFromContext(ctx)
out := iostreams.FromContext(ctx).Out
app, err := client.GetAppCompact(ctx, appName)

if err != nil {
return err
}

secrets, err := cmdutil.ParseKVStringsToMap(flag.Args())

for k, v := range secrets {
if v == "-" {
if !helpers.HasPipedStdin() {
return fmt.Errorf("Secret `%s` expects standard input but none provided", k)
}
inval, err := helpers.ReadStdin(64 * 1024)
if err != nil {
return fmt.Errorf("Error reading stdin for '%s': %s", k, err)
}
secrets[k] = inval
}
}

if len(secrets) < 1 {
return errors.New("requires at least one SECRET=VALUE pair")
}

release, err := client.SetSecrets(ctx, appName, secrets)

if err != nil {
return err
}

if !app.Deployed {
fmt.Fprint(out, "Secrets are staged for the first deployment")
return nil
}

fmt.Fprintf(out, "Release v%d created\n", release.Version)

err = watch.Deployment(ctx, release.EvaluationID)

return err
}

0 comments on commit d0f460c

Please sign in to comment.