forked from kyma-project/kyma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
static-file.go
80 lines (62 loc) · 1.98 KB
/
static-file.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
package overrides
import (
"io/ioutil"
"log"
"os"
"path"
)
// StaticFile interface defines contract for overrides file representation
type StaticFile interface {
HasOverrides() bool
GetOverrides() (*string, error)
}
// LocalStaticFile struct defines static file overrides for local
type LocalStaticFile struct{}
// NewLocalStaticFile function creates instance of LocalStaticFile struct for cluster overrides
func NewLocalStaticFile() *LocalStaticFile {
return &LocalStaticFile{}
}
// HasOverrides .
func (localStaticFile *LocalStaticFile) HasOverrides() bool {
return false
}
// GetOverrides .
func (localStaticFile *LocalStaticFile) GetOverrides() (*string, error) {
return nil, nil
}
// ClusterStaticFile struct defines static file overrides for cluster
type ClusterStaticFile struct {
DirectoryPath *string
}
// NewClusterStaticFile function creates instance of ClusterStaticFile struct for cluster overrides
func NewClusterStaticFile(dirPath string) *ClusterStaticFile {
return &ClusterStaticFile{
DirectoryPath: &dirPath,
}
}
// HasOverrides function returns boolean whether additional overrides are defined
func (clusterStaticFile *ClusterStaticFile) HasOverrides() bool {
if clusterStaticFile.DirectoryPath == nil {
return false
}
if _, err := os.Stat(clusterStaticFile.getFilePath()); os.IsNotExist(err) {
return false
}
return true
}
// GetOverrides function reads cluster overrides file and returns its content
func (clusterStaticFile *ClusterStaticFile) GetOverrides() (*string, error) {
fileBytes, err := ioutil.ReadFile(clusterStaticFile.getFilePath())
if err != nil {
log.Printf(
"An error occured while reading file with additional overrides from path %s",
clusterStaticFile.getFilePath())
return nil, err
}
overridesStr := string(fileBytes)
return &overridesStr, nil
}
func (clusterStaticFile *ClusterStaticFile) getFilePath() string {
const clusterStaticFileName = "cluster.yaml"
return path.Join(*clusterStaticFile.DirectoryPath, clusterStaticFileName)
}