/
dryrunsystem.go
142 lines (117 loc) · 3.32 KB
/
dryrunsystem.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
package chezmoi
import (
"io/fs"
"os/exec"
"time"
vfs "github.com/twpayne/go-vfs/v5"
)
// DryRunSystem is an System that reads from, but does not write to, to
// a wrapped System.
type DryRunSystem struct {
system System
modified bool
}
// NewDryRunSystem returns a new DryRunSystem that wraps fs.
func NewDryRunSystem(system System) *DryRunSystem {
return &DryRunSystem{
system: system,
}
}
// Chmod implements System.Chmod.
func (s *DryRunSystem) Chmod(name AbsPath, mode fs.FileMode) error {
s.setModified()
return nil
}
// Chtimes implements System.Chtimes.
func (s *DryRunSystem) Chtimes(name AbsPath, atime, mtime time.Time) error {
s.setModified()
return nil
}
// Glob implements System.Glob.
func (s *DryRunSystem) Glob(pattern string) ([]string, error) {
return s.system.Glob(pattern)
}
// Link implements System.Link.
func (s *DryRunSystem) Link(oldname, newname AbsPath) error {
s.setModified()
return nil
}
// Lstat implements System.Lstat.
func (s *DryRunSystem) Lstat(name AbsPath) (fs.FileInfo, error) {
return s.system.Lstat(name)
}
// Mkdir implements System.Mkdir.
func (s *DryRunSystem) Mkdir(name AbsPath, perm fs.FileMode) error {
s.setModified()
return nil
}
// Modified returns true if a method that would have modified the wrapped system
// has been called.
func (s *DryRunSystem) Modified() bool {
return s.modified
}
// RawPath implements System.RawPath.
func (s *DryRunSystem) RawPath(path AbsPath) (AbsPath, error) {
return s.system.RawPath(path)
}
// ReadDir implements System.ReadDir.
func (s *DryRunSystem) ReadDir(name AbsPath) ([]fs.DirEntry, error) {
return s.system.ReadDir(name)
}
// ReadFile implements System.ReadFile.
func (s *DryRunSystem) ReadFile(name AbsPath) ([]byte, error) {
return s.system.ReadFile(name)
}
// Readlink implements System.Readlink.
func (s *DryRunSystem) Readlink(name AbsPath) (string, error) {
return s.system.Readlink(name)
}
// Remove implements System.Remove.
func (s *DryRunSystem) Remove(AbsPath) error {
s.setModified()
return nil
}
// RemoveAll implements System.RemoveAll.
func (s *DryRunSystem) RemoveAll(AbsPath) error {
s.setModified()
return nil
}
// Rename implements System.Rename.
func (s *DryRunSystem) Rename(oldpath, newpath AbsPath) error {
s.setModified()
return nil
}
// RunCmd implements System.RunCmd.
func (s *DryRunSystem) RunCmd(cmd *exec.Cmd) error {
s.setModified()
return nil
}
// RunScript implements System.RunScript.
func (s *DryRunSystem) RunScript(scriptname RelPath, dir AbsPath, data []byte, options RunScriptOptions) error {
s.setModified()
return nil
}
// Stat implements System.Stat.
func (s *DryRunSystem) Stat(name AbsPath) (fs.FileInfo, error) {
return s.system.Stat(name)
}
// UnderlyingFS implements System.UnderlyingFS.
func (s *DryRunSystem) UnderlyingFS() vfs.FS {
return s.system.UnderlyingFS()
}
// WriteFile implements System.WriteFile.
func (s *DryRunSystem) WriteFile(AbsPath, []byte, fs.FileMode) error {
s.setModified()
return nil
}
// WriteSymlink implements System.WriteSymlink.
func (s *DryRunSystem) WriteSymlink(string, AbsPath) error {
s.setModified()
return nil
}
// setModified sets the modified flag to true. It is a separate function so that
// it can act as a convenient breakpoint for detecting modifications to the
// underlying system.
func (s *DryRunSystem) setModified() {
s.modified = true
}