summaryrefslogtreecommitdiff
path: root/old.go
blob: b4133fd1279183de0fb84be59d2aa1fae9586af3 (plain)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package shell

// old code and probably junk

import (
	"io/ioutil"
	"net/http"
	"os"
	"os/exec"

	"go.wit.com/log"
)

// TODO: look at https://github.com/go-cmd/cmd/issues/20
// use go-cmd instead here?

var callback func(interface{}, int)

var shellStdout *os.File
var shellStderr *os.File

var spewOn bool = false
var quiet bool = false

// var msecDelay   int  = 20	// number of milliseconds to delay between reads with no data

// var bytesBuffer bytes.Buffer
// var bytesSplice []byte

func handleError(c interface{}, ret int) {
	log.Log(INFO, "shell.Run() Returned", ret)
	if callback != nil {
		callback(c, ret)
	}
}

func init() {
	callback = nil
}

func InitCallback(f func(interface{}, int)) {
	callback = f
}

// this means it won't copy all the output to STDOUT
func Quiet(q bool) {
	quiet = q
}

/*
func Script(cmds string) int {
	// split on new lines (while we are at it, handle stupid windows text files
	lines := strings.Split(strings.Replace(cmds, "\r\n", "\n", -1), "\n")

	for _, line := range lines {
		line = Chomp(line) // this is like 'chomp' in perl
		log.Log(INFO, "LINE:", line)
		time.Sleep(1)
		RunString(line)
	}
	return 0
}
*/

func Unlink(filename string) bool {
	if err := os.Remove(filename); err != nil {
		return Exists(filename)
	} else {
		return Exists(filename)
	}
}

// run interactively. output from the cmd is in real time
// shows all the output. For example, 'ping -n localhost'
// shows the output like you would expect to see
func RunSimple(cmd []string) error {
	log.Log(INFO, "NewRun() ", cmd)

	return PathRunSimple("", cmd)
}

func PathRunSimple(workingpath string, cmd []string) error {
	log.Log(INFO, "NewRun() ", cmd)

	process := exec.Command(cmd[0], cmd[1:len(cmd)]...)
	// Set the working directory
	process.Dir = workingpath
	process.Stderr = os.Stderr
	process.Stdin = os.Stdin
	process.Stdout = os.Stdout
	process.Start()
	err := process.Wait()
	if err != nil {
		log.Log(INFO, "shell.Exec() err =", err)
	}
	return err
}

// return true if the filename exists (cross-platform)

// return true if the filename exists (cross-platform)
func Exists(filename string) bool {
	_, err := os.Stat(Path(filename))
	if os.IsNotExist(err) {
		return false
	}
	return true
}

// makes the directory
func Mkdir(dir string) bool {
	if Dir(dir) {
		// already a dir
		return true
	}
	if Exists(dir) {
		// something else is there
		return false
	}
	Run([]string{"mkdir", "-p", dir})
	return true
}

func IsDir(dirname string) bool {
	return Dir(dirname)
}

// return true if the filename exists (cross-platform)
func Dir(dirname string) bool {
	info, err := os.Stat(Path(dirname))
	if os.IsNotExist(err) {
		return false
	}
	return info.IsDir()
}

// Cat a file into a string
func Cat(filename string) string {
	buffer, err := ioutil.ReadFile(Path(filename))
	// log.Log(INFO, "buffer =", string(buffer))
	if err != nil {
		return ""
	}
	return string(buffer)
}

func RunPathHttpOut(path string, cmd []string, w http.ResponseWriter, r *http.Request) error {
	log.Warn("Run(): ", cmd)

	process := exec.Command(cmd[0], cmd[1:len(cmd)]...)
	process.Dir = path
	process.Stderr = os.Stderr
	process.Stdin = r.Body
	process.Stdout = w
	process.Start()
	err := process.Wait()
	log.Warn("shell.Exec() err =", err)
	return err
}

func RunHttpOut(cmd []string, w http.ResponseWriter, r *http.Request) error {
	log.Warn("NewRun() ", cmd)

	process := exec.Command(cmd[0], cmd[1:len(cmd)]...)
	process.Stderr = os.Stderr
	process.Stdin = r.Body
	process.Stdout = w
	process.Start()
	err := process.Wait()
	log.Warn("shell.Exec() err =", err)
	return err
}