Move secret repo validation into secrets subpackage
This commit is contained in:
parent
df8bb51c9c
commit
73244c010e
6 changed files with 59 additions and 53 deletions
|
|
@ -111,7 +111,7 @@ func removeRun(opts *DeleteOptions) error {
|
|||
return err
|
||||
}
|
||||
|
||||
err = cmdutil.ValidateHasOnlyOneRemote(opts.HasRepoOverride, opts.Remotes)
|
||||
err = shared.ValidateHasOnlyOneRemote(opts.HasRepoOverride, opts.Remotes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -109,7 +109,7 @@ func listRun(opts *ListOptions) error {
|
|||
return err
|
||||
}
|
||||
|
||||
err = cmdutil.ValidateHasOnlyOneRemote(opts.HasRepoOverride, opts.Remotes)
|
||||
err = shared.ValidateHasOnlyOneRemote(opts.HasRepoOverride, opts.Remotes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -198,12 +198,12 @@ func setRun(opts *SetOptions) error {
|
|||
return err
|
||||
}
|
||||
|
||||
if err = cmdutil.ValidateHasOnlyOneRemote(opts.HasRepoOverride, opts.Remotes); err != nil {
|
||||
if err = shared.ValidateHasOnlyOneRemote(opts.HasRepoOverride, opts.Remotes); err != nil {
|
||||
if !opts.CanPrompt {
|
||||
return err
|
||||
}
|
||||
|
||||
selectedRepo, errSelectingRepo := cmdutil.PromptForRepo(baseRepo, opts.Remotes, opts.Prompter)
|
||||
selectedRepo, errSelectingRepo := shared.PromptForRepo(baseRepo, opts.Remotes, opts.Prompter)
|
||||
if errSelectingRepo != nil {
|
||||
return errSelectingRepo
|
||||
}
|
||||
|
|
|
|||
55
pkg/cmd/secret/shared/base_repo.go
Normal file
55
pkg/cmd/secret/shared/base_repo.go
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
package shared
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
ghContext "github.com/cli/cli/v2/context"
|
||||
"github.com/cli/cli/v2/internal/ghrepo"
|
||||
"github.com/cli/cli/v2/internal/prompter"
|
||||
)
|
||||
|
||||
func ValidateHasOnlyOneRemote(hasRepoOverride bool, remotes func() (ghContext.Remotes, error)) error {
|
||||
if !hasRepoOverride && remotes != nil {
|
||||
remotes, err := remotes()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if remotes.Len() > 1 {
|
||||
return fmt.Errorf("multiple remotes detected %v. please specify which repo to use by providing the -R or --repo argument", remotes)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
|
@ -5,7 +5,6 @@ import (
|
|||
"fmt"
|
||||
|
||||
"github.com/AlecAivazis/survey/v2/terminal"
|
||||
ghContext "github.com/cli/cli/v2/context"
|
||||
)
|
||||
|
||||
// FlagErrorf returns a new FlagError that wraps an error produced by
|
||||
|
|
@ -58,21 +57,6 @@ func MutuallyExclusive(message string, conditions ...bool) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func ValidateHasOnlyOneRemote(hasRepoOverride bool, remotes func() (ghContext.Remotes, error)) error {
|
||||
if !hasRepoOverride && remotes != nil {
|
||||
remotes, err := remotes()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if remotes.Len() > 1 {
|
||||
return fmt.Errorf("multiple remotes detected %v. please specify which repo to use by providing the -R or --repo argument", remotes)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type NoResultsError struct {
|
||||
message string
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,11 @@
|
|||
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"
|
||||
)
|
||||
|
||||
|
|
@ -70,34 +68,3 @@ 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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue