Skip to content

Commit

Permalink
Pipeline: add exec operation
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Kosegi <richard.kosegi@gmail.com>
  • Loading branch information
rkosegi committed Aug 3, 2024
1 parent b23f7b2 commit c921de9
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 0 deletions.
56 changes: 56 additions & 0 deletions pipeline/exec_op.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright 2024 Richard Kosegi
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package pipeline

import (
"errors"
"fmt"
osx "os/exec"
"slices"
)

func (e *ExecOp) String() string {
return fmt.Sprintf("Exec[Program=%s,Args=%d]", e.Program, safeStrListSize(e.Args))
}

func (e *ExecOp) Do(_ ActionContext) error {
if e.ValidExitCodes == nil {
e.ValidExitCodes = &[]int{}
}
if e.Args == nil {
e.Args = &[]string{}
}
cmd := osx.Command(e.Program, *e.Args...)
err := cmd.Run()
var exitErr *osx.ExitError
if errors.As(err, &exitErr) {
if !slices.Contains(*e.ValidExitCodes, exitErr.ExitCode()) {
return err
}
} else {
return err
}
return nil
}

func (e *ExecOp) CloneWith(ctx ActionContext) Action {
ss := ctx.Snapshot()
return &ExecOp{
Program: ctx.TemplateEngine().RenderLenient(e.Program, ss),
Args: safeRenderStrSlice(e.Args, ctx.TemplateEngine(), ss),
}
}
58 changes: 58 additions & 0 deletions pipeline/exec_op_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Copyright 2024 Richard Kosegi
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package pipeline

import (
"github.com/rkosegi/yaml-toolkit/dom"
"github.com/stretchr/testify/assert"
"testing"
)

func TestExecOpDoEmptyCommand(t *testing.T) {
eo := &ExecOp{}
assert.Error(t, eo.Do(mockEmptyActCtx()))
}

func TestExecOpDo(t *testing.T) {
var (
eo *ExecOp
)
eo = &ExecOp{
Program: "sh",
Args: &[]string{"-c", "exit 3"},
ValidExitCodes: &[]int{3},
}
assert.NoError(t, eo.Do(mockEmptyActCtx()))
eo = &ExecOp{
Program: "sh",
Args: &[]string{"-c", "exit 4"},
ValidExitCodes: &[]int{3},
}
assert.Contains(t, eo.String(), "sh")
assert.Contains(t, eo.String(), "=2")
assert.Error(t, eo.Do(mockEmptyActCtx()))
}

func TestExecOpCloneWith(t *testing.T) {
eo := &ExecOp{
Program: "{{ .Shell }}",
}
d := b.Container()
d.AddValue("Shell", dom.LeafNode("/bin/bash"))
eo = eo.CloneWith(mockActCtx(d)).(*ExecOp)
assert.Equal(t, "/bin/bash", eo.Program)
}
4 changes: 4 additions & 0 deletions pipeline/foreach_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func TestForeachStringItem(t *testing.T) {
File: "/tmp/a-{{ .forEach }}.yaml",
Format: OutputFormatYaml,
},
Exec: &ExecOp{
Program: "sh",
Args: &[]string{"-c", "rm /tmp/a-{{ .forEach }}.yaml"},
},
},
}
d := b.Container()
Expand Down
9 changes: 9 additions & 0 deletions pipeline/opspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ func (as OpSpec) toList() []Action {
if as.Env != nil {
actions = append(actions, as.Env)
}
if as.Exec != nil {
actions = append(actions, as.Exec)
}
if as.ForEach != nil {
actions = append(actions, as.ForEach)
}
Expand Down Expand Up @@ -83,6 +86,9 @@ func (as OpSpec) CloneWith(ctx ActionContext) Action {
if as.Env != nil {
r.Env = as.Env.CloneWith(ctx).(*EnvOp)
}
if as.Exec != nil {
r.Exec = as.Exec.CloneWith(ctx).(*ExecOp)
}
if as.Abort != nil {
r.Abort = as.Abort.CloneWith(ctx).(*AbortOp)
}
Expand All @@ -102,6 +108,9 @@ func (as OpSpec) String() string {
if as.Export != nil {
parts = append(parts, fmt.Sprintf("Export=%v", as.Export.String()))
}
if as.Exec != nil {
parts = append(parts, fmt.Sprintf("Exec=%v", as.Exec.String()))
}
if as.ForEach != nil {
parts = append(parts, fmt.Sprintf("ForEach=%v", as.ForEach.String()))
}
Expand Down
5 changes: 5 additions & 0 deletions pipeline/opspec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ func TestOpSpecCloneWith(t *testing.T) {
Env: &EnvOp{
Path: "{{ .Path }}",
},
Exec: &ExecOp{
Program: "{{ .Shell }}",
},
Export: &ExportOp{
File: "/tmp/file.yaml",
Path: "{{ .Path }}",
Expand All @@ -59,6 +62,7 @@ func TestOpSpecCloneWith(t *testing.T) {
a := o.CloneWith(mockActCtx(b.FromMap(map[string]interface{}{
"Path": "root.sub2",
"Path3": "/root/sub3",
"Shell": "/bin/bash",
}))).(OpSpec)
t.Log(a.String())
assert.Equal(t, "root.sub2", a.Set.Path)
Expand All @@ -67,4 +71,5 @@ func TestOpSpecCloneWith(t *testing.T) {
assert.Equal(t, "root.sub2", a.Template.Path)
assert.Equal(t, "root.sub2", a.Export.Path)
assert.Equal(t, "root.sub2", a.Env.Path)
assert.Equal(t, "/bin/bash", a.Exec.Program)
}
12 changes: 12 additions & 0 deletions pipeline/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ type OpSpec struct {
// Env adds OS environment variables into data document
Env *EnvOp `yaml:"env,omitempty"`

// Exec executes program
Exec *ExecOp `yaml:"exec,omitempty"`

// Export exports data document into file
Export *ExportOp `yaml:"export,omitempty"`
// ForEach execute same operation in a loop for every configured item
Expand Down Expand Up @@ -171,6 +174,15 @@ type EnvOp struct {
envGetter func() []string
}

type ExecOp struct {
// Program to execute
Program string `yaml:"program,omitempty"`
// Optional arguments for program
Args *[]string `yaml:"args,omitempty"`
// List of exit codes that are assumed to be valid
ValidExitCodes *[]int `yaml:"validExitCodes,omitempty"`
}

type OutputFormat string

const (
Expand Down
11 changes: 11 additions & 0 deletions pipeline/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,14 @@ func sortActionNames(actions ChildActions) []string {
func actionNames(actions ChildActions) string {
return strings.Join(sortActionNames(actions), ",")
}

func safeRenderStrSlice(args *[]string, te TemplateEngine, data map[string]interface{}) *[]string {
if args == nil {
return nil
}
r := make([]string, len(*args))
for i, arg := range *args {
r[i] = te.RenderLenient(arg, data)
}
return &r
}

0 comments on commit c921de9

Please sign in to comment.