/
writefile.go
46 lines (40 loc) · 1.04 KB
/
writefile.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
package utils
import (
"io/ioutil"
"log"
"os"
"strconv"
)
// WriteFile saves output to a file
func WriteFile(workspace, config, name string) error {
// Create file inside workspace folder
err := workspaceExists(workspace)
if err == nil {
f, err := os.Create(workspace + "/" + name)
if err != nil {
log.Printf("Unable to create config file %s\n", name)
return err
}
defer f.Close()
// write config data
f.WriteString(config)
f.Close()
f.Sync()
}
return err
}
// workspaceExists checks for or creates the workspace directory
func workspaceExists(workspace string) error {
// Check if the workspace is a directory
if ws, err := os.Stat(workspace); err == nil && ws.IsDir() {
return nil
}
// Create directory if it isn't there
err := os.Mkdir(workspace, os.ModePerm)
return err
}
// WriteRunResult will write the count value into the runresult.log file and update the timestamp each run.
func WriteRunResult(filename string, count int) error {
d1 := []byte(strconv.Itoa(count))
return ioutil.WriteFile(filename, d1, 0644)
}