diff --git a/pkg/cmd/auth/refresh/refresh.go b/pkg/cmd/auth/refresh/refresh.go index fed0aae03..2c059a670 100644 --- a/pkg/cmd/auth/refresh/refresh.go +++ b/pkg/cmd/auth/refresh/refresh.go @@ -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, }, diff --git a/pkg/cmd/auth/setupgit/setupgit.go b/pkg/cmd/auth/setupgit/setupgit.go index e134ed10b..bcfd7207c 100644 --- a/pkg/cmd/auth/setupgit/setupgit.go +++ b/pkg/cmd/auth/setupgit/setupgit.go @@ -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, }, diff --git a/pkg/cmd/auth/shared/git_credential.go b/pkg/cmd/auth/shared/git_credential.go index 6d522acb6..0c1bc0656 100644 --- a/pkg/cmd/auth/shared/git_credential.go +++ b/pkg/cmd/auth/shared/git_credential.go @@ -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 -} diff --git a/pkg/cmd/auth/shared/git_credential_test.go b/pkg/cmd/auth/shared/git_credential_test.go index 017c649dd..b2f3df7f8 100644 --- a/pkg/cmd/auth/shared/git_credential_test.go +++ b/pkg/cmd/auth/shared/git_credential_test.go @@ -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"}, }, diff --git a/pkg/cmd/auth/shared/gitcredentials/gitcredentials.go b/pkg/cmd/auth/shared/gitcredentials/gitcredentials.go index d4f3616e3..1f8bafb07 100644 --- a/pkg/cmd/auth/shared/gitcredentials/gitcredentials.go +++ b/pkg/cmd/auth/shared/gitcredentials/gitcredentials.go @@ -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) } diff --git a/pkg/cmd/auth/shared/login_flow.go b/pkg/cmd/auth/shared/login_flow.go index dc6ff3cad..d357df5ff 100644 --- a/pkg/cmd/auth/shared/login_flow.go +++ b/pkg/cmd/auth/shared/login_flow.go @@ -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, },