/
chezmoi.go
179 lines (155 loc) · 4.52 KB
/
chezmoi.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
167
168
169
170
171
172
173
174
175
176
177
178
179
// Package chezmoi contains chezmoi's core logic.
package chezmoi
import (
"bytes"
"crypto/sha256"
"fmt"
"io/fs"
"path/filepath"
"regexp"
"strings"
)
var (
// DefaultTemplateOptions are the default template options.
DefaultTemplateOptions = []string{"missingkey=error"}
// Skip indicates that entry should be skipped.
Skip = filepath.SkipDir
// Umask is the process's umask.
Umask = fs.FileMode(0)
)
// Prefixes and suffixes.
const (
ignorePrefix = "."
afterPrefix = "after_"
beforePrefix = "before_"
createPrefix = "create_"
dotPrefix = "dot_"
emptyPrefix = "empty_"
encryptedPrefix = "encrypted_"
exactPrefix = "exact_"
executablePrefix = "executable_"
literalPrefix = "literal_"
modifyPrefix = "modify_"
oncePrefix = "once_"
onChangePrefix = "onchange_"
privatePrefix = "private_"
readOnlyPrefix = "readonly_"
removePrefix = "remove_"
runPrefix = "run_"
symlinkPrefix = "symlink_"
literalSuffix = ".literal"
TemplateSuffix = ".tmpl"
)
// Special file names.
const (
Prefix = ".chezmoi"
RootName = Prefix + "root"
dataName = Prefix + "data"
externalName = Prefix + "external"
ignoreName = Prefix + "ignore"
removeName = Prefix + "remove"
templatesDirName = Prefix + "templates"
versionName = Prefix + "version"
)
var (
dirPrefixRegexp = regexp.MustCompile(`\A(dot|exact|literal|readonly|private)_`)
filePrefixRegexp = regexp.MustCompile(`\A(after|before|create|dot|empty|encrypted|executable|literal|modify|once|private|readonly|remove|run|symlink)_`)
fileSuffixRegexp = regexp.MustCompile(`\.(literal|tmpl)\z`)
)
// knownPrefixedFiles is a set of known filenames with the .chezmoi prefix.
var knownPrefixedFiles = newStringSet(
Prefix+".json"+TemplateSuffix,
Prefix+".toml"+TemplateSuffix,
Prefix+".yaml"+TemplateSuffix,
RootName,
dataName,
externalName+".json",
externalName+".toml",
externalName+".yaml",
ignoreName,
removeName,
versionName,
)
var modeTypeNames = map[fs.FileMode]string{
0: "file",
fs.ModeDir: "dir",
fs.ModeSymlink: "symlink",
fs.ModeNamedPipe: "named pipe",
fs.ModeSocket: "socket",
fs.ModeDevice: "device",
fs.ModeCharDevice: "char device",
}
type inconsistentStateError struct {
targetRelPath RelPath
origins []string
}
func (e *inconsistentStateError) Error() string {
return fmt.Sprintf("%s: inconsistent state (%s)", e.targetRelPath, strings.Join(e.origins, ", "))
}
type notInAbsDirError struct {
pathAbsPath AbsPath
dirAbsPath AbsPath
}
func (e *notInAbsDirError) Error() string {
return fmt.Sprintf("%s: not in %s", e.pathAbsPath, e.dirAbsPath)
}
type notInRelDirError struct {
pathRelPath RelPath
dirRelPath RelPath
}
func (e *notInRelDirError) Error() string {
return fmt.Sprintf("%s: not in %s", e.pathRelPath, e.dirRelPath)
}
type unsupportedFileTypeError struct {
absPath AbsPath
mode fs.FileMode
}
func (e *unsupportedFileTypeError) Error() string {
return fmt.Sprintf("%s: unsupported file type %s", e.absPath, modeTypeName(e.mode))
}
// SHA256Sum returns the SHA256 sum of data.
func SHA256Sum(data []byte) []byte {
sha256SumArr := sha256.Sum256(data)
return sha256SumArr[:]
}
// SuspiciousSourceDirEntry returns true if base is a suspicious dir entry.
func SuspiciousSourceDirEntry(base string, info fs.FileInfo) bool {
switch info.Mode().Type() {
case 0:
return strings.HasPrefix(base, Prefix) && !knownPrefixedFiles.contains(base)
case fs.ModeDir:
return strings.HasPrefix(base, Prefix) && base != templatesDirName
case fs.ModeSymlink:
return strings.HasPrefix(base, Prefix)
default:
return true
}
}
// isEmpty returns true if data is empty after trimming whitespace from both
// ends.
func isEmpty(data []byte) bool {
return len(bytes.TrimSpace(data)) == 0
}
// modeTypeName returns a string representation of mode.
func modeTypeName(mode fs.FileMode) string {
if name, ok := modeTypeNames[mode.Type()]; ok {
return name
}
return fmt.Sprintf("0o%o: unknown type", mode.Type())
}
// mustTrimPrefix is like strings.TrimPrefix but panics if s is not prefixed by
// prefix.
func mustTrimPrefix(s, prefix string) string {
if !strings.HasPrefix(s, prefix) {
panic(fmt.Sprintf("%s: not prefixed by %s", s, prefix))
}
return s[len(prefix):]
}
// mustTrimSuffix is like strings.TrimSuffix but panics if s is not suffixed by
// suffix.
func mustTrimSuffix(s, suffix string) string {
if !strings.HasSuffix(s, suffix) {
panic(fmt.Sprintf("%s: not suffixed by %s", s, suffix))
}
return s[:len(s)-len(suffix)]
}