X-Git-Url: https://ruderich.org/simon/gitweb/?a=blobdiff_plain;f=cmd%2Fsafcm%2Fsync_changes.go;h=1ec3cb60b435632fedac3e95b975239d1f12160e;hb=ccde6eed234a8530c8180264937566ca5d092bdc;hp=14d73c271a4bb0d8434d5896bf17bd78a4552407;hpb=f2f2bc47e8729548f3c10117f7f008b547c4afc5;p=safcm%2Fsafcm.git diff --git a/cmd/safcm/sync_changes.go b/cmd/safcm/sync_changes.go index 14d73c2..1ec3cb6 100644 --- a/cmd/safcm/sync_changes.go +++ b/cmd/safcm/sync_changes.go @@ -30,6 +30,30 @@ import ( // the remote helper is untrusted and must be either escaped with %q or by // calling EscapeControlCharacters(). +func (s *Sync) formatChanges(resp safcm.MsgSyncResp) string { + var changes []string + if len(resp.FileChanges) > 0 { + changes = append(changes, + s.formatFileChanges(resp.FileChanges)) + } + if len(resp.PackageChanges) > 0 { + changes = append(changes, + s.formatPackageChanges(resp.PackageChanges)) + } + if len(resp.ServiceChanges) > 0 { + changes = append(changes, + s.formatServiceChanges(resp.ServiceChanges)) + } + if len(resp.CommandChanges) > 0 { + changes = append(changes, + s.formatCommandChanges(resp.CommandChanges)) + } + if len(changes) == 0 { + return "" + } + return "\n" + strings.Join(changes, "\n") +} + func (s *Sync) formatFileChanges(changes []safcm.FileChange) string { var buf strings.Builder fmt.Fprintf(&buf, "changed %d file(s):", len(changes)) @@ -145,13 +169,41 @@ func (s *Sync) formatServiceChanges(changes []safcm.ServiceChange) string { func (s *Sync) formatCommandChanges(changes []safcm.CommandChange) string { const indent = " > " + // Quiet hides all successful, non-trigger commands which produce no + // output. This is useful as many commands will be used to enforce a + // certain state (e.g. file not-present, `ainsl`, etc.) and are run on + // each sync. Displaying them provides not much useful information. + // Instead, quiet shows them only when they produce output (e.g. + // `ainsl`, `rm -v`) and thus modify the host's state. + var noOutput int + if s.config.Quiet && !s.config.DryRun { + for _, x := range changes { + if x.Trigger == "" && + x.Error == "" && + x.Output == "" { + noOutput++ + } + } + } + var buf strings.Builder - fmt.Fprintf(&buf, "executed %d command(s):", len(changes)) + fmt.Fprintf(&buf, "executed %d command(s)", len(changes)) + if noOutput > 0 { + fmt.Fprintf(&buf, ", %d with no output", noOutput) + } + if noOutput != len(changes) { + fmt.Fprintf(&buf, ":") + } if s.config.DryRun { fmt.Fprintf(&buf, " (dry-run)") } fmt.Fprintf(&buf, "\n") for _, x := range changes { + if noOutput > 0 && + x.Trigger == "" && x.Error == "" && x.Output == "" { + continue + } + fmt.Fprintf(&buf, "%s", s.formatTarget(x.Command)) if x.Trigger != "" { fmt.Fprintf(&buf, ", trigger for %q", x.Trigger) @@ -198,10 +250,6 @@ func (s *Sync) formatDiff(diff string) string { } func indentBlock(x string, sep string) string { - if x == "" { - return "" - } - lines := strings.Split(x, "\n") if lines[len(lines)-1] == "" { lines = lines[:len(lines)-1]