-
Notifications
You must be signed in to change notification settings - Fork 0
/
ioutil.go
48 lines (35 loc) · 824 Bytes
/
ioutil.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
package main
import (
"bufio"
"io"
"os"
"os/exec"
)
func check(e error) {
if e != nil {
panic(e)
}
}
// writeCmdToFile ... Writes the output of a command passed to the OS to a file in the current directory.
func writeCmdToFile(filename string, cmdstruct *exec.Cmd) {
outfile, err := os.Create("./" + filename)
check(err)
defer outfile.Close()
stdoutPipe, err := cmdstruct.StdoutPipe()
check(err)
writer := bufio.NewWriter(outfile)
defer writer.Flush()
err = cmdstruct.Start()
check(err)
go io.Copy(writer, stdoutPipe)
cmdstruct.Wait()
}
// writeStringToFile ... Writes a string to a file in the current directory.
func writeStringToFile(filename string, input string) {
f, err := os.Create("./" + filename)
check(err)
defer f.Close()
_, err = f.WriteString(input)
check(err)
f.Sync()
}