| 25 | } |
| 26 | |
| 27 | func NewCmdSetupGit(f *cmdutil.Factory, runF func(*SetupGitOptions) error) *cobra.Command { |
| 28 | opts := &SetupGitOptions{ |
| 29 | IO: f.IOStreams, |
| 30 | Config: f.Config, |
| 31 | } |
| 32 | |
| 33 | cmd := &cobra.Command{ |
| 34 | Use: "setup-git", |
| 35 | Short: "Setup git with GitHub CLI", |
| 36 | Long: heredoc.Docf(` |
| 37 | This command configures %[1]sgit%[1]s to use GitHub CLI as a credential helper. |
| 38 | For more information on git credential helpers please reference: |
| 39 | <https://git-scm.com/docs/gitcredentials>. |
| 40 | |
| 41 | By default, GitHub CLI will be set as the credential helper for all authenticated hosts. |
| 42 | If there is no authenticated hosts the command fails with an error. |
| 43 | |
| 44 | Alternatively, use the %[1]s--hostname%[1]s flag to specify a single host to be configured. |
| 45 | If the host is not authenticated with, the command fails with an error. |
| 46 | `, "`"), |
| 47 | Example: heredoc.Doc(` |
| 48 | # Configure git to use GitHub CLI as the credential helper for all authenticated hosts |
| 49 | $ gh auth setup-git |
| 50 | |
| 51 | # Configure git to use GitHub CLI as the credential helper for enterprise.internal host |
| 52 | $ gh auth setup-git --hostname enterprise.internal |
| 53 | `), |
| 54 | RunE: func(cmd *cobra.Command, args []string) error { |
| 55 | opts.CredentialsHelperConfig = &gitcredentials.HelperConfig{ |
| 56 | SelfExecutablePath: f.ExecutablePath, |
| 57 | GitClient: f.GitClient, |
| 58 | } |
| 59 | if opts.Hostname == "" && opts.Force { |
| 60 | return cmdutil.FlagErrorf("`--force` must be used in conjunction with `--hostname`") |
| 61 | } |
| 62 | if runF != nil { |
| 63 | return runF(opts) |
| 64 | } |
| 65 | return setupGitRun(opts) |
| 66 | }, |
| 67 | } |
| 68 | |
| 69 | cmd.Flags().StringVarP(&opts.Hostname, "hostname", "h", "", "The hostname to configure git for") |
| 70 | cmd.Flags().BoolVarP(&opts.Force, "force", "f", false, "Force setup even if the host is not known. Must be used in conjunction with `--hostname`") |
| 71 | |
| 72 | return cmd |
| 73 | } |
| 74 | |
| 75 | func setupGitRun(opts *SetupGitOptions) error { |
| 76 | cfg, err := opts.Config() |