Skip to content

Commit

Permalink
feat(pkg/task/lib/print): Add TaskPrint for simple prints to output
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejsika committed Mar 28, 2023
1 parent 94a844f commit ef6ccfd
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
53 changes: 53 additions & 0 deletions pkg/task/lib/print/print.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package print

import (
"fmt"
"os"
text_template "text/template"

"github.com/sikalabs/gobble/pkg/libtask"
"github.com/sikalabs/gobble/pkg/utils/exec_utils"
)

type TaskPrint struct {
Template string `yaml:"template"`
}

func Run(
taskInput libtask.TaskInput,
taskParams TaskPrint,
) libtask.TaskOutput {
tmpl, err := text_template.New("template").Parse(taskParams.Template)
if err != nil {
return libtask.TaskOutput{
Error: err,
}
}
tmpFile, err := os.CreateTemp("", "template")
if err != nil {
return libtask.TaskOutput{
Error: err,
}
}
err = tmpl.Execute(tmpFile, map[string]interface{}{
"Config": taskInput.Config,
"Vars": taskInput.Vars,
})
if err != nil {
return libtask.TaskOutput{
Error: err,
}
}
fmt.Println("OUTPUT:")
err = exec_utils.RawExecStdout("cat", tmpFile.Name())
if err != nil {
return libtask.TaskOutput{
Error: err,
}
}
defer os.Remove(tmpFile.Name())

return libtask.TaskOutput{
Error: err,
}
}
4 changes: 4 additions & 0 deletions pkg/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/sikalabs/gobble/pkg/task/lib/chmod"
"github.com/sikalabs/gobble/pkg/task/lib/command"
"github.com/sikalabs/gobble/pkg/task/lib/cp"
"github.com/sikalabs/gobble/pkg/task/lib/print"
"github.com/sikalabs/gobble/pkg/task/lib/template"
)

Expand All @@ -18,6 +19,7 @@ type Task struct {
Template template.TaskTemplate `yaml:"template"`
Command command.TaskCommand `yaml:"command"`
Chmod chmod.TaskChmod `yaml:"chmod"`
Print print.TaskPrint `yaml:"print"`
}

func Run(
Expand All @@ -35,6 +37,8 @@ func Run(
return command.Run(taskInput, task.Command)
case task.Chmod.Path != "":
return chmod.Run(taskInput, task.Chmod)
case task.Print.Template != "":
return print.Run(taskInput, task.Print)
}
return libtask.TaskOutput{
Error: fmt.Errorf("task \"%s\" not found", task.Name),
Expand Down

0 comments on commit ef6ccfd

Please # to comment.