-
Notifications
You must be signed in to change notification settings - Fork 0
/
filesystem.go
143 lines (101 loc) · 2.19 KB
/
filesystem.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
141
142
143
/*
Copyright © 2023 Patrick Hermann [email protected]
*/
package base
import (
"fmt"
"log"
"os"
)
func CreateNestedDirectoryStructure(directoryStructure string, permission int) {
osModePermissions := os.FileMode(int(permission))
if err := os.MkdirAll(directoryStructure, osModePermissions); err != nil {
fmt.Println(err)
}
}
func RemoveNestedFolder(path string) {
err := os.RemoveAll(path)
if err != nil {
log.Fatal(err)
}
}
func VerifyFileExistence(filePath string, log *Logger, exitOnError bool) bool {
exists := false
fileExists, _ := VerifyIfFileOrDirExists(filePath, "file")
if !fileExists {
log.Warn(filePath + " was not found")
if exitOnError {
log.Error("exiting.. goodbye galaxy!")
os.Exit(3)
}
} else {
log.Info(filePath + " exists")
exists = true
}
return exists
}
func StoreVariableInFile(outputFilePath, outputData string) bool {
f, err := os.Create(outputFilePath)
if err != nil {
fmt.Println(err)
return false
}
defer f.Close()
_, err2 := f.WriteString(outputData)
if err2 != nil {
fmt.Println(err2)
return false
}
return true
}
func ReadFileToVariable(filePath string) string {
content, err := os.ReadFile(filePath)
if err != nil {
log.Fatal(err)
}
return string(content)
}
/*
checks if a file exists or not
use w/ sthingsBase.FileExists("/home/my-file.yaml")
*/
func VerifyIfFileOrDirExists(filePath, kind string) (bool, error) {
info, err := os.Stat(filePath)
// returns true if folder exists
if err == nil && kind == "dir" {
return info.IsDir(), nil
}
// returns true if file exists
if info != nil && kind == "file" {
return true, nil
}
return false, err
}
func DeleteFile(filePath string) bool {
err := os.Remove(filePath)
if err != nil {
return false
} else {
return true
}
}
func WriteDataToFile(outputFilePath, outputData string) bool {
f, err := os.Create(outputFilePath)
if err != nil {
log.Fatal(err)
return false
}
defer f.Close()
_, err2 := f.WriteString(outputData)
if err2 != nil {
log.Fatal(err2)
return false
}
return true
}
func MoveRenameFileOnFS(oldLocation, newLocation string) {
err := os.Rename(oldLocation, newLocation)
if err != nil {
log.Fatal(err)
}
}