-
Notifications
You must be signed in to change notification settings - Fork 469
/
dumpsystem.go
152 lines (133 loc) · 3.77 KB
/
dumpsystem.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
package chezmoi
import (
"io/fs"
"os/exec"
vfs "github.com/twpayne/go-vfs/v4"
)
// A dataType is a data type.
type dataType string
// dataTypes.
const (
dataTypeCommand dataType = "command"
dataTypeDir dataType = "dir"
dataTypeFile dataType = "file"
dataTypeScript dataType = "script"
dataTypeSymlink dataType = "symlink"
)
// A DumpSystem is a System that writes to a data file.
type DumpSystem struct {
emptySystemMixin
noUpdateSystemMixin
data map[string]any
}
// A commandData contains data about a command.
type commandData struct {
Type dataType `json:"type" yaml:"type"`
Path string `json:"path" yaml:"path"`
Args []string `json:"args" yaml:"args"`
}
// A dirData contains data about a directory.
type dirData struct {
Type dataType `json:"type" yaml:"type"`
Name AbsPath `json:"name" yaml:"name"`
Perm fs.FileMode `json:"perm" yaml:"perm"`
}
// A fileData contains data about a file.
type fileData struct {
Type dataType `json:"type" yaml:"type"`
Name AbsPath `json:"name" yaml:"name"`
Contents string `json:"contents" yaml:"contents"`
Perm fs.FileMode `json:"perm" yaml:"perm"`
}
// A scriptData contains data about a script.
type scriptData struct {
Type dataType `json:"type" yaml:"type"`
Name AbsPath `json:"name" yaml:"name"`
Contents string `json:"contents" yaml:"contents"`
Condition string `json:"condition" yaml:"condition"`
Interpreter *Interpreter `json:"interpreter,omitempty" yaml:"interpreter,omitempty"`
}
// A symlinkData contains data about a symlink.
type symlinkData struct {
Type dataType `json:"type" yaml:"type"`
Name AbsPath `json:"name" yaml:"name"`
Linkname string `json:"linkname" yaml:"linkname"`
}
// NewDumpSystem returns a new DumpSystem that accumulates data.
func NewDumpSystem() *DumpSystem {
return &DumpSystem{
data: make(map[string]any),
}
}
// Data returns s's data.
func (s *DumpSystem) Data() any {
return s.data
}
// Mkdir implements System.Mkdir.
func (s *DumpSystem) Mkdir(dirname AbsPath, perm fs.FileMode) error {
return s.setData(dirname.String(), &dirData{
Type: dataTypeDir,
Name: dirname,
Perm: perm,
})
}
// RunCmd implements System.RunCmd.
func (s *DumpSystem) RunCmd(cmd *exec.Cmd) error {
if cmd.Dir == "" {
return nil
}
return s.setData(cmd.Dir, &commandData{
Type: dataTypeCommand,
Path: cmd.Path,
Args: cmd.Args,
})
}
// RunScript implements System.RunScript.
func (s *DumpSystem) RunScript(
scriptname RelPath,
dir AbsPath,
data []byte,
options RunScriptOptions,
) error {
scriptnameStr := scriptname.String()
scriptData := &scriptData{
Type: dataTypeScript,
Name: NewAbsPath(scriptnameStr),
Contents: string(data),
}
if options.Condition != ScriptConditionNone {
scriptData.Condition = string(options.Condition)
}
if !options.Interpreter.None() {
scriptData.Interpreter = options.Interpreter
}
return s.setData(scriptnameStr, scriptData)
}
// UnderlyingFS implements System.UnderlyingFS.
func (s *DumpSystem) UnderlyingFS() vfs.FS {
return nil
}
// WriteFile implements System.WriteFile.
func (s *DumpSystem) WriteFile(filename AbsPath, data []byte, perm fs.FileMode) error {
return s.setData(filename.String(), &fileData{
Type: dataTypeFile,
Name: filename,
Contents: string(data),
Perm: perm,
})
}
// WriteSymlink implements System.WriteSymlink.
func (s *DumpSystem) WriteSymlink(oldname string, newname AbsPath) error {
return s.setData(newname.String(), &symlinkData{
Type: dataTypeSymlink,
Name: newname,
Linkname: oldname,
})
}
func (s *DumpSystem) setData(key string, value any) error {
if _, ok := s.data[key]; ok {
return fs.ErrExist
}
s.data[key] = value
return nil
}