/
osutil.go
140 lines (125 loc) · 4.15 KB
/
osutil.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Utility functions for functionality you would expect to find in the os package, such as running programs and testing if files exist. Most of the program running functions are deprecaiated now that the exec package has been updated in the latest weekly.
package osutil
import (
"errors"
"log"
"os"
"os/exec"
"path"
"strings"
)
// Checks if a file exists, returns true if the file exists and can be opened, false otherwise. If the file exists but cannot be opened, it still returns false.
// NOTE: Assumes that any error opening the file for reading means that the file does not exist. This is not always true, but is probably the best behaviour anyway. There is rarely a usage case where you want to check if a file exists, but not if you can open it.
func FileExists(name string) bool {
_, err := os.Stat(name)
if err != nil {
return false
}
return true
}
func findEnv(env []string, name string) string {
for i := 0; i < len(env); i++ {
foundname, value := parseEnv(env[i])
if foundname == name {
return value
}
}
return ""
}
func parseEnv(variable string) (string, string) {
namevalue := strings.SplitN(variable, "=", 2)
name := namevalue[0]
value := ""
if len(namevalue) > 1 {
value = namevalue[1]
}
return name, value
}
func mergeEnv(base []string, overrides []string) []string {
envmap := make(map[string]string, len(base))
for i := 0; i < len(base); i++ {
name, value := parseEnv(base[i])
envmap[name] = value
}
for i := 0; i < len(overrides); i++ {
name, value := parseEnv(overrides[i])
envmap[name] = value
}
envslice := make([]string, len(envmap))
i := 0
for name, value := range envmap {
envslice[i] = name + "=" + value
i++
}
return envslice
}
func findCmd(PATH, cmd string) (string, error) {
// If the name contains a /, e.g. framework/foo/blar or ./foo, then try the path directly.
if strings.Index(cmd, "/") != -1 {
if FileExists(cmd) {
return cmd, nil
}
}
paths := strings.Split(PATH, ":")
for i := 0; i < len(paths); i++ {
lookpath := paths[i]
binpath := path.Join(lookpath, cmd)
if FileExists(binpath) {
return binpath, nil
}
}
return "", errors.New("Command " + cmd + " not found! Path is " + PATH)
}
func RunWithEnvAndWd(command string, args []string, env []string, wd string) (proc *exec.Cmd, err error) {
//log.Println(command, args)
//hho := exec.PassThrough
args = prepend(args, command)
env = mergeEnv(os.Environ(), env)
binpath, err := findCmd(findEnv(env, "PATH"), command)
if err != nil {
return nil, err
}
cmd := new(exec.Cmd)
cmd.Path = binpath
cmd.Args = args
cmd.Env = env
cmd.Dir = wd
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
err = cmd.Start()
if err != nil {
log.Print("Error running command ", command, ": ", err, "\n")
return nil, err
}
return cmd, nil
}
// More advanced, runs a program with a custom enviroment. Note that the normal enviroment is also passed here, as that is what is typically desired. Enviroment is a slice of strings, with each string usually having the form "NAME=VALUE". If you pass an enviroment string of the form NAME=VALUE that has the same name as an existing enviroment string, your value overwrites the value of the other variable.
func RunWithEnv(command string, args []string, env []string) (proc *exec.Cmd, err error) {
return RunWithEnvAndWd(command, args, env, ".")
}
// Simple way to run most programs. Searches for the program in PATH, and runs the first found program. Args need not contain the program name as the zeroth argument, it is prepended automatically.
func Run(command string, args []string) (proc *exec.Cmd, err error) {
return RunWithEnv(command, args, []string{})
}
// Runs a command using Run(), but waits for it to complete before returning.
func WaitRun(command string, args []string) (proc *exec.Cmd, err error) {
proc, err = Run(command, args)
if err != nil {
return
}
proc.Wait()
return
}
// Doesn't really belong here. Oh well...
func prepend(orig []string, prep ...string) []string {
//log.Println(orig, prep)
arr := make([]string, len(prep)+len(orig))
for i := len(prep); i < len(arr); i++ {
arr[i] = orig[i-len(prep)]
}
for i := 0; i < len(prep); i++ {
arr[i] = prep[i]
}
//log.Println(arr)
return arr
}