1 // "sync" sub-command: format changes
3 // Copyright (C) 2021 Simon Ruderich
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
25 "ruderich.org/simon/safcm"
26 "ruderich.org/simon/safcm/cmd/safcm/config"
29 // NOTE: Be careful when implementing new format* functions. All input from
30 // the remote helper is untrusted and must be either escaped with %q or by
31 // calling EscapeControlCharacters().
33 func (s *Sync) formatChanges(resp safcm.MsgSyncResp) string {
35 if len(resp.FileChanges) > 0 {
36 changes = append(changes,
37 s.formatFileChanges(resp.FileChanges))
39 if len(resp.PackageChanges) > 0 {
40 changes = append(changes,
41 s.formatPackageChanges(resp.PackageChanges))
43 if len(resp.ServiceChanges) > 0 {
44 changes = append(changes,
45 s.formatServiceChanges(resp.ServiceChanges))
47 if len(resp.CommandChanges) > 0 {
48 changes = append(changes,
49 s.formatCommandChanges(resp.CommandChanges))
51 if len(changes) == 0 {
54 return "\n" + strings.Join(changes, "\n")
57 func (s *Sync) formatFileChanges(changes []safcm.FileChange) string {
58 var buf strings.Builder
59 fmt.Fprintf(&buf, "changed %d file(s):", len(changes))
61 fmt.Fprintf(&buf, " (dry-run)")
63 fmt.Fprintf(&buf, "\n")
64 for _, x := range changes {
65 fmt.Fprintf(&buf, "%s:", s.formatTarget(x.Path))
70 ColorString(s.isTTY, ColorGreen, "created"),
71 formatFileType(x.New),
72 formatFileUserGroup(x.New),
73 formatFilePerm(x.New),
76 if x.Old.Mode.Type() != x.New.Mode.Type() {
77 info = append(info, fmt.Sprintf("%s -> %s",
78 formatFileType(x.Old),
79 formatFileType(x.New),
82 if x.Old.User != x.New.User ||
83 x.Old.Uid != x.New.Uid ||
84 x.Old.Group != x.New.Group ||
85 x.Old.Gid != x.New.Gid {
86 info = append(info, fmt.Sprintf("%s -> %s",
87 formatFileUserGroup(x.Old),
88 formatFileUserGroup(x.New),
91 if config.FileModeToFullPerm(x.Old.Mode) !=
92 config.FileModeToFullPerm(x.New.Mode) {
93 info = append(info, fmt.Sprintf("%s -> %s",
94 formatFilePerm(x.Old),
95 formatFilePerm(x.New),
100 fmt.Fprint(&buf, " ")
101 fmt.Fprint(&buf, strings.Join(info, ", "))
104 if x.DataDiff != "" {
105 fmt.Fprintf(&buf, "\n%s", s.formatDiff(x.DataDiff))
107 fmt.Fprintf(&buf, "\n")
112 func formatFileType(info safcm.FileChangeInfo) string {
113 switch info.Mode.Type() {
114 case 0: // regular file
121 return fmt.Sprintf("invalid type %v", info.Mode.Type())
124 func formatFileUserGroup(info safcm.FileChangeInfo) string {
125 return fmt.Sprintf("%s(%d) %s(%d)",
126 EscapeControlCharacters(false, info.User), info.Uid,
127 EscapeControlCharacters(false, info.Group), info.Gid)
129 func formatFilePerm(info safcm.FileChangeInfo) string {
130 return fmt.Sprintf("%#o", config.FileModeToFullPerm(info.Mode))
133 func (s *Sync) formatPackageChanges(changes []safcm.PackageChange) string {
134 var buf strings.Builder
135 fmt.Fprintf(&buf, "installed %d package(s):", len(changes))
137 fmt.Fprintf(&buf, " (dry-run)")
139 fmt.Fprintf(&buf, "\n")
140 for _, x := range changes {
141 // TODO: indicate if installation failed
142 fmt.Fprintf(&buf, "%s\n", s.formatTarget(x.Name))
147 func (s *Sync) formatServiceChanges(changes []safcm.ServiceChange) string {
148 var buf strings.Builder
149 fmt.Fprintf(&buf, "modified %d service(s):", len(changes))
151 fmt.Fprintf(&buf, " (dry-run)")
153 fmt.Fprintf(&buf, "\n")
154 for _, x := range changes {
157 info = append(info, "started")
160 info = append(info, "enabled")
162 fmt.Fprintf(&buf, "%s: %s\n",
163 s.formatTarget(x.Name),
164 strings.Join(info, ", "))
169 func (s *Sync) formatCommandChanges(changes []safcm.CommandChange) string {
172 // Quiet hides all successful, non-trigger commands which produce no
173 // output. This is useful as many commands will be used to enforce a
174 // certain state (e.g. file not-present, `ainsl`, etc.) and are run on
175 // each sync. Displaying them provides not much useful information.
176 // Instead, quiet shows them only when they produce output (e.g.
177 // `ainsl`, `rm -v`) and thus modify the host's state.
179 if s.config.Quiet && !s.config.DryRun {
180 for _, x := range changes {
181 if x.Trigger == "" &&
189 var buf strings.Builder
190 fmt.Fprintf(&buf, "executed %d command(s)", len(changes))
192 fmt.Fprintf(&buf, ", %d with no output", noOutput)
194 if noOutput != len(changes) {
195 fmt.Fprintf(&buf, ":")
198 fmt.Fprintf(&buf, " (dry-run)")
200 fmt.Fprintf(&buf, "\n")
201 for _, x := range changes {
203 x.Trigger == "" && x.Error == "" && x.Output == "" {
207 fmt.Fprintf(&buf, "%s", s.formatTarget(x.Command))
209 fmt.Fprintf(&buf, ", trigger for %q", x.Trigger)
212 fmt.Fprintf(&buf, ", failed: %q", x.Error)
215 // TODO: truncate very large outputs?
216 x := indentBlock(x.Output, indent)
217 fmt.Fprintf(&buf, ":\n%s",
218 EscapeControlCharacters(s.isTTY, x))
220 fmt.Fprintf(&buf, "\n")
225 func (s *Sync) formatTarget(x string) string {
226 x = fmt.Sprintf("%q", x) // escape!
227 return ColorString(s.isTTY, ColorCyan, x)
230 func (s *Sync) formatDiff(diff string) string {
233 diff = indentBlock(diff, indent)
234 // Never color diff content as we want to color the whole diff
235 diff = EscapeControlCharacters(false, diff)
241 for _, x := range strings.Split(diff, "\n") {
242 if strings.HasPrefix(x, indent+"+") {
243 x = ColorString(s.isTTY, ColorGreen, x)
244 } else if strings.HasPrefix(x, indent+"-") {
245 x = ColorString(s.isTTY, ColorRed, x)
249 return strings.Join(res, "\n")
252 func indentBlock(x string, sep string) string {
253 lines := strings.Split(x, "\n")
254 if lines[len(lines)-1] == "" {
255 lines = lines[:len(lines)-1]
257 lines = append(lines, "\\ No newline at end of file")
260 return sep + strings.Join(lines, "\n"+sep)