0
0
mirror of https://github.com/thegeeklab/wp-git-action.git synced 2024-06-03 04:39:42 +02:00
wp-git-action/git/utils.go

96 lines
1.2 KiB
Go
Raw Normal View History

2022-11-27 14:33:39 +01:00
package git
2019-06-07 12:32:16 +02:00
import (
"fmt"
"os"
"os/user"
"path/filepath"
)
const netrcFile = `
machine %s
login %s
password %s
`
const configFile = `
Host *
StrictHostKeyChecking no
UserKnownHostsFile=/dev/null
`
2022-11-27 14:33:39 +01:00
// WriteKey writes the SSH private key.
func WriteSSHKey(privateKey string) error {
2019-06-07 12:32:16 +02:00
home := "/root"
if currentUser, err := user.Current(); err == nil {
home = currentUser.HomeDir
}
sshpath := filepath.Join(
home,
".ssh")
2022-11-27 14:33:39 +01:00
if err := os.MkdirAll(sshpath, 0o700); err != nil {
2019-06-07 12:32:16 +02:00
return err
}
confpath := filepath.Join(
sshpath,
"config")
2022-11-27 14:33:39 +01:00
if err := os.WriteFile(
2019-06-07 12:32:16 +02:00
confpath,
[]byte(configFile),
2022-11-27 14:33:39 +01:00
0o700,
2019-06-07 12:32:16 +02:00
); err != nil {
return err
}
privpath := filepath.Join(
sshpath,
"id_rsa",
)
2022-11-27 14:33:39 +01:00
if err := os.WriteFile(
2019-06-07 12:32:16 +02:00
privpath,
[]byte(privateKey),
2022-11-27 14:33:39 +01:00
0o600,
2019-06-07 12:32:16 +02:00
); err != nil {
return err
}
return nil
}
// WriteNetrc writes the netrc file.
func WriteNetrc(machine, login, password string) error {
if machine == "" {
return nil
}
netrcContent := fmt.Sprintf(
netrcFile,
machine,
login,
password,
)
home := "/root"
if currentUser, err := user.Current(); err == nil {
home = currentUser.HomeDir
}
netpath := filepath.Join(
home,
".netrc",
)
2022-11-27 14:33:39 +01:00
return os.WriteFile(
2019-06-07 12:32:16 +02:00
netpath,
[]byte(netrcContent),
2022-11-27 14:33:39 +01:00
0o600,
2019-06-07 12:32:16 +02:00
)
}