1
0
mirror of https://github.com/helm/chart-testing.git synced 2026-02-05 09:45:14 +01:00
Files
chart-testing/pkg/tool/cmdexecutor.go
Torsten Walter e4d7b78183 Support execution of additional commands during ct lint (#283)
* support execution of additional commands for lint

Given that helm unittest is installed locally or mounted into the chart
testing container one could use this to run helm unittest for each
chart.

```
additional-commands:
 - helm unittest --helm3 -f tests/*.yaml {{ .Path }}
```

The command is treated as a go template. Like this it's possible to get
the path to the chart as in the example. It would also be open for
further extension if needed.

Signed-off-by: Torsten Walter <mail@torstenwalter.de>

* Use sh to execute command

Signed-off-by: Torsten Walter <mail@torstenwalter.de>

Co-authored-by: Reinhard Nägele <unguiculus@gmail.com>

* rename function to NewCmdTemplateExecutor

Signed-off-by: Torsten Walter <mail@torstenwalter.de>

* add error handling

Signed-off-by: Torsten Walter <mail@torstenwalter.de>

* add documentation

Signed-off-by: Torsten Walter <mail@torstenwalter.de>

* use go-shellwords to split rendered command

Signed-off-by: Torsten Walter <mail@torstenwalter.de>

* Update pkg/tool/cmdexecutor.go

Signed-off-by: Torsten Walter <mail@torstenwalter.de>

Co-authored-by: Reinhard Nägele <unguiculus@gmail.com>

* add unit tests

Signed-off-by: Torsten Walter <mail@torstenwalter.de>

Co-authored-by: Reinhard Nägele <unguiculus@gmail.com>
2020-10-20 17:10:56 -04:00

39 lines
793 B
Go

package tool
import (
"strings"
"text/template"
"github.com/mattn/go-shellwords"
)
type ProcessExecutor interface {
RunProcess(executable string, execArgs ...interface{}) error
}
type CmdTemplateExecutor struct {
exec ProcessExecutor
}
func NewCmdTemplateExecutor(exec ProcessExecutor) CmdTemplateExecutor {
return CmdTemplateExecutor{
exec: exec,
}
}
func (t CmdTemplateExecutor) RunCommand(cmdTemplate string, data interface{}) error {
var template = template.Must(template.New("command").Parse(cmdTemplate))
var b strings.Builder
if err := template.Execute(&b, data); err != nil {
return err
}
rendered := b.String()
words, err := shellwords.Parse(rendered)
if err != nil {
return err
}
name, args := words[0], words[1:]
return t.exec.RunProcess(name, args)
}