Skip to content

Commit

Permalink
Shorten up long (over 40 characters) commands in the logs
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
andrewshadura authored and sjoerdsimons committed Apr 19, 2022
1 parent 87af7aa commit aac4596
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions actions/run_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ import (
"github.com/go-debos/debos"
)

const (
maxLabelLength = 40
)

type RunAction struct {
debos.BaseAction `yaml:",inline"`
Chroot bool
Expand Down Expand Up @@ -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 += "..."
}
}
Expand Down

0 comments on commit aac4596

Please sign in to comment.