0
0
forked from toolshed/abra

Prompt for secrets if not provided on CLI

Re #646
This commit is contained in:
3wc
2025-08-25 17:16:50 -04:00
parent f39eab8f1e
commit 238647a987
2 changed files with 35 additions and 7 deletions

View File

@ -15,6 +15,7 @@ import (
"coopcloud.tech/abra/pkg/i18n"
"coopcloud.tech/abra/pkg/log"
"coopcloud.tech/abra/pkg/secret"
"github.com/AlecAivazis/survey/v2"
"github.com/docker/docker/api/types"
dockerClient "github.com/docker/docker/client"
"github.com/spf13/cobra"
@ -28,7 +29,8 @@ var AppSecretGenerateCommand = &cobra.Command{
ValidArgsFunction: func(
cmd *cobra.Command,
args []string,
toComplete string) ([]string, cobra.ShellCompDirective) {
toComplete string,
) ([]string, cobra.ShellCompDirective) {
switch l := len(args); l {
case 0:
return autocomplete.AppNameComplete()
@ -140,7 +142,7 @@ var AppSecretGenerateCommand = &cobra.Command{
}
var AppSecretInsertCommand = &cobra.Command{
Use: i18n.G("insert <domain> <secret> <version> <data> [flags]"),
Use: i18n.G("insert <domain> <secret> <version> [<data>] [flags]"),
Aliases: []string{i18n.G("i")},
Short: i18n.G("Insert secret"),
Long: i18n.G(`This command inserts a secret into an app environment.
@ -156,11 +158,12 @@ environment. Typically, you can let Abra generate them for you on app creation
# insert secret as file
abra app secret insert 1312.net my_secret v1 secret.txt -f`),
Args: cobra.MinimumNArgs(4),
Args: cobra.MinimumNArgs(3),
ValidArgsFunction: func(
cmd *cobra.Command,
args []string,
toComplete string) ([]string, cobra.ShellCompDirective) {
toComplete string,
) ([]string, cobra.ShellCompDirective) {
switch l := len(args); l {
case 0:
return autocomplete.AppNameComplete()
@ -188,7 +191,13 @@ environment. Typically, you can let Abra generate them for you on app creation
name := args[1]
version := args[2]
data := args[3]
data := ""
if len(args) > 3 {
data = args[3]
} else if internal.NoInput {
log.Fatal("Must provide <data> argument if --no-input is passed")
}
composeFiles, err := app.Recipe.GetComposeFiles(app.Env)
if err != nil {
@ -210,6 +219,20 @@ environment. Typically, you can let Abra generate them for you on app creation
log.Fatal(i18n.G("no secret %s available for recipe %s?", name, app.Recipe.Name))
}
if data == "" && !internal.NoInput {
log.Debug("Secret data not provided on command-line, prompting")
message := "Specify secret value"
if insertFromFile {
message = "Specify secret file"
}
prompt := &survey.Input{
Message: i18n.G(message),
}
if err := survey.AskOne(prompt, &data); err != nil {
log.Fatal(err)
}
}
if insertFromFile {
raw, err := os.ReadFile(data)
if err != nil {
@ -269,7 +292,8 @@ match those configured in the recipe beforehand.`),
ValidArgsFunction: func(
cmd *cobra.Command,
args []string,
toComplete string) ([]string, cobra.ShellCompDirective) {
toComplete string,
) ([]string, cobra.ShellCompDirective) {
switch l := len(args); l {
case 0:
return autocomplete.AppNameComplete()
@ -376,7 +400,8 @@ var AppSecretLsCommand = &cobra.Command{
ValidArgsFunction: func(
cmd *cobra.Command,
args []string,
toComplete string) ([]string, cobra.ShellCompDirective) {
toComplete string,
) ([]string, cobra.ShellCompDirective) {
return autocomplete.AppNameComplete()
},
Run: func(cmd *cobra.Command, args []string) {

View File

@ -208,6 +208,9 @@ teardown(){
run $ABRA app secret insert "$TEST_APP_DOMAIN" bar baz
assert_failure
run $ABRA app secret insert "$TEST_APP_DOMAIN" test_pass_one v1 -n
assert_failure
}
@test "insert: cannot insert unknown secret" {