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 <andrew.shadura@collabora.co.uk>
This commit is contained in:
Andrej Shadura
2022-04-15 17:26:09 +02:00
committed by Sjoerd Simons
parent 87af7aa575
commit aac45966c9

View File

@@ -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 += "..."
}
}