From 8222dd7be72bef3773eb66b04af6094e584b13fb Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Sun, 9 May 2021 12:44:29 +0200 Subject: [PATCH] changes: change dry-run messages to use "will" Don't suggest that the change already took place. "dry-run" is still printed to make it clear why no changes were made. --- cmd/safcm/main_sync_test.go | 8 ++++---- cmd/safcm/sync_changes.go | 28 +++++++++++++++++----------- cmd/safcm/sync_changes_test.go | 22 +++++++++++----------- 3 files changed, 32 insertions(+), 26 deletions(-) diff --git a/cmd/safcm/main_sync_test.go b/cmd/safcm/main_sync_test.go index 0b54aef..8726307 100644 --- a/cmd/safcm/main_sync_test.go +++ b/cmd/safcm/main_sync_test.go @@ -217,7 +217,7 @@ func TestSyncSshEndToEnd(t *testing.T) { []string{"-n", "no-effect-commands.example.org"}, `[info] [no-effect-commands.example.org] remote helper upload in progress [info] [no-effect-commands.example.org] -executed 2 command(s): (dry-run) +will execute 2 command(s): (dry-run) "echo this is a command" "true" `, @@ -232,7 +232,7 @@ executed 2 command(s): (dry-run) [verbose] [no-effect-commands.example.org] host groups: all no-effect-commands.example.org [verbose] [no-effect-commands.example.org] host group priorities (descending): no-effect-commands.example.org [info] [no-effect-commands.example.org] -executed 2 command(s): (dry-run) +will execute 2 command(s): (dry-run) "echo this is a command" "true" `, @@ -280,7 +280,7 @@ executed 2 command(s): []string{"-n", "no-effect-commands-failing.example.org"}, `[info] [no-effect-commands-failing.example.org] remote helper upload in progress [info] [no-effect-commands-failing.example.org] -executed 2 command(s): (dry-run) +will execute 2 command(s): (dry-run) "echo this is a command" "echo failing; false" `, @@ -295,7 +295,7 @@ executed 2 command(s): (dry-run) [verbose] [no-effect-commands-failing.example.org] host groups: all no-effect-commands-failing.example.org [verbose] [no-effect-commands-failing.example.org] host group priorities (descending): no-effect-commands-failing.example.org [info] [no-effect-commands-failing.example.org] -executed 2 command(s): (dry-run) +will execute 2 command(s): (dry-run) "echo this is a command" "echo failing; false" `, diff --git a/cmd/safcm/sync_changes.go b/cmd/safcm/sync_changes.go index 3e91768..f597ae1 100644 --- a/cmd/safcm/sync_changes.go +++ b/cmd/safcm/sync_changes.go @@ -66,11 +66,12 @@ func (s *Sync) formatChanges(resp safcm.MsgSyncResp) string { func (s *Sync) formatFileChanges(changes []safcm.FileChange) string { var buf strings.Builder - fmt.Fprintf(&buf, "changed %d file(s):", len(changes)) if s.config.DryRun { - fmt.Fprintf(&buf, " (dry-run)") + fmt.Fprintf(&buf, "will change %d file(s): (dry-run)\n", + len(changes)) + } else { + fmt.Fprintf(&buf, "changed %d file(s):\n", len(changes)) } - fmt.Fprintf(&buf, "\n") for _, x := range changes { fmt.Fprintf(&buf, "%s:", s.formatTarget(x.Path)) @@ -116,7 +117,6 @@ func (s *Sync) formatFileChanges(changes []safcm.FileChange) string { } fmt.Fprintf(&buf, "\n") } - return buf.String() } func formatFileType(info safcm.FileChangeInfo) string { @@ -142,11 +142,12 @@ func formatFilePerm(info safcm.FileChangeInfo) string { func (s *Sync) formatPackageChanges(changes []safcm.PackageChange) string { var buf strings.Builder - fmt.Fprintf(&buf, "installed %d package(s):", len(changes)) if s.config.DryRun { - fmt.Fprintf(&buf, " (dry-run)") + fmt.Fprintf(&buf, "will install %d package(s): (dry-run)\n", + len(changes)) + } else { + fmt.Fprintf(&buf, "installed %d package(s):\n", len(changes)) } - fmt.Fprintf(&buf, "\n") for _, x := range changes { // TODO: indicate if installation failed fmt.Fprintf(&buf, "%s\n", s.formatTarget(x.Name)) @@ -156,11 +157,12 @@ func (s *Sync) formatPackageChanges(changes []safcm.PackageChange) string { func (s *Sync) formatServiceChanges(changes []safcm.ServiceChange) string { var buf strings.Builder - fmt.Fprintf(&buf, "modified %d service(s):", len(changes)) if s.config.DryRun { - fmt.Fprintf(&buf, " (dry-run)") + fmt.Fprintf(&buf, "will modify %d service(s): (dry-run)\n", + len(changes)) + } else { + fmt.Fprintf(&buf, "modified %d service(s):\n", len(changes)) } - fmt.Fprintf(&buf, "\n") for _, x := range changes { var info []string if x.Started { @@ -197,7 +199,11 @@ func (s *Sync) formatCommandChanges(changes []safcm.CommandChange) string { } var buf strings.Builder - fmt.Fprintf(&buf, "executed %d command(s)", len(changes)) + if s.config.DryRun { + fmt.Fprintf(&buf, "will execute %d command(s)", len(changes)) + } else { + fmt.Fprintf(&buf, "executed %d command(s)", len(changes)) + } if noOutput > 0 && !s.config.DryRun { fmt.Fprintf(&buf, ", %d with no output", noOutput) } diff --git a/cmd/safcm/sync_changes_test.go b/cmd/safcm/sync_changes_test.go index febb784..feb8dd3 100644 --- a/cmd/safcm/sync_changes_test.go +++ b/cmd/safcm/sync_changes_test.go @@ -125,7 +125,7 @@ func TestFormatChanges(t *testing.T) { }, }, }, - "\nexecuted 5 command(s): (dry-run)\n\"fake command\"\n\"fake command with no output\"\n\"fake command with newline\"\n\"fake command with more output\"\n\"fake failed command\"\n", + "\nwill execute 5 command(s): (dry-run)\n\"fake command\"\n\"fake command with no output\"\n\"fake command with newline\"\n\"fake command with more output\"\n\"fake failed command\"\n", }, { "command changes only, quiet & dry-run", @@ -151,7 +151,7 @@ func TestFormatChanges(t *testing.T) { }, }, }, - "executed 5 command(s) (dry-run)\n", + "will execute 5 command(s) (dry-run)\n", }, } @@ -543,7 +543,7 @@ func TestFormatFileChanges(t *testing.T) { }, }, }, - `changed 1 file(s): (dry-run) + `will change 1 file(s): (dry-run) "file": created, file, user(1000) group(2000), 0644 `, }, @@ -565,7 +565,7 @@ func TestFormatFileChanges(t *testing.T) { }, }, }, - "changed 1 file(s): (dry-run)\n\x1B[36m\"file\"\x1B[0m: \x1B[32mcreated\x1B[0m, file, user(1000) group(2000), 0644\n", + "will change 1 file(s): (dry-run)\n\x1B[36m\"file\"\x1B[0m: \x1B[32mcreated\x1B[0m, file, user(1000) group(2000), 0644\n", }, { @@ -723,7 +723,7 @@ func TestFormatPackageChanges(t *testing.T) { Name: "package-two", }, }, - `installed 2 package(s): (dry-run) + `will install 2 package(s): (dry-run) "package-one" "package-two" `, @@ -741,7 +741,7 @@ func TestFormatPackageChanges(t *testing.T) { Name: "package-two", }, }, - "installed 2 package(s): (dry-run)\n\x1b[36m\"package-one\"\x1b[0m\n\x1b[36m\"package-two\"\x1b[0m\n", + "will install 2 package(s): (dry-run)\n\x1b[36m\"package-one\"\x1b[0m\n\x1b[36m\"package-two\"\x1b[0m\n", }, { @@ -862,7 +862,7 @@ func TestFormatServiceChanges(t *testing.T) { Enabled: true, }, }, - `modified 3 service(s): (dry-run) + `will modify 3 service(s): (dry-run) "service-one": started "service-two": enabled "service-three": started, enabled @@ -888,7 +888,7 @@ func TestFormatServiceChanges(t *testing.T) { Enabled: true, }, }, - "modified 3 service(s): (dry-run)\n\x1b[36m\"service-one\"\x1b[0m: started\n\x1b[36m\"service-two\"\x1b[0m: enabled\n\x1b[36m\"service-three\"\x1b[0m: started, enabled\n", + "will modify 3 service(s): (dry-run)\n\x1b[36m\"service-one\"\x1b[0m: started\n\x1b[36m\"service-two\"\x1b[0m: enabled\n\x1b[36m\"service-three\"\x1b[0m: started, enabled\n", }, { @@ -1038,7 +1038,7 @@ func TestFormatCommandChanges(t *testing.T) { Command: "fake command", }, }, - `executed 1 command(s): (dry-run) + `will execute 1 command(s): (dry-run) "fake command" `, }, @@ -1053,7 +1053,7 @@ func TestFormatCommandChanges(t *testing.T) { Command: "fake command", }, }, - "executed 1 command(s): (dry-run)\n\x1b[36m\"fake command\"\x1b[0m\n", + "will execute 1 command(s): (dry-run)\n\x1b[36m\"fake command\"\x1b[0m\n", }, { @@ -1187,7 +1187,7 @@ func TestFormatCommandChanges(t *testing.T) { Command: "fake failed command", }, }, - `executed 5 command(s) (dry-run) + `will execute 5 command(s) (dry-run) `, }, -- 2.43.2