]> ruderich.org/simon Gitweb - safcm/safcm.git/commitdiff
changes: shorten output to one line with quiet and only silent commands
authorSimon Ruderich <simon@ruderich.org>
Sat, 10 Apr 2021 18:24:03 +0000 (20:24 +0200)
committerSimon Ruderich <simon@ruderich.org>
Sat, 10 Apr 2021 18:24:03 +0000 (20:24 +0200)
cmd/safcm/sync_changes.go
cmd/safcm/sync_changes_test.go

index 6327fd9addbaec497a31452a635082cb6f65b32e..a85b8100946da96a6bf2670a2a4f0d32917eba2e 100644 (file)
@@ -51,7 +51,16 @@ func (s *Sync) formatChanges(resp safcm.MsgSyncResp) string {
        if len(changes) == 0 {
                return ""
        }
-       return "\n" + strings.Join(changes, "\n")
+
+       x := strings.Join(changes, "\n")
+       // If quiet is used and only commands without output were executed
+       // then don't prepend a newline so that the whole change output of a
+       // host fits in a single line. This makes the output much more
+       // readable with multiple hosts.
+       if strings.Count(x, "\n") == 1 {
+               return x
+       }
+       return "\n" + x
 }
 
 func (s *Sync) formatFileChanges(changes []safcm.FileChange) string {
index c1e4aef899e257eeef2cb832282b6cca9b29770a..6c7ba392aac7b36b01399eb7e1d8c6fba4af9b8c 100644 (file)
@@ -28,6 +28,7 @@ func TestFormatChanges(t *testing.T) {
        tests := []struct {
                name   string
                dryRun bool
+               quiet  bool
                isTTY  bool
                resp   safcm.MsgSyncResp
                exp    string
@@ -40,6 +41,7 @@ func TestFormatChanges(t *testing.T) {
                        "no changes",
                        false,
                        false,
+                       false,
                        safcm.MsgSyncResp{},
                        "",
                },
@@ -48,6 +50,7 @@ func TestFormatChanges(t *testing.T) {
                        "changes",
                        false,
                        false,
+                       false,
                        safcm.MsgSyncResp{
                                FileChanges: []safcm.FileChange{
                                        {
@@ -97,6 +100,59 @@ func TestFormatChanges(t *testing.T) {
                        },
                        "\nchanged 1 file(s):\n\"created\": created, file, user(1000) group(2000), 0644\n\ninstalled 2 package(s):\n\"package-one\"\n\"package-two\"\n\nmodified 3 service(s):\n\"service-one\": started\n\"service-two\": enabled\n\"service-three\": started, enabled\n\nexecuted 2 command(s):\n\"fake command\":\n   > fake output\n   > \\ No newline at end of file\n\"fake command with no output\"\n",
                },
+
+               {
+                       "command changes only, dry-run",
+                       true,
+                       false,
+                       false,
+                       safcm.MsgSyncResp{
+                               CommandChanges: []safcm.CommandChange{
+                                       {
+                                               Command: "fake command",
+                                       },
+                                       {
+                                               Command: "fake command with no output",
+                                       },
+                                       {
+                                               Command: "fake command with newline",
+                                       },
+                                       {
+                                               Command: "fake command with more output",
+                                       },
+                                       {
+                                               Command: "fake failed command",
+                                       },
+                               },
+                       },
+                       "\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",
+               },
+               {
+                       "command changes only, quiet & dry-run",
+                       true,
+                       true,
+                       false,
+                       safcm.MsgSyncResp{
+                               CommandChanges: []safcm.CommandChange{
+                                       {
+                                               Command: "fake command",
+                                       },
+                                       {
+                                               Command: "fake command with no output",
+                                       },
+                                       {
+                                               Command: "fake command with newline",
+                                       },
+                                       {
+                                               Command: "fake command with more output",
+                                       },
+                                       {
+                                               Command: "fake failed command",
+                                       },
+                               },
+                       },
+                       "executed 5 command(s) (dry-run)\n",
+               },
        }
 
        for _, tc := range tests {
@@ -104,6 +160,7 @@ func TestFormatChanges(t *testing.T) {
                        s := &Sync{
                                config: &config.Config{
                                        DryRun: tc.dryRun,
+                                       Quiet:  tc.quiet,
                                },
                                isTTY: tc.isTTY,
                        }