Skip to content

Commit

Permalink
Tidy up username and password requests when using builtin git
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Jul 29, 2021
1 parent cfe9084 commit 523496c
Showing 1 changed file with 13 additions and 31 deletions.
44 changes: 13 additions & 31 deletions internal/cmd/initcmd.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"bufio"
"bytes"
"encoding/json"
"errors"
Expand All @@ -12,7 +11,6 @@ import (
"runtime"
"strconv"
"strings"
"syscall"
"text/template"

"github.com/go-git/go-git/v5"
Expand Down Expand Up @@ -151,7 +149,6 @@ func (c *Config) runInitCmd(cmd *cobra.Command, args []string) error {
} else {
dotfilesRepoURL := guessDotfilesRepoURL(args[0], c.init.ssh)
if useBuiltinGit {
isBare := false
var referenceName plumbing.ReferenceName
if c.init.branch != "" {
referenceName = plumbing.NewBranchReferenceName(c.init.branch)
Expand All @@ -162,36 +159,21 @@ func (c *Config) runInitCmd(cmd *cobra.Command, args []string) error {
ReferenceName: referenceName,
RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
}

if _, err := git.PlainClone(string(rawSourceDir), isBare, &cloneOptions); err != nil {
if errors.Is(err, transport.ErrAuthenticationRequired) {
reader := bufio.NewReader(os.Stdin)

c.writeToStdout("Git authentication required - enter username and password...\n")
c.writeToStdout("Username: ")
username, err := reader.ReadString('\n')
if err != nil {
return err
}
c.writeToStdout("Password: ")
passwordBytes, err := term.ReadPassword(int(syscall.Stdin))
c.writeToStdout("\n")
if err != nil {
return err
}
password := string(passwordBytes)
username = strings.TrimSpace(username)
password = strings.TrimSpace(password)
cloneOptions.Auth = &http.BasicAuth{
Username: username,
Password: password,
}
if _, err := git.PlainClone(string(rawSourceDir), isBare, &cloneOptions); err != nil {
return err
}
} else {
isBare := false
_, err = git.PlainClone(string(rawSourceDir), isBare, &cloneOptions)
if errors.Is(err, transport.ErrAuthenticationRequired) {
var basicAuth http.BasicAuth
if basicAuth.Username, err = c.readLine("Username? "); err != nil {
return err
}
if basicAuth.Password, err = c.readPassword("Password? "); err != nil {
return err
}
cloneOptions.Auth = &basicAuth
_, err = git.PlainClone(string(rawSourceDir), isBare, &cloneOptions)
}
if err != nil {
return err
}
} else {
args := []string{
Expand Down

0 comments on commit 523496c

Please sign in to comment.