1

I can't run git pull --tags from Go's exec.Command as I do not have access rights..

Code...

func SimpleCommand(start string, args ...string) (result string) {
    cmd := exec.Command(start, args...)

    //cmd.Dir = dir
    bytes, err := cmd.Output()
    if err != nil {
        fmt.Print(err)
    }
    s := string(bytes)

    return s

}
func main() {
    SimpleCommand("git", "pull", "--tags")


}

Normally when I run git commands I am doing it manually in Git-bash and I have the .bashrc file setup as follows...

 eval $(ssh-agent -s) 
 ssh-add c:/sshkeys/id_rsa 
 ssh-add c:/sshkeys/github

This works fine but I as I am running the git command from with in a Go program It obviously does not run these commands. So I get the following Error...

git@gitlab.com: Permission denied (publickey). fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists.

I have tried running these command individually using the SimpleCommand function however it cannot find the eval command (it does not appear to be a an executable),

I also feel there should be a better way of doing this is there not way i can have the go program manage the ssh keys in such a way it will be able to use them the run the git commands.

user3755946
  • 804
  • 1
  • 10
  • 22
  • Go doesn't run commands in a bash shell. Instead try setting the `core.sshCommand`. For example `git config core.sshCommand "ssh -i c:/sshkeys/id_rsa -i s:/sshkeys/github"`. This is useful because git config settings can be global or local to a repo. – Mark Mar 02 '20 at 19:49
  • 1
    See this answer for various clues for workarounds https://stackoverflow.com/questions/7772190/passing-ssh-options-to-git-clone – Vorsprung Mar 02 '20 at 21:57
  • @Mark I had a more in-depth look last night and found out about core.sshCommand and eventually got it working, was going to post answer this more but if you want go ahead? – user3755946 Mar 03 '20 at 08:13
  • @user3755946 do post your answer, it's good you got it working. – Mark Mar 03 '20 at 10:15

1 Answers1

1

Was unaware for the core.sshCommand, this now works for me.

Setup The current repo to use an ssh key in a specific location...

Setup function...

func SetupGitSsh() {
    sshKeysDir := "c:/sshkeys/id_rsa"
    sshloc := fmt.Sprintf("ssh -i %s", sshKeysDir)
    DirCommand("git", "c:\\DEV\\outsideprj", "config", "core.sshCommand", sshloc)
}

DirCommand Function...

func DirCommand(app string, dir string, args ...string) {

    cmd := exec.Command(app, args...)
    cmd.Dir = dir
    stdout, err := cmd.Output()

    if err != nil {
        fmt.Println(err.Error())
        return
    }

    fmt.Print(string(stdout))
}
user3755946
  • 804
  • 1
  • 10
  • 22