| 73 | } |
| 74 | |
| 75 | func setupGitRun(opts *SetupGitOptions) error { |
| 76 | cfg, err := opts.Config() |
| 77 | if err != nil { |
| 78 | return err |
| 79 | } |
| 80 | authCfg := cfg.Authentication() |
| 81 | hostnames := authCfg.Hosts() |
| 82 | |
| 83 | stderr := opts.IO.ErrOut |
| 84 | cs := opts.IO.ColorScheme() |
| 85 | |
| 86 | // If a hostname was provided, we'll set up just that one |
| 87 | if opts.Hostname != "" { |
| 88 | if !opts.Force && !has(opts.Hostname, hostnames) { |
| 89 | return fmt.Errorf("You are not logged into the GitHub host %q. Run %s to authenticate or provide `--force`", |
| 90 | opts.Hostname, |
| 91 | cs.Bold(fmt.Sprintf("gh auth login -h %s", opts.Hostname)), |
| 92 | ) |
| 93 | } |
| 94 | |
| 95 | if err := opts.CredentialsHelperConfig.ConfigureOurs(opts.Hostname); err != nil { |
| 96 | return fmt.Errorf("failed to set up git credential helper: %s", err) |
| 97 | } |
| 98 | |
| 99 | return nil |
| 100 | } |
| 101 | |
| 102 | // Otherwise we'll set up any known hosts |
| 103 | if len(hostnames) == 0 { |
| 104 | fmt.Fprintf( |
| 105 | stderr, |
| 106 | "You are not logged into any GitHub hosts. Run %s to authenticate.\n", |
| 107 | cs.Bold("gh auth login"), |
| 108 | ) |
| 109 | |
| 110 | return cmdutil.SilentError |
| 111 | } |
| 112 | |
| 113 | for _, hostname := range hostnames { |
| 114 | if err := opts.CredentialsHelperConfig.ConfigureOurs(hostname); err != nil { |
| 115 | return fmt.Errorf("failed to set up git credential helper: %s", err) |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | return nil |
| 120 | } |
| 121 | |
| 122 | func has(needle string, haystack []string) bool { |
| 123 | for _, s := range haystack { |