mirror of
https://github.com/thegeeklab/wp-git-clone.git
synced 2024-11-14 19:30:40 +00:00
48 lines
569 B
Go
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,
|
||
|
)
|
||
|
}
|