Move fetching configured helper into gitcredentials
This commit is contained in:
parent
a3d65e0dce
commit
af589aa2f3
6 changed files with 27 additions and 33 deletions
|
|
@ -176,7 +176,7 @@ func refreshRun(opts *RefreshOptions) error {
|
|||
credentialFlow := &shared.GitCredentialFlow{
|
||||
Prompter: opts.Prompter,
|
||||
GitClient: opts.GitClient,
|
||||
HelperConfigurer: &gitcredentials.HelperConfigurer{
|
||||
HelperConfig: &gitcredentials.HelperConfig{
|
||||
SelfExecutablePath: opts.MainExecutable,
|
||||
GitClient: opts.GitClient,
|
||||
},
|
||||
|
|
|
|||
|
|
@ -55,7 +55,7 @@ func NewCmdSetupGit(f *cmdutil.Factory, runF func(*SetupGitOptions) error) *cobr
|
|||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
opts.gitConfigure = &shared.GitCredentialFlow{
|
||||
GitClient: f.GitClient,
|
||||
HelperConfigurer: &gitcredentials.HelperConfigurer{
|
||||
HelperConfig: &gitcredentials.HelperConfig{
|
||||
SelfExecutablePath: f.Executable(),
|
||||
GitClient: f.GitClient,
|
||||
},
|
||||
|
|
|
|||
|
|
@ -1,14 +1,11 @@
|
|||
package shared
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/cli/cli/v2/git"
|
||||
"github.com/cli/cli/v2/internal/ghinstance"
|
||||
"github.com/cli/cli/v2/pkg/cmd/auth/shared/gitcredentials"
|
||||
"github.com/google/shlex"
|
||||
)
|
||||
|
|
@ -17,8 +14,8 @@ type GitCredentialFlow struct {
|
|||
Prompter Prompt
|
||||
GitClient *git.Client
|
||||
|
||||
HelperConfigurer *gitcredentials.HelperConfigurer
|
||||
Updater *gitcredentials.Updater
|
||||
HelperConfig *gitcredentials.HelperConfig
|
||||
Updater *gitcredentials.Updater
|
||||
|
||||
shouldSetup bool
|
||||
helper string
|
||||
|
|
@ -27,7 +24,7 @@ type GitCredentialFlow struct {
|
|||
|
||||
func (flow *GitCredentialFlow) Prompt(hostname string) error {
|
||||
var gitErr error
|
||||
flow.helper, gitErr = gitCredentialHelper(flow.GitClient, hostname)
|
||||
flow.helper, gitErr = flow.HelperConfig.ConfiguredHelper(hostname)
|
||||
if isOurCredentialHelper(flow.helper) {
|
||||
flow.scopes = append(flow.scopes, "workflow")
|
||||
return nil
|
||||
|
|
@ -65,7 +62,7 @@ func (flow *GitCredentialFlow) gitCredentialSetup(hostname, username, password s
|
|||
// If there is no credential helper configured then we will set ourselves up as
|
||||
// the credential helper for this host.
|
||||
if flow.helper == "" {
|
||||
return flow.HelperConfigurer.Configure(hostname)
|
||||
return flow.HelperConfig.Configure(hostname)
|
||||
}
|
||||
|
||||
// Otherwise, we'll tell git to inform the existing credential helper of the new credentials.
|
||||
|
|
@ -92,19 +89,3 @@ func isGitMissing(err error) bool {
|
|||
var errNotInstalled *git.NotInstalled
|
||||
return errors.As(err, &errNotInstalled)
|
||||
}
|
||||
|
||||
func gitCredentialHelperKey(hostname string) string {
|
||||
host := strings.TrimSuffix(ghinstance.HostPrefix(hostname), "/")
|
||||
return fmt.Sprintf("credential.%s.helper", host)
|
||||
}
|
||||
|
||||
func gitCredentialHelper(gitClient *git.Client, hostname string) (helper string, err error) {
|
||||
ctx := context.TODO()
|
||||
|
||||
helper, err = gitClient.Config(ctx, gitCredentialHelperKey(hostname))
|
||||
if helper != "" {
|
||||
return
|
||||
}
|
||||
helper, err = gitClient.Config(ctx, "credential.helper")
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -66,7 +66,7 @@ func TestGitCredentialsSetup_setOurs_GH(t *testing.T) {
|
|||
f := GitCredentialFlow{
|
||||
helper: "",
|
||||
GitClient: &git.Client{GitPath: "some/path/git"},
|
||||
HelperConfigurer: &gitcredentials.HelperConfigurer{
|
||||
HelperConfig: &gitcredentials.HelperConfig{
|
||||
SelfExecutablePath: "/path/to/gh",
|
||||
GitClient: &git.Client{GitPath: "some/path/git"},
|
||||
},
|
||||
|
|
@ -101,7 +101,7 @@ func TestGitCredentialSetup_setOurs_nonGH(t *testing.T) {
|
|||
f := GitCredentialFlow{
|
||||
helper: "",
|
||||
GitClient: &git.Client{GitPath: "some/path/git"},
|
||||
HelperConfigurer: &gitcredentials.HelperConfigurer{
|
||||
HelperConfig: &gitcredentials.HelperConfig{
|
||||
SelfExecutablePath: "/path/to/gh",
|
||||
GitClient: &git.Client{GitPath: "some/path/git"},
|
||||
},
|
||||
|
|
|
|||
|
|
@ -11,21 +11,21 @@ import (
|
|||
"github.com/cli/cli/v2/internal/ghinstance"
|
||||
)
|
||||
|
||||
type HelperConfigurer struct {
|
||||
type HelperConfig struct {
|
||||
SelfExecutablePath string
|
||||
GitClient *git.Client
|
||||
}
|
||||
|
||||
func (hc *HelperConfigurer) Configure(hostname string) error {
|
||||
func (hc *HelperConfig) Configure(hostname string) error {
|
||||
ctx := context.TODO()
|
||||
|
||||
credHelperKeys := []string{
|
||||
gitCredentialHelperKey(hostname),
|
||||
keyFor(hostname),
|
||||
}
|
||||
|
||||
gistHost := strings.TrimSuffix(ghinstance.GistHost(hostname), "/")
|
||||
if strings.HasPrefix(gistHost, "gist.") {
|
||||
credHelperKeys = append(credHelperKeys, gitCredentialHelperKey(gistHost))
|
||||
credHelperKeys = append(credHelperKeys, keyFor(gistHost))
|
||||
}
|
||||
|
||||
var configErr error
|
||||
|
|
@ -61,7 +61,20 @@ func (hc *HelperConfigurer) Configure(hostname string) error {
|
|||
return configErr
|
||||
}
|
||||
|
||||
func gitCredentialHelperKey(hostname string) string {
|
||||
func (hc *HelperConfig) ConfiguredHelper(hostname string) (string, error) {
|
||||
ctx := context.TODO()
|
||||
|
||||
helper, err := hc.GitClient.Config(ctx, keyFor(hostname))
|
||||
if helper != "" {
|
||||
// TODO: This is a direct refactoring removing named and naked returns
|
||||
// but we should probably look closer at the error handling here
|
||||
return helper, err
|
||||
}
|
||||
|
||||
return hc.GitClient.Config(ctx, "credential.helper")
|
||||
}
|
||||
|
||||
func keyFor(hostname string) string {
|
||||
host := strings.TrimSuffix(ghinstance.HostPrefix(hostname), "/")
|
||||
return fmt.Sprintf("credential.%s.helper", host)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -75,7 +75,7 @@ func Login(opts *LoginOptions) error {
|
|||
credentialFlow := &GitCredentialFlow{
|
||||
Prompter: opts.Prompter,
|
||||
GitClient: opts.GitClient,
|
||||
HelperConfigurer: &gitcredentials.HelperConfigurer{
|
||||
HelperConfig: &gitcredentials.HelperConfig{
|
||||
SelfExecutablePath: opts.Executable,
|
||||
GitClient: opts.GitClient,
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue