-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial port of secrets to new CLI command chain
- Loading branch information
Showing
2 changed files
with
105 additions
and
0 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
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 | ||
} |
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,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 | ||
} |