-
Notifications
You must be signed in to change notification settings - Fork 58
/
osutil.go
166 lines (142 loc) · 4.6 KB
/
osutil.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
156
157
158
159
160
161
162
163
164
165
166
package osutil
import (
"fmt"
"io/fs"
"os"
"path"
"regexp"
"runtime"
"strings"
"syscall"
log "github.com/sirupsen/logrus"
"github.com/Azure/draft/pkg/config"
"github.com/Azure/draft/pkg/templatewriter"
)
// A draft variable is defined as a string of non-whitespace characters wrapped in double curly braces.
var draftVariableRegex = regexp.MustCompile("{{[^\\s.]+\\S*}}")
// Exists returns whether the given file or directory exists or not.
func Exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}
// SymlinkWithFallback attempts to symlink a file or directory, but falls back to a move operation
// in the event of the user not having the required privileges to create the symlink.
func SymlinkWithFallback(oldname, newname string) (err error) {
err = os.Symlink(oldname, newname)
if runtime.GOOS == "windows" {
// If creating the symlink fails on Windows because the user
// does not have the required privileges, ignore the error and
// fall back to renaming the file.
//
// ERROR_PRIVILEGE_NOT_HELD is 0x522:
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms681385(v=vs.85).aspx
if lerr, ok := err.(*os.LinkError); ok && lerr.Err == syscall.Errno(0x522) {
err = os.Rename(oldname, newname)
}
}
return
}
// EnsureDirectory checks if a directory exists and creates it if it doesn't
func EnsureDirectory(dir string) error {
if fi, err := os.Stat(dir); err != nil {
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("could not create %s: %s", dir, err)
}
} else if !fi.IsDir() {
return fmt.Errorf("%s must be a directory", dir)
}
return nil
}
// EnsureFile checks if a file exists and creates it if it doesn't
func EnsureFile(file string) error {
fi, err := os.Stat(file)
if err != nil {
f, err := os.Create(file)
if err != nil {
return fmt.Errorf("could not create %s: %s", file, err)
}
defer f.Close()
} else if fi.IsDir() {
return fmt.Errorf("%s must not be a directory", file)
}
return nil
}
func CopyDir(
fileSys fs.FS,
src, dest string,
config *config.DraftConfig,
customInputs map[string]string,
templateWriter templatewriter.TemplateWriter) error {
files, err := fs.ReadDir(fileSys, src)
if err != nil {
return err
}
for _, f := range files {
if f.Name() == "draft.yaml" {
continue
}
srcPath := path.Join(src, f.Name())
destPath := path.Join(dest, f.Name())
if f.IsDir() {
if err = templateWriter.EnsureDirectory(destPath); err != nil {
return err
}
if err = CopyDir(fileSys, srcPath, destPath, config, customInputs, templateWriter); err != nil {
return err
}
} else {
fileContent, err := replaceTemplateVariables(fileSys, srcPath, customInputs)
if err != nil {
return err
}
if err = checkAllVariablesSubstituted(string(fileContent)); err != nil {
return fmt.Errorf("error substituting file %s: %w", srcPath, err)
}
if err = templateWriter.WriteFile(destPath, fileContent); err != nil {
return err
}
}
}
return nil
}
/*
checkAllVariablesSubstituted checks that all draft variables have been substituted.
If any draft variables are found, an error is returned.
Draft variables are defined as a string of non-whitespace characters starting with a non-period character wrapped in double curly braces.
The non-period first character constraint is used to avoid matching helm template functions.
*/
func checkAllVariablesSubstituted(fileContent string) error {
if unsubstitutedVars := draftVariableRegex.FindAllString(fileContent, -1); len(unsubstitutedVars) > 0 {
unsubstitutedVarsString := strings.Join(unsubstitutedVars, ", ")
return fmt.Errorf("unsubstituted variable: %s", unsubstitutedVarsString)
}
return nil
}
func replaceTemplateVariables(fileSys fs.FS, srcPath string, customInputs map[string]string) ([]byte, error) {
file, err := fs.ReadFile(fileSys, srcPath)
if err != nil {
return nil, err
}
fileString := string(file)
for oldString, newString := range customInputs {
log.Debugf("replacing %s with %s", oldString, newString)
fileString = strings.ReplaceAll(fileString, "{{"+oldString+"}}", newString)
}
return []byte(fileString), nil
}
func checkNameOverrides(fileName, srcPath, destPath string, config *config.DraftConfig) string {
if config != nil {
log.Debugf("checking name override for srcPath: %s, destPath: %s", srcPath, destPath)
if prefix := config.GetNameOverride(fileName); prefix != "" {
log.Debugf("overriding file: %s with prefix: %s", destPath, prefix)
fileName = fmt.Sprintf("%s%s", prefix, fileName)
}
}
return fileName
}