Prompt for secrets if not provided on CLI #616

Merged
3wordchant merged 3 commits from calix/646 into main 2025-08-26 17:15:48 +00:00
2 changed files with 38 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(i18n.G("must provide <data> argument if --no-input is passed"))
3wordchant marked this conversation as resolved Outdated

Gotta do the i18n.G(...) string wrapping translation dance 🙃

Also, convention for logging is usually no capitalisation / lower-case 🤷

Gotta do the `i18n.G(...)` string wrapping translation dance 🙃 Also, convention for logging is usually no capitalisation / lower-case 🤷
}
composeFiles, err := app.Recipe.GetComposeFiles(app.Env)
if err != nil {
@ -210,6 +219,23 @@ 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(i18n.G("secret data not provided on command-line, prompting"))
3wordchant marked this conversation as resolved Outdated

i18n.G / capitalisaion 👁‍🗨

`i18n.G` / capitalisaion 👁‍🗨
var prompt survey.Prompt
if !insertFromFile {
prompt = &survey.Password{
Message: i18n.G("specify secret value"),
3wordchant marked this conversation as resolved Outdated

i18n.G / capitalisaion 👁‍🗨

`i18n.G` / capitalisaion 👁‍🗨
}
3wordchant marked this conversation as resolved Outdated
[`survey.Password`](https://github.com/AlecAivazis/survey?tab=readme-ov-file#password)
} else {
prompt = &survey.Input{
Message: i18n.G("specify secret file"),
3wordchant marked this conversation as resolved Outdated

i18n.G / capitalisaion 👁‍🗨

`i18n.G` / capitalisaion 👁‍🗨
}
}
if err := survey.AskOne(prompt, &data); err != nil {
log.Fatal(err)
}
}
if insertFromFile {
raw, err := os.ReadFile(data)
if err != nil {
@ -269,7 +295,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 +403,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" {