-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource-generator.go
56 lines (43 loc) · 1.24 KB
/
resource-generator.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
package main
import (
"fmt"
)
type ResourceGenerator struct {
Res ResourceInterface
}
func (rg *ResourceGenerator) Execute(race chan<- bool) bool {
if _, err := rg.createDirIfNotExists(); err != nil {
fmt.Println("Unexpected error while checking ", rg.Res.GetDir(), err)
race <- true
return false
}
rg.createFileIfNotExists()
race <- true
return true
}
func (rg *ResourceGenerator) createDirIfNotExists() (bool, error) {
fullDirPath := GetFullDirPath(rg.Res.GetDir())
exists, err := PathExists(fullDirPath)
if err != nil {
return false, err
}
if exists == false {
CreateDir(fullDirPath)
return true, nil
} else {
return false, nil
}
}
func (rg *ResourceGenerator) createFileIfNotExists() bool {
fullFilePath := GetFullFilePath(rg.Res.GetDir(), rg.Res.GetFileName())
exists, err := PathExists(fullFilePath)
if exists || err != nil {
fmt.Println("File alrady exists, skipping", fullFilePath)
return false
}
file := CreateFile(fullFilePath);
defer CloseFile(file, fullFilePath);
WriteFile(file, rg.Res.GetFileContent(), fullFilePath)
fmt.Println("File", fullFilePath, "created")
return true
}