0
0
mirror of https://github.com/thegeeklab/wp-git-action.git synced 2024-11-22 10:10:39 +00:00
wp-git-action/git/utils.go

108 lines
1.5 KiB
Go

package git
import (
"fmt"
"os"
"os/user"
"path/filepath"
"strings"
"golang.org/x/sys/execabs"
)
const (
netrcFile = `
machine %s
login %s
password %s
`
configFile = `
Host *
StrictHostKeyChecking no
UserKnownHostsFile=/dev/null
`
)
const (
strictFilePerm = 0o600
strictDirPerm = 0o600
)
// WriteKey writes the SSH private key.
func WriteSSHKey(privateKey string) error {
home := "/root"
if currentUser, err := user.Current(); err == nil {
home = currentUser.HomeDir
}
sshpath := filepath.Join(home, ".ssh")
if err := os.MkdirAll(sshpath, strictDirPerm); err != nil {
return err
}
confpath := filepath.Join(sshpath, "config")
if err := os.WriteFile(
confpath,
[]byte(configFile),
strictFilePerm,
); err != nil {
return err
}
privpath := filepath.Join(sshpath, "id_rsa")
return os.WriteFile(
privpath,
[]byte(privateKey),
strictFilePerm,
)
}
// WriteNetrc writes the netrc file.
func WriteNetrc(machine, login, password string) error {
netrcContent := fmt.Sprintf(
netrcFile,
machine,
login,
password,
)
home := "/root"
if currentUser, err := user.Current(); err == nil {
home = currentUser.HomeDir
}
netpath := filepath.Join(
home,
".netrc",
)
return os.WriteFile(
netpath,
[]byte(netrcContent),
strictFilePerm,
)
}
func trace(cmd *execabs.Cmd) {
fmt.Fprintf(os.Stdout, "+ %s\n", strings.Join(cmd.Args, " "))
}
func runCommand(cmd *execabs.Cmd) error {
if cmd.Stdout == nil {
cmd.Stdout = os.Stdout
}
if cmd.Stderr == nil {
cmd.Stderr = os.Stderr
}
trace(cmd)
return cmd.Run()
}