Skip to content

Commit

Permalink
Merge pull request #19 from krzko/feat-add-stderr-as-info
Browse files Browse the repository at this point in the history
feat: add stderr as info
  • Loading branch information
krzko authored May 22, 2024
2 parents 1aae857 + 658d9ee commit c2636c3
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ This action is intended to be used in conjunction with the [OpenTelemetry Collec
| `otel-resource-attributes` | Key-value pairs as resource attributes, formatted as comma-separated values: `key1=value1,key2=value2`. | Optional |
| `otel-service-name` | The logical name of the service which sets the value of the service.name resource attribute. | Required |
| `run` | The command to be executed. | Required |
| `shell` | Override the default shell settings in the runner's operating system. Supported options are `bash`, `pwsh`, `python`, `sh`, `cmd`, `pwsh`, and `powershell`. | Optional |
| `shell` | Override the default shell settings in the runner's operating system. Supported options are `bash`, `pwsh`, `python`, `sh`, `cmd`, `pwsh`, and `powershell`. Default: `bash`. | Optional |
| `stderr-as-info` | If set to `true`, the standard error output will be treated as standard output. Default: `false`. | Optional |
| `step-name` | The name of the step. | Required |

### Outputs
Expand Down
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: '3'

vars:
BUILD_VERSION:
sh: echo "0.4.5"
sh: echo "0.5.0"
BUILD_DATE:
sh: date "+%F %T"
COMMIT_ID:
Expand Down
6 changes: 6 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ inputs:
You can override the default shell settings in the runner's operating
system using the shell keyword. Supported options are bash, pwsh,
python, sh, cmd, pwsh and powershell. The default is bash.
stderr-as-info:
required: false
default: false
description: >
If set to 'true', logs stderr as informational messages instead of errors.
Useful for commands that use stderr for standard output.
step-name:
required: true
description: >
Expand Down
Binary file modified bin/run-with-telemetry-darwin-amd64
Binary file not shown.
Binary file modified bin/run-with-telemetry-darwin-arm64
Binary file not shown.
Binary file modified bin/run-with-telemetry-linux-amd64
Binary file not shown.
Binary file modified bin/run-with-telemetry-linux-armv8
Binary file not shown.
Binary file modified bin/run-with-telemetry-windows-amd64.exe
Binary file not shown.
16 changes: 13 additions & 3 deletions cmd/run-with-telemetry/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type InputParams struct {
StepName string
JobName string
JobAsParent string
StderrAsInfo string
}

type TextMapCarrier map[string]string
Expand Down Expand Up @@ -102,7 +103,7 @@ func emitStepSummary(params InputParams, traceID trace.TraceID, spanID trace.Spa
githubactions.AddStepSummary(markdownSummary)
}

func executeCommand(shell string, command string, span trace.Span, headers map[string]string) (string, int, string, string, error) {
func executeCommand(shell string, command string, span trace.Span, headers map[string]string, stderrAsInfo bool) (string, int, string, string, error) {
var cmd *exec.Cmd
var args []string

Expand Down Expand Up @@ -165,12 +166,19 @@ func executeCommand(shell string, command string, span trace.Span, headers map[s
}
}()

if stderrAsInfo {
githubactions.Infof("Redirecting stderr to info: %t", stderrAsInfo)
}
go func() {
defer wg.Done()
scanner := bufio.NewScanner(stderrPipe)
for scanner.Scan() {
line := scanner.Text()
githubactions.Errorf("%s", line)
if stderrAsInfo {
githubactions.Infof("%s", line)
} else {
githubactions.Errorf("%s", line)
}
stderrBuf.WriteString(line + "\n")
}
}()
Expand Down Expand Up @@ -393,6 +401,7 @@ func parseInputParams() InputParams {
StepName: githubactions.GetInput("step-name"),
JobName: githubactions.GetInput("job-name"),
JobAsParent: githubactions.GetInput("job-as-parent"),
StderrAsInfo: githubactions.GetInput("stderr-as-info"),
}
}

Expand Down Expand Up @@ -539,7 +548,8 @@ func main() {
shell := githubactions.GetInput("shell")
githubactions.Infof("Executing command: %s with shell: %s", params.Run, shell)

usedShell, pid, stdout, stderr, err := executeCommand(shell, params.Run, span, params.OtelExporterOtlpHeaders)
stderrAsInfo := strings.ToLower(params.StderrAsInfo) == "true"
usedShell, pid, stdout, stderr, err := executeCommand(shell, params.Run, span, params.OtelExporterOtlpHeaders, stderrAsInfo)

if err != nil {
githubactions.Errorf("Failed to execute command: %v", err)
Expand Down

0 comments on commit c2636c3

Please sign in to comment.