-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileBase.go
155 lines (120 loc) · 3.37 KB
/
FileBase.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
148
149
150
151
152
153
154
155
package asciichgolangpublic
import "errors"
var ErrFileBaseParentNotSet = errors.New("parent is not set")
// This is the base for `File` providing most convenience functions for file operations.
type FileBase struct {
parentFileForBaseClass File
}
func NewFileBase() (f *FileBase) {
return new(FileBase)
}
func (f *FileBase) GetParentFileForBaseClass() (parentFileForBaseClass File, err error) {
if f.parentFileForBaseClass == nil {
return nil, TracedErrorf("%w", ErrFileBaseParentNotSet)
}
return f.parentFileForBaseClass, nil
}
func (f *FileBase) GetSha256Sum() (sha256sum string, err error) {
parent, err := f.GetParentFileForBaseClass()
if err != nil {
return "", err
}
content, err := parent.ReadAsString()
if err != nil {
return "", err
}
sha256sum = Checksums().GetSha256SumFromString(content)
return sha256sum, nil
}
func (f *FileBase) IsContentEqualByComparingSha256Sum(otherFile File, verbose bool) (isEqual bool, err error) {
if otherFile == nil {
return false, TracedErrorNil("otherFile")
}
thisChecksum, err := f.GetSha256Sum()
if err != nil {
return false, err
}
otherChecksum, err := otherFile.GetSha256Sum()
if err != nil {
return false, err
}
isEqual = thisChecksum == otherChecksum
return isEqual, nil
}
func (f *FileBase) IsMatchingSha256Sum(sha256sum string) (isMatching bool, err error) {
currentSum, err := f.GetSha256Sum()
if err != nil {
return false, err
}
isMatching = currentSum == sha256sum
return isMatching, nil
}
func (f *FileBase) MustGetParentFileForBaseClass() (parentFileForBaseClass File) {
parentFileForBaseClass, err := f.GetParentFileForBaseClass()
if err != nil {
LogGoErrorFatal(err)
}
return parentFileForBaseClass
}
func (f *FileBase) MustGetSha256Sum() (sha256sum string) {
sha256sum, err := f.GetSha256Sum()
if err != nil {
LogGoErrorFatal(err)
}
return sha256sum
}
func (f *FileBase) MustIsContentEqualByComparingSha256Sum(otherFile File, verbose bool) (isEqual bool) {
isEqual, err := f.IsContentEqualByComparingSha256Sum(otherFile, verbose)
if err != nil {
LogGoErrorFatal(err)
}
return isEqual
}
func (f *FileBase) MustIsMatchingSha256Sum(sha256sum string) (isMatching bool) {
isMatching, err := f.IsMatchingSha256Sum(sha256sum)
if err != nil {
LogGoErrorFatal(err)
}
return isMatching
}
func (f *FileBase) MustReadAsString() (content string) {
content, err := f.ReadAsString()
if err != nil {
LogGoErrorFatal(err)
}
return content
}
func (f *FileBase) MustSetParentFileForBaseClass(parentFileForBaseClass File) {
err := f.SetParentFileForBaseClass(parentFileForBaseClass)
if err != nil {
LogGoErrorFatal(err)
}
}
func (f *FileBase) MustWriteString(toWrite string, verbose bool) {
err := f.WriteString(toWrite, verbose)
if err != nil {
LogGoErrorFatal(err)
}
}
func (f *FileBase) ReadAsString() (content string, err error) {
parent, err := f.GetParentFileForBaseClass()
if err != nil {
return "", err
}
contentBytes, err := parent.ReadAsBytes()
if err != nil {
return "", err
}
return string(contentBytes), nil
}
func (f *FileBase) SetParentFileForBaseClass(parentFileForBaseClass File) (err error) {
f.parentFileForBaseClass = parentFileForBaseClass
return nil
}
func (f *FileBase) WriteString(toWrite string, verbose bool) (err error) {
parent, err := f.GetParentFileForBaseClass()
if err != nil {
return err
}
return parent.WriteBytes([]byte(toWrite), verbose)
}