Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch human logging to stderr to enable pipeable validation output #416

Merged
merged 7 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions action/pipeline/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ func (c *Config) ValidateLocal(client compiler.Engine) error {
return err
}

// output the message in stdout format
// output the message in stderr format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Stdout
err = output.Stdout(fmt.Sprintf("%s is valid", path))
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Stderr
err = output.Stderr(fmt.Sprintf("%s is valid", path))
if err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions internal/output/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ const (
// when outputting in stdout format.
DriverStdout = "stdout"

// DriverStderr defines the driver type
// when outputting in stderr format.
DriverStderr = "stderr"

// DriverDump defines the driver type
// when outputting in dump format.
DriverDump = "dump"
Expand Down
30 changes: 30 additions & 0 deletions internal/output/stderr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2023 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package output

import (
"fmt"
"os"

"github.com/sirupsen/logrus"
)

// Stderr outputs the provided input to stderr.
func Stderr(_input interface{}) error {
logrus.Debugf("creating output with %s driver", DriverStderr)

// validate the input provided
err := validate(DriverStderr, _input)
if err != nil {
return err
}

logrus.Tracef("sending output to stderr with %s driver", DriverStderr)

// ensure we output to stderr
fmt.Fprintln(os.Stderr, _input)

return nil
}
63 changes: 63 additions & 0 deletions internal/output/stderr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) 2023 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package output

import (
"testing"
)

func TestErrout_Default(t *testing.T) {
// setup tests
tests := []struct {
failure bool
input interface{}
}{
{
failure: false,
input: "hello",
},
{ // map
failure: false,
input: map[string]string{"hello": "world"},
},
{ // slice
failure: false,
input: []interface{}{1, 2, 3},
},
{ // slice complex
failure: false,
input: []interface{}{struct{ Foo string }{Foo: "bar"}},
},
{ // complex
failure: false,
input: []struct{ Foo string }{{"bar"}, {"baz"}},
},
{
failure: true,
input: nil,
},
{
failure: true,
input: "",
},
}

// run tests
for _, test := range tests {
err := Stderr(test.input)

if test.failure {
if err == nil {
t.Errorf("Stderr should have returned err")
}

continue
}

if err != nil {
t.Errorf("Stderr returned err: %v", err)
}
}
}