-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfile.go
65 lines (59 loc) · 1.37 KB
/
file.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
63
64
65
package muts
import (
"log"
"os"
"path/filepath"
)
// Workspace holds the current working directory on startup.
var Workspace, _ = os.Getwd()
// CreateFileWith does what you think it should do.
func CreateFileWith(filename, contents string) {
f, err := os.Create(filename)
if err != nil {
Abort("CreateFileWith failed:", err)
}
defer f.Close()
n, err := f.WriteString(contents)
if err != nil {
Abort("CreateFileWith failed:", err)
}
log.Printf("written %d/%d bytes to %s\n", n, len(contents), filename) // show absolute name
}
// Setenv wraps the os one to check and log it
func Setenv(key, value string) {
if err := os.Setenv(key, value); err != nil {
Abort("Setenv failed:", err)
}
log.Println(key, "=", value)
}
// Chdir wraps the os one to check and log it
func Chdir(whereto string) {
here, err := os.Getwd()
if err != nil {
Abort("Chdir failed:", err)
}
if here == whereto {
return
}
abs, err := filepath.Abs(whereto)
if err != nil {
Abort("Chdir failed:", err)
}
if here == abs {
return
}
err = os.Chdir(whereto)
if err != nil {
Abort("Chdir failed:", err)
}
PrintfFunc("changed workdir: [%s] -> [%s]", here, abs)
}
// Mkdir wraps os.MkdirAll to check and log it.
func Mkdir(path string) {
abs, err := filepath.Abs(path)
err = os.MkdirAll(abs, os.ModePerm)
if err != nil {
Abort("Mkdir failed:", err)
}
PrintfFunc("created dir: [%s]", abs)
}