-
Notifications
You must be signed in to change notification settings - Fork 58
/
osutil.go
130 lines (112 loc) · 3.18 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
package osutil
import (
"fmt"
"io/fs"
"os"
"runtime"
"strings"
"syscall"
"github.com/Azure/draft/pkg/config"
log "github.com/sirupsen/logrus"
)
// 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) error {
files, err := fs.ReadDir(fileSys, src)
if err != nil {
return err
}
for _, f := range files {
if f.Name() == "draft.yaml" {
continue
}
srcPath := src + "/" + f.Name()
destPath := dest + "/" + f.Name()
if f.IsDir() {
if err = EnsureDirectory(destPath); err != nil {
return err
}
if err = CopyDir(fileSys, srcPath, destPath, config, customInputs); err != nil {
return err
}
} else {
file, err := fs.ReadFile(fileSys, srcPath)
if err != nil {
return err
}
fileString := string(file)
for oldString, newString := range customInputs {
log.Debugf("replacing %s with %s", oldString, newString)
fileString = strings.ReplaceAll(fileString, "{{"+oldString+"}}", newString)
}
fileName := f.Name()
if config != nil {
log.Debugf("checking name override for srcPath: %s, destPath: %s, destPrefix: %s/",
srcPath, destPath, dest)
if prefix := config.GetNameOverride(fileName); prefix != "" {
log.Debugf("overriding file: %s with prefix: %s", destPath, prefix)
fileName = fmt.Sprintf("%s%s", prefix, fileName)
}
}
if err = os.WriteFile(fmt.Sprintf("%s/%s", dest, fileName), []byte(fileString), 0644); err != nil {
return err
}
}
}
return nil
}