/
entrystate.go
84 lines (74 loc) · 2.23 KB
/
entrystate.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
package chezmoi
import (
"bytes"
"io/fs"
"log/slog"
"runtime"
"github.com/twpayne/chezmoi/v2/internal/chezmoilog"
)
// An EntryStateType is an entry state type.
type EntryStateType string
// Entry state types.
const (
EntryStateTypeDir EntryStateType = "dir"
EntryStateTypeFile EntryStateType = "file"
EntryStateTypeSymlink EntryStateType = "symlink"
EntryStateTypeRemove EntryStateType = "remove"
EntryStateTypeScript EntryStateType = "script"
)
// An EntryState represents the state of an entry. A nil EntryState is
// equivalent to EntryStateTypeAbsent.
type EntryState struct {
Type EntryStateType `json:"type" yaml:"type"`
Mode fs.FileMode `json:"mode,omitempty" yaml:"mode,omitempty"`
ContentsSHA256 HexBytes `json:"contentsSHA256,omitempty" yaml:"contentsSHA256,omitempty"` //nolint:tagliatelle
contents []byte
overwrite bool
}
// Contents returns s's contents, if available.
func (s *EntryState) Contents() []byte {
return s.contents
}
// Equal returns true if s is equal to other.
func (s *EntryState) Equal(other *EntryState) bool {
if s.Type != other.Type {
return false
}
if runtime.GOOS != "windows" && s.Mode.Perm() != other.Mode.Perm() {
return false
}
return bytes.Equal(s.ContentsSHA256, other.ContentsSHA256)
}
// Equivalent returns true if s is equivalent to other.
func (s *EntryState) Equivalent(other *EntryState) bool {
switch {
case s == nil:
return other == nil || other.Type == EntryStateTypeRemove
case other == nil:
return s.Type == EntryStateTypeRemove
default:
return s.Equal(other)
}
}
// LogValue implements log/slog.LogValuer.LogValue.
func (s *EntryState) LogValue() slog.Value {
if s == nil {
return slog.Value{}
}
attrs := []slog.Attr{
slog.String("Type", string(s.Type)),
slog.Int("Mode", int(s.Mode)),
chezmoilog.Stringer("ContentsSHA256", s.ContentsSHA256),
}
if len(s.contents) != 0 {
attrs = append(attrs, chezmoilog.FirstFewBytes("contents", s.contents))
}
if s.overwrite {
attrs = append(attrs, slog.Bool("overwrite", s.overwrite))
}
return slog.GroupValue(attrs...)
}
// Overwrite returns true if s should be overwritten by default.
func (s *EntryState) Overwrite() bool {
return s.overwrite
}