Prompt for secret commands

Co-authored-by: William Martin <williammartin@github.com>
This commit is contained in:
Wing 2024-12-04 15:27:01 +01:00 committed by William Martin
parent 57c9ee0ad2
commit 3de2fd94b3
3 changed files with 64 additions and 6 deletions

View file

@ -4,6 +4,7 @@ import (
"bytes"
"encoding/base64"
"fmt"
"github.com/cli/cli/v2/internal/prompter"
"io"
"net/http"
"os"
@ -29,7 +30,7 @@ type SetOptions struct {
Config func() (gh.Config, error)
BaseRepo func() (ghrepo.Interface, error)
Remotes func() (ghContext.Remotes, error)
Prompter iprompter
Prompter prompter.Prompter
RandomOverride func() io.Reader
@ -45,10 +46,7 @@ type SetOptions struct {
Application string
HasRepoOverride bool
}
type iprompter interface {
Password(string) (string, error)
Interactive bool
}
func NewCmdSet(f *cmdutil.Factory, runF func(*SetOptions) error) *cobra.Command {
@ -82,6 +80,9 @@ func NewCmdSet(f *cmdutil.Factory, runF func(*SetOptions) error) *cobra.Command
# Read secret value from an environment variable
$ gh secret set MYSECRET --body "$ENV_VALUE"
# Set secret for a specific remote repository
$ gh secret set MYSECRET --repo origin/repo --body "$ENV_VALUE"
# Read secret value from a file
$ gh secret set MYSECRET < myfile.txt
@ -111,6 +112,8 @@ func NewCmdSet(f *cmdutil.Factory, runF func(*SetOptions) error) *cobra.Command
// support `-R, --repo` override
opts.BaseRepo = f.BaseRepo
flagCount := cmdutil.CountSetFlags(cmd.Flags())
if err := cmdutil.MutuallyExclusive("specify only one of `--org`, `--env`, or `--user`", opts.OrgName != "", opts.EnvName != "", opts.UserSecrets); err != nil {
return err
}
@ -129,6 +132,10 @@ func NewCmdSet(f *cmdutil.Factory, runF func(*SetOptions) error) *cobra.Command
}
} else {
opts.SecretName = args[0]
if flagCount == 0 {
opts.Interactive = true
}
}
if cmd.Flags().Changed("visibility") {
@ -197,7 +204,17 @@ func setRun(opts *SetOptions) error {
err = cmdutil.ValidateHasOnlyOneRemote(opts.HasRepoOverride, opts.Remotes)
if err != nil {
return err
if opts.Interactive {
selectedRepo, errSelectedRepo := cmdutil.PromptForRepo(baseRepo, opts.Remotes, opts.Prompter)
if errSelectedRepo != nil {
return errSelectedRepo
}
baseRepo = selectedRepo
} else {
return err
}
}
host = baseRepo.RepoHost()

View file

@ -180,3 +180,11 @@ func isIncluded(value string, opts []string) bool {
}
return false
}
func CountSetFlags(flags *pflag.FlagSet) int {
count := 0
flags.Visit(func(f *pflag.Flag) {
count++
})
return count
}

View file

@ -1,11 +1,13 @@
package cmdutil
import (
ghContext "github.com/cli/cli/v2/context"
"os"
"sort"
"strings"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/internal/prompter"
"github.com/spf13/cobra"
)
@ -68,3 +70,34 @@ func OverrideBaseRepoFunc(f *Factory, override string) func() (ghrepo.Interface,
}
return f.BaseRepo
}
func PromptForRepo(baseRepo ghrepo.Interface, remotes func() (ghContext.Remotes, error), survey prompter.Prompter) (ghrepo.Interface, error) {
var defaultRepo string
var remoteArray []string
if remotes, _ := remotes(); remotes != nil {
if defaultRemote, _ := remotes.ResolvedRemote(); defaultRemote != nil {
// this is a remote explicitly chosen via `repo set-default`
defaultRepo = ghrepo.FullName(defaultRemote)
} else if len(remotes) > 0 {
// as a fallback, just pick the first remote
defaultRepo = ghrepo.FullName(remotes[0])
}
for _, remote := range remotes {
remoteArray = append(remoteArray, ghrepo.FullName(remote))
}
}
baseRepoInput, errInput := survey.Select("Select a base repo", defaultRepo, remoteArray)
if errInput != nil {
return baseRepo, errInput
}
selectedRepo, errSelectedRepo := ghrepo.FromFullName(remoteArray[baseRepoInput])
if errSelectedRepo != nil {
return baseRepo, errSelectedRepo
}
return selectedRepo, nil
}