-
Notifications
You must be signed in to change notification settings - Fork 20
/
file_service.go
147 lines (138 loc) · 3.85 KB
/
file_service.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
144
145
146
147
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
"syscall"
)
type FileServiceInterface interface {
GetFileUid(filePath string) uint32
RemoveFile(filePath string, ignoreNoSuchFileError bool)
ReadFile(filePath string) string
ReadDockerComposeFile(filePath string) string
WriteToFile(filePath string, contents string, logLevel string)
AppendContents(filePath string, contents string, logLevel string)
GetCurrentDir() string
RemoveGeneratedFile(removeContainers string, filePath string)
RemoveGeneratedFileIgnoreError(removeContainers string, filePath string, ignoreNoSuchFileError bool)
FileExists(filePath string) bool
}
type FileService struct {
Logger *Logger
}
func NewFileService(logger *Logger) FileService {
return FileService{
Logger: logger,
}
}
func (f FileService) GetFileUid(filePath string) uint32 {
if filePath == "" {
panic("filePath was empty")
}
fileInfo, err := os.Stat(filePath)
if err != nil {
panic(err)
}
mode := fileInfo.Sys().(*syscall.Stat_t)
uid := mode.Uid
return uid
}
func (f FileService) RemoveFile(filePath string, ignoreNoSuchFileError bool) {
if filePath == "" {
panic("filePath was empty")
}
err := os.Remove(filePath)
if err != nil {
if strings.Contains(err.Error(),"no such file or directory") && ignoreNoSuchFileError {
f.Logger.Log("debug", fmt.Sprintf("File already removed: %s", filePath))
return
}
panic(err)
}
f.Logger.Log("debug", fmt.Sprintf("Removed file: %s", filePath))
}
func (f FileService) ReadFile(filePath string) string {
if filePath == "" {
panic("filePath was empty")
}
contents, err := ioutil.ReadFile(filePath)
if err != nil {
panic(err)
}
return string(contents)
}
func (f FileService) FileExists(filePath string) bool {
_, err := os.Lstat(filePath)
if err != nil {
if os.IsNotExist(err) {
return false
}
panic(fmt.Sprintf("error when running os.Lstat(%q): %s", filePath, err))
}
return true
}
func (f FileService) ReadDockerComposeFile(filePath string) string {
if filePath == "" {
panic("filePath was empty")
}
return f.ReadFile(filePath)
}
func (f FileService) WriteToFile(filePath string, contents string, logLevel string) {
if filePath == "" {
panic("filePath was empty")
}
err := ioutil.WriteFile(filePath, []byte(contents), 0644)
if err != nil {
panic(err)
}
f.Logger.Log(logLevel, fmt.Sprintf("Written file %s, contents:\n %s", filePath, contents))
}
func (f FileService) AppendContents(filePath string, contents string, logLevel string) {
oldContents := f.ReadFile(filePath)
contentsMerged := fmt.Sprintf("%s\n%s", oldContents, contents)
f.WriteToFile(filePath, contentsMerged, logLevel)
}
func (f FileService) GetCurrentDir() string {
currentDirectory, err := os.Getwd()
if err != nil {
panic(err)
}
return currentDirectory
}
func (f FileService) RemoveGeneratedFile(removeContainers string, filePath string) {
if filePath == "" {
panic("filePath was empty")
}
if removeContainers != "false" {
err := os.Remove(filePath)
if err != nil {
panic(err)
}
f.Logger.Log("debug", fmt.Sprintf("Removed generated file: %s", filePath))
return
} else {
f.Logger.Log("debug", fmt.Sprintf("Not removed generated file: %s, because RemoveContainers is set", filePath))
return
}
}
func (f FileService) RemoveGeneratedFileIgnoreError(removeContainers string, filePath string, ignoreNoSuchFileError bool) {
if filePath == "" {
panic("filePath was empty")
}
if removeContainers != "false" {
err := os.Remove(filePath)
if err != nil {
if strings.Contains(err.Error(),"no such file or directory") && ignoreNoSuchFileError {
f.Logger.Log("debug", fmt.Sprintf("File already removed: %s", filePath))
return
}
panic(err)
}
f.Logger.Log("debug", fmt.Sprintf("Removed file: %s", filePath))
return
} else {
f.Logger.Log("debug", fmt.Sprintf("Not removed generated file: %s, because RemoveContainers is set", filePath))
return
}
}