Skip to content

Commit

Permalink
🛂 Finalize ssh authentication issue (Fix #3)
Browse files Browse the repository at this point in the history
  • Loading branch information
yodamad committed Oct 6, 2024
1 parent 7ec9235 commit 7c9985a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
14 changes: 9 additions & 5 deletions cmd/gitinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
)
Expand Down Expand Up @@ -350,12 +351,15 @@ func selectItems(items []entity.GitFolder, fn filterFolder) []entity.GitFolder {

func gitFetch(repo *git.Repository) error {
fetchOptions := &git.FetchOptions{}
origin, _ := repo.Remote("origin")
if strings.Contains(origin.Config().URLs[0], "git@") {
remote, _ := repo.Remote(commons.REMOTE_NAME)
origin := remote.Config().URLs[0]
if strings.Contains(origin, "@") {
re := regexp.MustCompile(`\w+`)
user := re.FindStringSubmatch(origin)

var publicKey *ssh.PublicKeys
sshPath := os.Getenv("HOME") + "/.ssh/id_rsa"
sshKey, _ := os.ReadFile(sshPath)
publicKey, _ = ssh.NewPublicKeys("git", sshKey, "")
sshKey, _ := os.ReadFile(commons.PUBLICKEY_PATH)
publicKey, _ = ssh.NewPublicKeys(user[0], sshKey, commons.SSHKEY_PASSWORD)
fetchOptions.Auth = publicKey
}
return repo.Fetch(fetchOptions)
Expand Down
10 changes: 10 additions & 0 deletions commons/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commons
import (
"fmt"
"github.com/mitchellh/colorstring"
"os"
)

// ldflags vars
Expand All @@ -18,6 +19,15 @@ const DEFAULT_FOLDER = "."
// MAX_DEPTH /* Depth of search */
const MAX_DEPTH = 3

// PUBLICKEY_PATH /* Path for public key for ssh authentication */
var PUBLICKEY_PATH = os.Getenv("HOME") + "/.ssh/id_rsa"

// SSHKEY_PASSWORD /* Default password for ssh authentication key opening */
const SSHKEY_PASSWORD = ""

// REMOTE_NAME /* Name of the remote URL */
const REMOTE_NAME = "origin"

// RootDir /* Which directory to start from */
var RootDir string

Expand Down

0 comments on commit 7c9985a

Please sign in to comment.