Skip to content

Instantly share code, notes, and snippets.

@alesr
Created October 7, 2015 15:06
Show Gist options
  • Save alesr/4fa2725994f6c1622594 to your computer and use it in GitHub Desktop.
Save alesr/4fa2725994f6c1622594 to your computer and use it in GitHub Desktop.
func (p *Project) insertSshkey() {
homeDir := fileUtil.FindUserHomeDir()
cmd := exec.Command("cat", homeDir+"/.ssh/"+p.sshkey.name+".pub", "|", "ssh", p.projectname.name+"@"+p.host.name, "'cat", ">>", "~/.ssh/authorized_keys'")
err := cmd.Run()
if err != nil {
...
}
}
@marcosinger
Copy link

pensando somente nessa função, tenho uma alternativa com ssh-copy-id:

identity := fmt.Sprintf("%s/.ssh/%s.pub", fileUtil.FindUserHomeDir(), p.sshkey.name)
address := fmt.Sprintf("%s@%s", p.projectname.name, p.host.name)
cmd := exec.Command("ssh-copy-id", "-i", identity, address)

tanto a lógica do identity quanto o address poderiam estar dentro da struct ( se fizer sentido ), indo para algo como:

cmd := exec.Command("ssh-copy-id", "-i", p.PublicKey(), p.Address())

assim você só tem um lugar para fazer toda essa lógica e ficando muito mais fácil de testar.

@alesr
Copy link
Author

alesr commented Oct 8, 2015

Solução:

keyFile, err := os.Open(fileUtil.FindUserHomeDir() + sep + ".ssh" + sep + p.sshkey.name + ".pub")
if err != nil {
    log.Fatal(err)
}

cmd := exec.Command("ssh", p.projectname.name+"@"+p.host.name, "cat >> ~/.ssh/authorized_keys")
cmd.Stdin = keyFile

out, err := cmd.CombinedOutput()
if err != nil {
    fmt.Println(string(out))
    log.Fatal(err)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment