ConfigureOurs sets up the git credential helper chain to use the GitHub CLI credential helper for git repositories including gists.
(hostname string)
| 20 | // ConfigureOurs sets up the git credential helper chain to use the GitHub CLI credential helper for git repositories |
| 21 | // including gists. |
| 22 | func (hc *HelperConfig) ConfigureOurs(hostname string) error { |
| 23 | ctx := context.TODO() |
| 24 | |
| 25 | credHelperKeys := []string{ |
| 26 | keyFor(hostname), |
| 27 | } |
| 28 | |
| 29 | gistHost := strings.TrimSuffix(ghinstance.GistHost(hostname), "/") |
| 30 | if strings.HasPrefix(gistHost, "gist.") { |
| 31 | credHelperKeys = append(credHelperKeys, keyFor(gistHost)) |
| 32 | } |
| 33 | |
| 34 | var configErr error |
| 35 | |
| 36 | for _, credHelperKey := range credHelperKeys { |
| 37 | if configErr != nil { |
| 38 | break |
| 39 | } |
| 40 | // first use a blank value to indicate to git we want to sever the chain of credential helpers |
| 41 | preConfigureCmd, err := hc.GitClient.Command(ctx, "config", "--global", "--replace-all", credHelperKey, "") |
| 42 | if err != nil { |
| 43 | configErr = err |
| 44 | break |
| 45 | } |
| 46 | if _, err = preConfigureCmd.Output(); err != nil { |
| 47 | configErr = err |
| 48 | break |
| 49 | } |
| 50 | |
| 51 | // second configure the actual helper for this host |
| 52 | configureCmd, err := hc.GitClient.Command(ctx, |
| 53 | "config", "--global", "--add", |
| 54 | credHelperKey, |
| 55 | fmt.Sprintf("!%s auth git-credential", shellQuote(hc.SelfExecutablePath)), |
| 56 | ) |
| 57 | if err != nil { |
| 58 | configErr = err |
| 59 | } else { |
| 60 | _, configErr = configureCmd.Output() |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | return configErr |
| 65 | } |
| 66 | |
| 67 | // A Helper represents a git credential helper configuration. |
| 68 | type Helper struct { |