]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm/sync_changes.go
First working version
[safcm/safcm.git] / cmd / safcm / sync_changes.go
1 // "sync" sub-command: format changes
2
3 // Copyright (C) 2021  Simon Ruderich
4 //
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.
9 //
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.
14 //
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/>.
17
18 package main
19
20 import (
21         "fmt"
22         "io/fs"
23         "strings"
24
25         "ruderich.org/simon/safcm"
26         "ruderich.org/simon/safcm/cmd/safcm/config"
27 )
28
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().
32
33 func (s *Sync) formatFileChanges(changes []safcm.FileChange) string {
34         var buf strings.Builder
35         fmt.Fprintf(&buf, "changed %d file(s):", len(changes))
36         if s.config.DryRun {
37                 fmt.Fprintf(&buf, " (dry-run)")
38         }
39         fmt.Fprintf(&buf, "\n")
40         for _, x := range changes {
41                 fmt.Fprintf(&buf, "%s:", s.formatTarget(x.Path))
42
43                 var info []string
44                 if x.Created {
45                         info = append(info,
46                                 ColorString(s.isTTY, ColorGreen, "created"),
47                                 formatFileType(x.New),
48                                 formatFileUserGroup(x.New),
49                                 formatFilePerm(x.New),
50                         )
51                 } else {
52                         if x.Old.Mode.Type() != x.New.Mode.Type() {
53                                 info = append(info, fmt.Sprintf("%s -> %s",
54                                         formatFileType(x.Old),
55                                         formatFileType(x.New),
56                                 ))
57                         }
58                         if x.Old.User != x.New.User ||
59                                 x.Old.Uid != x.New.Uid ||
60                                 x.Old.Group != x.New.Group ||
61                                 x.Old.Gid != x.New.Gid {
62                                 info = append(info, fmt.Sprintf("%s -> %s",
63                                         formatFileUserGroup(x.Old),
64                                         formatFileUserGroup(x.New),
65                                 ))
66                         }
67                         if config.FileModeToFullPerm(x.Old.Mode) !=
68                                 config.FileModeToFullPerm(x.New.Mode) {
69                                 info = append(info, fmt.Sprintf("%s -> %s",
70                                         formatFilePerm(x.Old),
71                                         formatFilePerm(x.New),
72                                 ))
73                         }
74                 }
75                 if len(info) > 0 {
76                         fmt.Fprint(&buf, " ")
77                         fmt.Fprint(&buf, strings.Join(info, ", "))
78                 }
79
80                 if x.DataDiff != "" {
81                         fmt.Fprintf(&buf, "\n%s", s.formatDiff(x.DataDiff))
82                 }
83                 fmt.Fprintf(&buf, "\n")
84         }
85
86         return buf.String()
87 }
88 func formatFileType(info safcm.FileChangeInfo) string {
89         switch info.Mode.Type() {
90         case 0: // regular file
91                 return "file"
92         case fs.ModeSymlink:
93                 return "symlink"
94         case fs.ModeDir:
95                 return "dir"
96         default:
97                 return fmt.Sprintf("invalid type %v", info.Mode.Type())
98         }
99 }
100 func formatFileUserGroup(info safcm.FileChangeInfo) string {
101         return fmt.Sprintf("%s(%d) %s(%d)",
102                 EscapeControlCharacters(false, info.User), info.Uid,
103                 EscapeControlCharacters(false, info.Group), info.Gid)
104 }
105 func formatFilePerm(info safcm.FileChangeInfo) string {
106         return fmt.Sprintf("%#o", config.FileModeToFullPerm(info.Mode))
107 }
108
109 func (s *Sync) formatPackageChanges(changes []safcm.PackageChange) string {
110         var buf strings.Builder
111         fmt.Fprintf(&buf, "installed %d package(s):", len(changes))
112         if s.config.DryRun {
113                 fmt.Fprintf(&buf, " (dry-run)")
114         }
115         fmt.Fprintf(&buf, "\n")
116         for _, x := range changes {
117                 // TODO: indicate if installation failed
118                 fmt.Fprintf(&buf, "%s\n", s.formatTarget(x.Name))
119         }
120         return buf.String()
121 }
122
123 func (s *Sync) formatServiceChanges(changes []safcm.ServiceChange) string {
124         var buf strings.Builder
125         fmt.Fprintf(&buf, "modified %d service(s):", len(changes))
126         if s.config.DryRun {
127                 fmt.Fprintf(&buf, " (dry-run)")
128         }
129         fmt.Fprintf(&buf, "\n")
130         for _, x := range changes {
131                 var info []string
132                 if x.Started {
133                         info = append(info, "started")
134                 }
135                 if x.Enabled {
136                         info = append(info, "enabled")
137                 }
138                 fmt.Fprintf(&buf, "%s: %s\n",
139                         s.formatTarget(x.Name),
140                         strings.Join(info, ", "))
141         }
142         return buf.String()
143 }
144
145 func (s *Sync) formatCommandChanges(changes []safcm.CommandChange) string {
146         const indent = "   > "
147
148         var buf strings.Builder
149         fmt.Fprintf(&buf, "executed %d command(s):", len(changes))
150         if s.config.DryRun {
151                 fmt.Fprintf(&buf, " (dry-run)")
152         }
153         fmt.Fprintf(&buf, "\n")
154         for _, x := range changes {
155                 fmt.Fprintf(&buf, "%s", s.formatTarget(x.Command))
156                 if x.Trigger != "" {
157                         fmt.Fprintf(&buf, ", trigger for %q", x.Trigger)
158                 }
159                 if x.Error != "" {
160                         fmt.Fprintf(&buf, ", failed: %q", x.Error)
161                 }
162                 if x.Output != "" {
163                         // TODO: truncate very large outputs?
164                         x := indentBlock(x.Output, indent)
165                         fmt.Fprintf(&buf, ":\n%s",
166                                 EscapeControlCharacters(s.isTTY, x))
167                 }
168                 fmt.Fprintf(&buf, "\n")
169         }
170         return buf.String()
171 }
172
173 func (s *Sync) formatTarget(x string) string {
174         x = fmt.Sprintf("%q", x) // escape!
175         return ColorString(s.isTTY, ColorCyan, x)
176 }
177
178 func (s *Sync) formatDiff(diff string) string {
179         const indent = "   "
180
181         diff = indentBlock(diff, indent)
182         // Never color diff content as we want to color the whole diff
183         diff = EscapeControlCharacters(false, diff)
184         if !s.isTTY {
185                 return diff
186         }
187
188         var res []string
189         for _, x := range strings.Split(diff, "\n") {
190                 if strings.HasPrefix(x, indent+"+") {
191                         x = ColorString(s.isTTY, ColorGreen, x)
192                 } else if strings.HasPrefix(x, indent+"-") {
193                         x = ColorString(s.isTTY, ColorRed, x)
194                 }
195                 res = append(res, x)
196         }
197         return strings.Join(res, "\n")
198 }
199
200 func indentBlock(x string, sep string) string {
201         if x == "" {
202                 return ""
203         }
204
205         lines := strings.Split(x, "\n")
206         if lines[len(lines)-1] == "" {
207                 lines = lines[:len(lines)-1]
208         } else {
209                 lines = append(lines, "\\ No newline at end of file")
210         }
211
212         return sep + strings.Join(lines, "\n"+sep)
213 }