From aac45966c96f829bca9728e61eed92f083da11a9 Mon Sep 17 00:00:00 2001 From: Andrej Shadura Date: Fri, 15 Apr 2022 17:26:09 +0200 Subject: [PATCH] Shorten up long (over 40 characters) commands in the logs Very long commands clutter the logs too, so shortening them up should also be helpful: - action: run chroot: false command: | echo test1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 echo test2 echo test3 It will print this: 2022/04/15 15:24:49 echo test1 2 3 4 5 6 7 8 9 10 11 12 13 1... | test1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2022/04/15 15:24:49 echo test1 2 3 4 5 6 7 8 9 10 11 12 13 1... | test2 2022/04/15 15:24:49 echo test1 2 3 4 5 6 7 8 9 10 11 12 13 1... | test3 On the other hand, a single-line command written as a multiline string is still a single-line command: - action: run chroot: false command: | echo test1 Should print: 2022/04/15 15:29:27 echo test1 | test1 Signed-off-by: Andrej Shadura --- actions/run_action.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/actions/run_action.go b/actions/run_action.go index 630b5ad..7520a97 100644 --- a/actions/run_action.go +++ b/actions/run_action.go @@ -51,6 +51,10 @@ import ( "github.com/go-debos/debos" ) +const ( + maxLabelLength = 40 +) + type RunAction struct { debos.BaseAction `yaml:",inline"` Chroot bool @@ -113,11 +117,21 @@ func (run *RunAction) doRun(context debos.DebosContext) error { label = path.Base(run.Script) } else { cmdline = []string{run.Command} - commands := strings.Split(run.Command, "\n") + + // Remove leading and trailing spaces and — importantly — newlines + // before splitting, so that single-line scripts split into an array + // of a single string only. + commands := strings.Split(strings.TrimSpace(run.Command), "\n") label = commands[0] - // Make it clear a multi-line command is being run - if len(commands) > 1 { + // Make it clear a long or a multi-line command is being run + if len(label) > maxLabelLength { + label = label[:maxLabelLength] + + label = strings.TrimSpace(label) + + label += "..." + } else if len(commands) > 1 { label += "..." } }