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...

[email protected]: 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.

4
  • 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 2, 2020 at 19:49
  • 1
    See this answer for various clues for workarounds stackoverflow.com/questions/7772190/…
    – Vorsprung
    Mar 2, 2020 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? Mar 3, 2020 at 8:13
  • @user3755946 do post your answer, it's good you got it working.
    – Mark
    Mar 3, 2020 at 10:15

1 Answer 1

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))
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.