creds: expose FirstEntryForKey

We'd like to use this function in a different package, so let's rename
it to be public.
This commit is contained in:
brian m. carlson 2023-08-30 20:40:04 +00:00
parent a3829f4865
commit f2810c3c96
No known key found for this signature in database
GPG Key ID: 2D0C9BC12F82B3A1
2 changed files with 28 additions and 28 deletions

@ -184,9 +184,9 @@ const (
// provided, i.e. through the git URL // provided, i.e. through the git URL
func (a *AskPassCredentialHelper) Fill(what Creds) (Creds, error) { func (a *AskPassCredentialHelper) Fill(what Creds) (Creds, error) {
u := &url.URL{ u := &url.URL{
Scheme: firstEntryForKey(what, "protocol"), Scheme: FirstEntryForKey(what, "protocol"),
Host: firstEntryForKey(what, "host"), Host: FirstEntryForKey(what, "host"),
Path: firstEntryForKey(what, "path"), Path: FirstEntryForKey(what, "path"),
} }
creds := make(Creds) creds := make(Creds)
@ -297,9 +297,9 @@ type commandCredentialHelper struct {
func (h *commandCredentialHelper) Fill(creds Creds) (Creds, error) { func (h *commandCredentialHelper) Fill(creds Creds) (Creds, error) {
tracerx.Printf("creds: git credential fill (%q, %q, %q)", tracerx.Printf("creds: git credential fill (%q, %q, %q)",
firstEntryForKey(creds, "protocol"), FirstEntryForKey(creds, "protocol"),
firstEntryForKey(creds, "host"), FirstEntryForKey(creds, "host"),
firstEntryForKey(creds, "path")) FirstEntryForKey(creds, "path"))
return h.exec("fill", creds) return h.exec("fill", creds)
} }
@ -310,9 +310,9 @@ func (h *commandCredentialHelper) Reject(creds Creds) error {
func (h *commandCredentialHelper) Approve(creds Creds) error { func (h *commandCredentialHelper) Approve(creds Creds) error {
tracerx.Printf("creds: git credential approve (%q, %q, %q)", tracerx.Printf("creds: git credential approve (%q, %q, %q)",
firstEntryForKey(creds, "protocol"), FirstEntryForKey(creds, "protocol"),
firstEntryForKey(creds, "host"), FirstEntryForKey(creds, "host"),
firstEntryForKey(creds, "path")) FirstEntryForKey(creds, "path"))
_, err := h.exec("approve", creds) _, err := h.exec("approve", creds)
return err return err
} }
@ -345,7 +345,7 @@ func (h *commandCredentialHelper) exec(subcommand string, input Creds) (Creds, e
if _, ok := err.(*exec.ExitError); ok { if _, ok := err.(*exec.ExitError); ok {
if h.SkipPrompt { if h.SkipPrompt {
return nil, errors.New(tr.Tr.Get("change the GIT_TERMINAL_PROMPT env var to be prompted to enter your credentials for %s://%s", return nil, errors.New(tr.Tr.Get("change the GIT_TERMINAL_PROMPT env var to be prompted to enter your credentials for %s://%s",
firstEntryForKey(input, "protocol"), firstEntryForKey(input, "host"))) FirstEntryForKey(input, "protocol"), FirstEntryForKey(input, "host")))
} }
// 'git credential' exits with 128 if the helper doesn't fill the username // 'git credential' exits with 128 if the helper doesn't fill the username
@ -386,9 +386,9 @@ func NewCredentialCacher() *credentialCacher {
func credCacheKey(creds Creds) string { func credCacheKey(creds Creds) string {
parts := []string{ parts := []string{
firstEntryForKey(creds, "protocol"), FirstEntryForKey(creds, "protocol"),
firstEntryForKey(creds, "host"), FirstEntryForKey(creds, "host"),
firstEntryForKey(creds, "path"), FirstEntryForKey(creds, "path"),
} }
return strings.Join(parts, "//") return strings.Join(parts, "//")
} }
@ -401,9 +401,9 @@ func (c *credentialCacher) Fill(what Creds) (Creds, error) {
if ok { if ok {
tracerx.Printf("creds: git credential cache (%q, %q, %q)", tracerx.Printf("creds: git credential cache (%q, %q, %q)",
firstEntryForKey(what, "protocol"), FirstEntryForKey(what, "protocol"),
firstEntryForKey(what, "host"), FirstEntryForKey(what, "host"),
firstEntryForKey(what, "path")) FirstEntryForKey(what, "path"))
return cached, nil return cached, nil
} }
@ -572,7 +572,7 @@ func (h *nullCredentialHelper) Reject(creds Creds) error {
return nil return nil
} }
func firstEntryForKey(input Creds, key string) string { func FirstEntryForKey(input Creds, key string) string {
if val, ok := input[key]; ok && len(val) > 0 { if val, ok := input[key]; ok && len(val) > 0 {
return val[0] return val[0]
} }

@ -63,7 +63,7 @@ func newNetrcCredentialHelper(osEnv config.Environment) *netrcCredentialHelper {
} }
func (c *netrcCredentialHelper) Fill(what Creds) (Creds, error) { func (c *netrcCredentialHelper) Fill(what Creds) (Creds, error) {
host, err := getNetrcHostname(firstEntryForKey(what, "host")) host, err := getNetrcHostname(FirstEntryForKey(what, "host"))
if err != nil { if err != nil {
return nil, credHelperNoOp return nil, credHelperNoOp
} }
@ -72,7 +72,7 @@ func (c *netrcCredentialHelper) Fill(what Creds) (Creds, error) {
if c.skip[host] { if c.skip[host] {
return nil, credHelperNoOp return nil, credHelperNoOp
} }
if machine := c.netrcFinder.FindMachine(host, firstEntryForKey(what, "username")); machine != nil { if machine := c.netrcFinder.FindMachine(host, FirstEntryForKey(what, "username")); machine != nil {
creds := make(Creds) creds := make(Creds)
creds["username"] = []string{machine.Login} creds["username"] = []string{machine.Login}
creds["password"] = []string{machine.Password} creds["password"] = []string{machine.Password}
@ -82,9 +82,9 @@ func (c *netrcCredentialHelper) Fill(what Creds) (Creds, error) {
creds["path"] = what["path"] creds["path"] = what["path"]
creds["source"] = []string{"netrc"} creds["source"] = []string{"netrc"}
tracerx.Printf("netrc: git credential fill (%q, %q, %q, %q)", tracerx.Printf("netrc: git credential fill (%q, %q, %q, %q)",
firstEntryForKey(what, "protocol"), FirstEntryForKey(what, "protocol"),
firstEntryForKey(what, "host"), machine.Login, FirstEntryForKey(what, "host"), machine.Login,
firstEntryForKey(what, "path")) FirstEntryForKey(what, "path"))
return creds, nil return creds, nil
} }
@ -105,15 +105,15 @@ func getNetrcHostname(hostname string) (string, error) {
} }
func (c *netrcCredentialHelper) Approve(what Creds) error { func (c *netrcCredentialHelper) Approve(what Creds) error {
if firstEntryForKey(what, "source") == "netrc" { if FirstEntryForKey(what, "source") == "netrc" {
host, err := getNetrcHostname(firstEntryForKey(what, "host")) host, err := getNetrcHostname(FirstEntryForKey(what, "host"))
if err != nil { if err != nil {
return credHelperNoOp return credHelperNoOp
} }
tracerx.Printf("netrc: git credential approve (%q, %q, %q)", tracerx.Printf("netrc: git credential approve (%q, %q, %q)",
firstEntryForKey(what, "protocol"), FirstEntryForKey(what, "protocol"),
firstEntryForKey(what, "host"), FirstEntryForKey(what, "host"),
firstEntryForKey(what, "path")) FirstEntryForKey(what, "path"))
c.mu.Lock() c.mu.Lock()
c.skip[host] = false c.skip[host] = false
c.mu.Unlock() c.mu.Unlock()
@ -123,7 +123,7 @@ func (c *netrcCredentialHelper) Approve(what Creds) error {
} }
func (c *netrcCredentialHelper) Reject(what Creds) error { func (c *netrcCredentialHelper) Reject(what Creds) error {
if firstEntryForKey(what, "source") == "netrc" { if FirstEntryForKey(what, "source") == "netrc" {
host, err := getNetrcHostname(what["host"][0]) host, err := getNetrcHostname(what["host"][0])
if err != nil { if err != nil {
return credHelperNoOp return credHelperNoOp