forked from timewasted/go-check-certs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileIO.go
86 lines (72 loc) · 1.67 KB
/
fileIO.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
package main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"strings"
)
func outputCert(outPut error) error {
f, err := os.OpenFile(*resultsDir+"/results.csv", os.O_APPEND|os.O_WRONLY, os.ModeAppend)
check(err)
defer f.Close()
_, err = f.WriteString(outPut.Error() + "\n")
check(err)
return nil
}
func outputProblemCert(outPut string, problem string) {
f, err := os.OpenFile(*resultsDir+"/results.csv", os.O_APPEND|os.O_WRONLY, os.ModeAppend)
check(err)
defer f.Close()
// First remove the commas from the problem variable, otherwise new columns get made for each comma
problem = strings.Replace(problem, ",", "", -1)
_, err = f.WriteString(outPut + ",,,," + problem + "\n")
check(err)
return
}
func createOutPutFile() {
// write output file
f, err := os.Create(*resultsDir + "/results.csv")
check(err)
defer f.Close()
w := bufio.NewWriter(f)
// String to put into file
_, err = fmt.Fprintf(w, "%s", columnNames+"\n")
check(err)
w.Flush()
f.Close()
}
func refreshResults() {
err := os.Remove(*resultsDir + "/results.csv")
createOutPutFile()
processHosts()
check(err)
}
func addHost(s string) {
f, err := os.OpenFile(*hostsFile, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
panic(err)
}
defer f.Close()
if _, err = f.WriteString("\n" + s); err != nil {
panic(err)
}
}
func removeHost(s string) {
input, err := ioutil.ReadFile(*hostsFile)
check(err)
lines := strings.Split(string(input), "\n")
for i, line := range lines {
if strings.Contains(line, s) && s != "" {
lines[i] = ""
}
}
output := strings.Join(lines, "\n")
err = ioutil.WriteFile(*hostsFile, []byte(output), 0644)
check(err)
}
func check(err error) {
if err != nil {
panic(err)
}
}