add option to continue pipeline when the input is empty

This commit is contained in:
Donny Xia
2020-08-19 11:21:19 -07:00
parent 0ff4e53046
commit 037ac3b134
3 changed files with 40 additions and 2 deletions

View File

@@ -77,6 +77,6 @@ func (r *SourceRunner) runE(c *cobra.Command, args []string) error {
inputs = []kio.Reader{&kio.ByteReader{Reader: c.InOrStdin()}}
}
err := kio.Pipeline{Inputs: inputs, Outputs: outputs}.Execute()
err := kio.Pipeline{Inputs: inputs, Outputs: outputs, ContinueIfInputEmpty: true}.Execute()
return handleError(c, err)
}

View File

@@ -75,6 +75,11 @@ type Pipeline struct {
// Outputs are where the transformed Resource Configuration is written.
Outputs []Writer `yaml:"outputs,omitempty"`
// ContinueIfInputEmpty indicates should pipeline continue when the
// the input result is empty. This is useful when we want to run outputs
// even the input is empty.
ContinueIfInputEmpty bool
}
// Execute executes each step in the sequence, returning immediately after encountering
@@ -99,7 +104,7 @@ func (p Pipeline) ExecuteWithCallback(callback PipelineExecuteCallbackFunc) erro
}
result = append(result, nodes...)
}
if len(result) == 0 {
if len(result) == 0 && !p.ContinueIfInputEmpty {
// no inputs to operate on
return nil
}

View File

@@ -4,7 +4,9 @@
package kio_test
import (
"bytes"
"reflect"
"strings"
"testing"
"github.com/stretchr/testify/assert"
@@ -75,3 +77,34 @@ func TestPipelineWithCallback(t *testing.T) {
)
}
}
func TestContinueIfInputEmpty(t *testing.T) {
actual := &bytes.Buffer{}
output := ByteWriter{
Sort: true,
WrappingKind: ResourceListKind,
WrappingAPIVersion: ResourceListAPIVersion,
}
output.Writer = actual
p := Pipeline{
Outputs: []Writer{output},
ContinueIfInputEmpty: true,
}
err := p.Execute()
if err != nil {
t.Fatal(err)
}
expected := `
apiVersion: config.kubernetes.io/v1alpha1
kind: ResourceList
items: []
`
if !assert.Equal(t,
strings.TrimSpace(expected), strings.TrimSpace(actual.String())) {
t.FailNow()
}
}