0
0
mirror of https://github.com/thegeeklab/wp-git-clone.git synced 2024-11-22 10:20:38 +00:00
wp-git-clone/git/utils.go
2023-12-23 00:59:23 +01:00

48 lines
569 B
Go

package git
import (
"fmt"
"os"
"os/user"
"path/filepath"
)
const (
netrcFile = `
machine %s
login %s
password %s
`
)
const (
strictFilePerm = 0o600
)
// 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,
)
}