Skip to content

Commit

Permalink
fix: pass stdout parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
vsukhin committed May 29, 2024
1 parent 16c375c commit 5bd9944
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 24 deletions.
11 changes: 6 additions & 5 deletions cmd/kubectl-testkube/commands/tests/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tests
import (
"errors"
"fmt"
"io"
"os"
"path"
"regexp"
Expand Down Expand Up @@ -30,7 +31,7 @@ const (
maxArgSize = int64(131072) // maximum argument size in linux-based systems is 128 KiB
)

func printExecutionDetails(cmd *cobra.Command, execution testkube.Execution) error {
func printExecutionDetails(cmd *cobra.Command, w io.Writer, execution testkube.Execution) error {
outputFlag := cmd.Flag("output")
outputType := render.OutputPretty
if outputFlag != nil {
Expand Down Expand Up @@ -60,14 +61,14 @@ func printExecutionDetails(cmd *cobra.Command, execution testkube.Execution) err
ui.NL()
ui.NL()
case render.OutputYAML:
return render.RenderYaml(execution, os.Stdout)
return render.RenderYaml(execution, w)
case render.OutputJSON:
return render.RenderJSON(execution, os.Stdout)
return render.RenderJSON(execution, w)
case render.OutputGoTemplate:
tpl := cmd.Flag("go-template").Value.String()
return render.RenderGoTemplate(execution, os.Stdout, tpl)
return render.RenderGoTemplate(execution, w, tpl)
default:
return render.RenderYaml(execution, os.Stdout)
return render.RenderYaml(execution, w)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubectl-testkube/commands/tests/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ func NewRunTestCmd() *cobra.Command {

var execErrors []error
for _, execution := range executions {
err = printExecutionDetails(cmd, execution)
err = printExecutionDetails(cmd, os.Stdout, execution)
ui.ExitOnError("printing test execution "+execution.Id, err)

if execution.ExecutionResult != nil && execution.ExecutionResult.ErrorMessage != "" {
Expand Down
13 changes: 7 additions & 6 deletions cmd/kubectl-testkube/commands/testsuites/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"time"
Expand All @@ -18,7 +19,7 @@ import (
"github.com/kubeshop/testkube/pkg/ui"
)

func printExecution(cmd *cobra.Command, execution testkube.TestSuiteExecution, startTime time.Time) error {
func printExecution(cmd *cobra.Command, w io.Writer, execution testkube.TestSuiteExecution, startTime time.Time) error {
outputFlag := cmd.Flag("output")
outputType := render.OutputPretty
if outputFlag != nil {
Expand All @@ -42,20 +43,20 @@ func printExecution(cmd *cobra.Command, execution testkube.TestSuiteExecution, s

if execution.Id != "" {
ui.Warn("Duration:", execution.CalculateDuration().String()+"\n")
ui.Table(execution, os.Stdout)
ui.Table(execution, w)
}

ui.NL()
ui.NL()
case render.OutputYAML:
return render.RenderYaml(execution, os.Stdout)
return render.RenderYaml(execution, w)
case render.OutputJSON:
return render.RenderJSON(execution, os.Stdout)
return render.RenderJSON(execution, w)
case render.OutputGoTemplate:
tpl := cmd.Flag("go-template").Value.String()
return render.RenderGoTemplate(execution, os.Stdout, tpl)
return render.RenderGoTemplate(execution, w, tpl)
default:
return render.RenderYaml(execution, os.Stdout)
return render.RenderYaml(execution, w)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions cmd/kubectl-testkube/commands/testsuites/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func NewRunTestSuiteCmd() *cobra.Command {
ui.ExitOnError("watching test suite execution", resp.Error)
if !silentMode {
execution.TruncateErrorMessages(maxErrorMessageLength)
err = printExecution(cmd, execution, startTime)
err = printExecution(cmd, os.Stdout, execution, startTime)
ui.ExitOnError("printing test suite execution "+execution.Id, err)
}
}
Expand All @@ -178,7 +178,7 @@ func NewRunTestSuiteCmd() *cobra.Command {
execution.TruncateErrorMessages(maxErrorMessageLength)
ui.ExitOnError("getting recent execution data id:"+execution.Id, err)

err = printExecution(cmd, execution, startTime)
err = printExecution(cmd, os.Stdout, execution, startTime)
ui.ExitOnError("printing test suite execution "+execution.Id, err)

if outputPretty {
Expand Down
4 changes: 2 additions & 2 deletions cmd/kubectl-testkube/commands/testsuites/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ func NewWatchTestSuiteExecutionCmd() *cobra.Command {
watchResp := client.WatchTestSuiteExecution(executionID)
for resp := range watchResp {
ui.ExitOnError("watching test suite execution", resp.Error)
printExecution(cmd, resp.Execution, startTime)
printExecution(cmd, os.Stdout, resp.Execution, startTime)
}

execution, err := client.GetTestSuiteExecution(executionID)
ui.ExitOnError("getting test suite excecution", err)
printExecution(cmd, execution, startTime)
printExecution(cmd, os.Stdout, execution, startTime)
ui.ExitOnError("getting recent execution data id:"+execution.Id, err)

err = uiPrintExecutionStatus(client, execution)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package renderer

import (
"fmt"
"os"
"io"

"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand All @@ -13,7 +13,7 @@ import (
"github.com/kubeshop/testkube/pkg/ui"
)

func PrintTestWorkflowExecution(cmd *cobra.Command, execution testkube.TestWorkflowExecution) error {
func PrintTestWorkflowExecution(cmd *cobra.Command, w io.Writer, execution testkube.TestWorkflowExecution) error {
outputFlag := cmd.Flag("output")
outputType := render.OutputPretty
if outputFlag != nil {
Expand All @@ -22,16 +22,16 @@ func PrintTestWorkflowExecution(cmd *cobra.Command, execution testkube.TestWorkf

switch outputType {
case render.OutputPretty:
printPrettyOutput(ui.NewUI(ui.Verbose, os.Stdout), execution)
printPrettyOutput(ui.NewUI(ui.Verbose, w), execution)
case render.OutputYAML:
return render.RenderYaml(execution, os.Stdout)
return render.RenderYaml(execution, w)
case render.OutputJSON:
return render.RenderJSON(execution, os.Stdout)
return render.RenderJSON(execution, w)
case render.OutputGoTemplate:
tpl := cmd.Flag("go-template").Value.String()
return render.RenderGoTemplate(execution, os.Stdout, tpl)
return render.RenderGoTemplate(execution, w, tpl)
default:
return render.RenderYaml(execution, os.Stdout)
return render.RenderYaml(execution, w)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubectl-testkube/commands/testworkflows/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func NewRunTestWorkflowCmd() *cobra.Command {
Config: config,
})
ui.ExitOnError("execute test workflow "+name+" from namespace "+namespace, err)
err = renderer.PrintTestWorkflowExecution(cmd, execution)
err = renderer.PrintTestWorkflowExecution(cmd, os.Stdout, execution)
ui.ExitOnError("render test workflow execution", err)

var exitCode = 0
Expand Down

0 comments on commit 5bd9944

Please sign in to comment.