-
Notifications
You must be signed in to change notification settings - Fork 28
/
utils.go
62 lines (54 loc) · 1.12 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
)
// trace writes the command in the programs stdout for debug purposes.
// the command is wrapped in xml tags for easy parsing.
func trace(cmd *exec.Cmd) {
fmt.Printf("+ %s\n", strings.Join(cmd.Args, " "))
}
// pathExists returns whether the given file or directory exists or not
func pathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}
// helper function returns true if directory dir is empty.
func isDirEmpty(dir string) bool {
f, err := os.Open(dir)
if err != nil {
return true
}
defer f.Close()
_, err = f.Readdir(1)
return err == io.EOF
}
// helper function to write a netrc file.
func writeNetrc(home, machine, login, password string) error {
if machine == "" || (login == "" && password == "") {
return nil
}
out := fmt.Sprintf(
netrcFile,
machine,
login,
password,
)
path := filepath.Join(home, ".netrc")
return os.WriteFile(path, []byte(out), 0o600)
}
const netrcFile = `
machine %s
login %s
password %s
`