-
Notifications
You must be signed in to change notification settings - Fork 472
/
verbosemutator.go
162 lines (147 loc) · 4.35 KB
/
verbosemutator.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
package chezmoi
import (
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/pkg/diff"
"github.com/pkg/diff/write"
)
// A VerboseMutator wraps an Mutator and logs all of the actions it executes and
// any errors as pseudo shell commands.
type VerboseMutator struct {
m Mutator
w io.Writer
colored bool
maxDiffDataSize int
}
// NewVerboseMutator returns a new VerboseMutator.
func NewVerboseMutator(w io.Writer, m Mutator, colored bool, maxDiffDataSize int) *VerboseMutator {
return &VerboseMutator{
m: m,
w: w,
colored: colored,
maxDiffDataSize: maxDiffDataSize,
}
}
// Chmod implements Mutator.Chmod.
func (m *VerboseMutator) Chmod(name string, mode os.FileMode) error {
action := fmt.Sprintf("chmod %o %s", mode, MaybeShellQuote(name))
err := m.m.Chmod(name, mode)
if err == nil {
_, _ = fmt.Fprintln(m.w, action)
} else {
_, _ = fmt.Fprintf(m.w, "%s: %v\n", action, err)
}
return err
}
// IdempotentCmdOutput implements Mutator.IdempotentCmdOutput.
func (m *VerboseMutator) IdempotentCmdOutput(cmd *exec.Cmd) ([]byte, error) {
action := cmdString(cmd)
output, err := m.m.IdempotentCmdOutput(cmd)
if err != nil {
_, _ = fmt.Fprintf(m.w, "%s: %v\n", action, err)
}
return output, err
}
// Mkdir implements Mutator.Mkdir.
func (m *VerboseMutator) Mkdir(name string, perm os.FileMode) error {
action := fmt.Sprintf("mkdir -m %o %s", perm, MaybeShellQuote(name))
err := m.m.Mkdir(name, perm)
if err == nil {
_, _ = fmt.Fprintln(m.w, action)
} else {
_, _ = fmt.Fprintf(m.w, "%s: %v\n", action, err)
}
return err
}
// RemoveAll implements Mutator.RemoveAll.
func (m *VerboseMutator) RemoveAll(name string) error {
action := fmt.Sprintf("rm -rf %s", MaybeShellQuote(name))
err := m.m.RemoveAll(name)
if err == nil {
_, _ = fmt.Fprintln(m.w, action)
} else {
_, _ = fmt.Fprintf(m.w, "%s: %v\n", action, err)
}
return err
}
// Rename implements Mutator.Rename.
func (m *VerboseMutator) Rename(oldpath, newpath string) error {
action := fmt.Sprintf("mv %s %s", MaybeShellQuote(oldpath), MaybeShellQuote(newpath))
err := m.m.Rename(oldpath, newpath)
if err == nil {
_, _ = fmt.Fprintln(m.w, action)
} else {
_, _ = fmt.Fprintf(m.w, "%s: %v\n", action, err)
}
return err
}
// RunCmd implements Mutator.RunCmd.
func (m *VerboseMutator) RunCmd(cmd *exec.Cmd) error {
action := cmdString(cmd)
err := m.m.RunCmd(cmd)
if err == nil {
_, _ = fmt.Fprintln(m.w, action)
} else {
_, _ = fmt.Fprintf(m.w, "%s: %v\n", action, err)
}
return err
}
// Stat implements Mutator.Stat.
func (m *VerboseMutator) Stat(name string) (os.FileInfo, error) {
return m.m.Stat(name)
}
// WriteFile implements Mutator.WriteFile.
func (m *VerboseMutator) WriteFile(name string, data []byte, perm os.FileMode, currData []byte) error {
action := fmt.Sprintf("install -m %o /dev/null %s", perm, MaybeShellQuote(name))
err := m.m.WriteFile(name, data, perm, currData)
if err == nil {
_, _ = fmt.Fprintln(m.w, action)
// Don't print diffs if either file is binary.
if isBinary(currData) || isBinary(data) {
return nil
}
// Don't print diffs if either file is too large.
if m.maxDiffDataSize != 0 {
if len(currData) > m.maxDiffDataSize || len(data) > m.maxDiffDataSize {
return nil
}
}
var opts []write.Option
if m.colored {
opts = append(opts, write.TerminalColor())
}
if err := diff.Text(filepath.Join("a", name), filepath.Join("b", name), string(currData), string(data), m.w, opts...); err != nil {
return err
}
} else {
_, _ = fmt.Fprintf(m.w, "%s: %v\n", action, err)
}
return err
}
// WriteSymlink implements Mutator.WriteSymlink.
func (m *VerboseMutator) WriteSymlink(oldname, newname string) error {
action := fmt.Sprintf("ln -sf %s %s", MaybeShellQuote(oldname), MaybeShellQuote(newname))
err := m.m.WriteSymlink(oldname, newname)
if err == nil {
_, _ = fmt.Fprintln(m.w, action)
} else {
_, _ = fmt.Fprintf(m.w, "%s: %v\n", action, err)
}
return err
}
// cmdString returns a string representation of cmd.
func cmdString(cmd *exec.Cmd) string {
s := ShellQuoteArgs(append([]string{cmd.Path}, cmd.Args[1:]...))
if cmd.Dir == "" {
return s
}
return fmt.Sprintf("( cd %s && %s )", MaybeShellQuote(cmd.Dir), s)
}
func isBinary(data []byte) bool {
return len(data) != 0 && !strings.HasPrefix(http.DetectContentType(data), "text/")
}