Skip to content

Commit

Permalink
feat: trim blank leading or trailing log lines (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshdk committed Apr 26, 2022
1 parent 4cfe514 commit 98ccf7e
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,9 @@ func mainCmd() error {
}
}

// Discard any blank leading or trailing log lines.
logs = trimBlankLogs(logs)

// Format a GitHub comment body.
comment, err := templateComment(templateContext{
BuildNumber: droneBuildNumberInt,
Expand Down Expand Up @@ -451,3 +454,30 @@ func templateComment(params templateContext) (string, error) {
err := commentTemplate.Execute(&buf, params)
return buf.String(), err
}

// trimBlankLogs discards any blank leading or trailing log lines. Blank lines
// surrounded by non-blank lines are kept intact.
func trimBlankLogs(logs []string) []string {
// Starting from the front, discard any lines that are completely blank.
// Break out when the first non-blank line is found.
for len(logs) > 0 {
if strings.TrimSpace(logs[0]) != "" {
break
}
// Discard the first item.
logs = logs[1:]
}

// Starting from the back, discard any lines that are completely blank.
// Break out when the first (actually the last) non-blank line is found.
for len(logs) > 0 {
if strings.TrimSpace(logs[len(logs)-1]) != "" {
break
}
// Discard the last item.
logs = logs[:len(logs)-1]
}

// Return whatever remains
return logs
}

0 comments on commit 98ccf7e

Please sign in to comment.