Skip to content

Commit

Permalink
cleanup duplicate definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
marshall007 committed Jun 18, 2024
1 parent 0d908e4 commit d8f1393
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 62 deletions.
5 changes: 2 additions & 3 deletions src/pkg/runner/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (

"github.com/defenseunicorns/maru-runner/src/config"
"github.com/defenseunicorns/maru-runner/src/message"
"github.com/defenseunicorns/maru-runner/src/pkg/tasks"
"github.com/defenseunicorns/maru-runner/src/pkg/utils"
"github.com/defenseunicorns/maru-runner/src/pkg/variables"
"github.com/defenseunicorns/maru-runner/src/types"
Expand Down Expand Up @@ -69,7 +68,7 @@ func (r *Runner) performAction(action types.Action) error {
}

// processAction checks if action needs to be processed for a given task
func (r *Runner) processAction(task *tasks.Task, action types.Action) bool {
func (r *Runner) processAction(task *types.Task, action types.Action) bool {

taskReferenceName := strings.Split(task.Name, ":")[0]
actionReferenceName := strings.Split(action.TaskReference, ":")[0]
Expand Down Expand Up @@ -369,7 +368,7 @@ func convertWaitToCmd(wait types.ActionWait, timeout int) (string, error) {
}

// validateActionableTaskCall validates a tasks "withs" and inputs
func validateActionableTaskCall(inputTaskName string, inputs map[string]tasks.InputParameter, withs map[string]string) error {
func validateActionableTaskCall(inputTaskName string, inputs map[string]types.InputParameter, withs map[string]string) error {
missing := []string{}
for inputKey, input := range inputs {
// skip inputs that are not required or have a default value
Expand Down
21 changes: 11 additions & 10 deletions src/pkg/runner/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/defenseunicorns/maru-runner/src/config"
"github.com/defenseunicorns/maru-runner/src/types"

"github.com/defenseunicorns/maru-runner/src/pkg/tasks"
"github.com/defenseunicorns/maru-runner/src/pkg/variables"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -216,7 +217,7 @@ func Test_validateActionableTaskCall(t *testing.T) {

func TestRunner_performAction(t *testing.T) {
type fields struct {
TasksFile types.TasksFile
TasksFile *tasks.TasksFile
TaskNameMap map[string]bool
envFilePath string
variableConfig *variables.VariableConfig[variables.ExtraVariableInfo]
Expand All @@ -234,7 +235,7 @@ func TestRunner_performAction(t *testing.T) {
{
name: "failed action processing due to invalid command",
fields: fields{
TasksFile: types.TasksFile{},
TasksFile: &tasks.TasksFile{},
TaskNameMap: make(map[string]bool),
envFilePath: "",
variableConfig: GetMaruVariableConfig(),
Expand All @@ -255,7 +256,7 @@ func TestRunner_performAction(t *testing.T) {
{
name: "Unable to open path",
fields: fields{
TasksFile: types.TasksFile{},
TasksFile: &tasks.TasksFile{},
TaskNameMap: make(map[string]bool),
envFilePath: "test/path",
variableConfig: GetMaruVariableConfig(),
Expand Down Expand Up @@ -299,7 +300,7 @@ func TestRunner_performAction(t *testing.T) {

func TestRunner_processAction(t *testing.T) {
type fields struct {
TasksFile types.TasksFile
TasksFile *tasks.TasksFile
TaskNameMap map[string]bool
envFilePath string
variableConfig *variables.VariableConfig[variables.ExtraVariableInfo]
Expand All @@ -317,7 +318,7 @@ func TestRunner_processAction(t *testing.T) {
{
name: "successful action processing",
fields: fields{
TasksFile: types.TasksFile{},
TasksFile: &tasks.TasksFile{},
TaskNameMap: map[string]bool{},
envFilePath: "",
variableConfig: GetMaruVariableConfig(),
Expand All @@ -335,7 +336,7 @@ func TestRunner_processAction(t *testing.T) {
{
name: "action processing with same task and action reference",
fields: fields{
TasksFile: types.TasksFile{},
TasksFile: &tasks.TasksFile{},
TaskNameMap: map[string]bool{},
envFilePath: "",
variableConfig: GetMaruVariableConfig(),
Expand All @@ -353,7 +354,7 @@ func TestRunner_processAction(t *testing.T) {
{
name: "action processing with empty task reference",
fields: fields{
TasksFile: types.TasksFile{},
TasksFile: &tasks.TasksFile{},
TaskNameMap: map[string]bool{},
envFilePath: "",
variableConfig: GetMaruVariableConfig(),
Expand All @@ -371,7 +372,7 @@ func TestRunner_processAction(t *testing.T) {
{
name: "action processing with non-empty task reference and different task and action reference names",
fields: fields{
TasksFile: types.TasksFile{},
TasksFile: &tasks.TasksFile{},
TaskNameMap: map[string]bool{},
envFilePath: "",
variableConfig: GetMaruVariableConfig(),
Expand All @@ -389,8 +390,8 @@ func TestRunner_processAction(t *testing.T) {
{
name: "action processing with task reference already processed",
fields: fields{
TasksFile: types.TasksFile{
Tasks: []types.Task{
TasksFile: &tasks.TasksFile{
Tasks: []*types.Task{
{
Name: "testTaskRef:subTask",
},
Expand Down
6 changes: 3 additions & 3 deletions src/pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func loadTasksFileFromPath(fullPath string) (*tasks.TasksFile, error) {
return &tasksFile, nil
}

func (r *Runner) getTask(taskName string) (*tasks.Task, error) {
func (r *Runner) getTask(taskName string) (*types.Task, error) {
for _, task := range r.TasksFile.Tasks {
if task.Name == taskName {
return task, nil
Expand All @@ -268,7 +268,7 @@ func (r *Runner) getTask(taskName string) (*tasks.Task, error) {
return nil, fmt.Errorf("task name %s not found", taskName)
}

func (r *Runner) executeTask(task *tasks.Task) error {
func (r *Runner) executeTask(task *types.Task) error {
defaultEnv := []string{}
for name, inputParam := range task.Inputs {
d := inputParam.Default
Expand All @@ -292,7 +292,7 @@ func (r *Runner) executeTask(task *tasks.Task) error {
return nil
}

func (r *Runner) checkForTaskLoops(task *tasks.Task, tasksFile *tasks.TasksFile, setVariables map[string]string) error {
func (r *Runner) checkForTaskLoops(task *types.Task, tasksFile *tasks.TasksFile, setVariables map[string]string) error {
// Filtering unique task actions allows for rerunning tasks in the same execution
uniqueTaskActions := getUniqueTaskActions(task.Actions)
for _, action := range uniqueTaskActions {
Expand Down
25 changes: 11 additions & 14 deletions src/pkg/tasks/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ type TaskRunner struct {
ctx context.Context
workDir string
taskFiles map[string]*TasksFile
taskMap map[string]*Task
taskMap map[string]*types.Task
}

type TaskRun struct {

Check warning on line 28 in src/pkg/tasks/runner.go

View workflow job for this annotation

GitHub Actions / validate

exported type TaskRun should have comment or be unexported
ctx context.Context
task *Task
task *types.Task
steps []*types.Step
inputs map[string]string
stepOutputs map[string]interface{}
Expand All @@ -37,7 +37,7 @@ func NewRunner() *TaskRunner {
runner := &TaskRunner{
ctx: context.Background(),
taskFiles: make(map[string]*TasksFile),
taskMap: make(map[string]*Task),
taskMap: make(map[string]*types.Task),
}

return runner
Expand Down Expand Up @@ -194,7 +194,7 @@ func (r *TaskRunner) Run(run *TaskRun) error {
return nil
}

func NewRun(task *Task, ctx context.Context) *TaskRun {
func NewRun(task *types.Task, ctx context.Context) *TaskRun {
run := &TaskRun{
ctx: ctx,
task: task,
Expand Down Expand Up @@ -284,12 +284,16 @@ func (tr *TaskRun) eval(expression string) (object.Object, error) {
}

func (tr *TaskRun) exec(shell string, args []string) (object.Object, error) {
return risor.Eval(
result, err := risor.Eval(
tr.ctx,
"exec(shell, args).stdout",
risor.WithGlobal("shell", shell),
risor.WithGlobal("args", args),
)

fmt.Println(result)

return result, err
}

func (r *TaskRunner) getContext() (context.Context, error) {
Expand Down Expand Up @@ -319,6 +323,8 @@ func (r *TaskRunner) getContext() (context.Context, error) {

func fromRisor(value object.Object) (interface{}, error) {
switch obj := value.(type) {
case *object.NilType:
case *object.Bool:
case *object.Int:
case *object.Float:
case *object.String:
Expand All @@ -341,15 +347,6 @@ func fromRisor(value object.Object) (interface{}, error) {

json.Unmarshal([]byte(str), &out)
return out, nil
// json, err := obj.MarshalJSON()

// if err != nil {
// return "", fmt.Errorf("could not serialize output")
// }

// return string(json), nil
case *object.NilType:
return nil, nil
}

return "", fmt.Errorf("unsupported output type: %T", value)
Expand Down
20 changes: 1 addition & 19 deletions src/pkg/tasks/taskfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,9 @@ type TasksFile struct {
Includes []map[string]string `json:"includes,omitempty" jsonschema:"description=List of local task files to include"`
Env map[string]string `json:"env,omitempty" jsonschema:"description=Environment variables to set for all tasks"`
Variables []variables.InteractiveVariable[variables.ExtraVariableInfo] `json:"variables,omitempty" jsonschema:"description=Definitions and default values for variables used in run.yaml"`
Tasks []*Task `json:"tasks" jsonschema:"description=The list of tasks that can be run"`
Tasks []*types.Task `json:"tasks" jsonschema:"description=The list of tasks that can be run"`

src string
dirPath string
filePath string
}

// Task represents a single task
type Task struct {
Name string `json:"name" jsonschema:"description=Name of the task"`
Description string `json:"description,omitempty" jsonschema:"description=Description of the task"`
Actions []types.Action `json:"actions,omitempty" jsonschema:"description=Actions to take when running the task"`
Steps []types.Step `json:"steps,omitempty" jsonschema:"description=Actions to take when running the task"`
Inputs map[string]InputParameter `json:"inputs,omitempty" jsonschema:"description=Input parameters for the task"`
Outputs map[string]string `json:"outputs,omitempty" jsonschema:"description=Outputs from the task"`
}

// InputParameter represents a single input parameter for a task, to be used w/ `with`
type InputParameter struct {
Description string `json:"description" jsonschema:"description=Description of the parameter,required"`
DeprecatedMessage string `json:"deprecatedMessage,omitempty" jsonschema:"description=Message to display when the parameter is deprecated"`
Required bool `json:"required,omitempty" jsonschema:"description=Whether the parameter is required,default=true"`
Default string `json:"default,omitempty" jsonschema:"description=Default value for the parameter"`
}
13 changes: 0 additions & 13 deletions src/types/steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,9 @@
package types

import (
"github.com/defenseunicorns/maru-runner/src/pkg/variables"
"github.com/defenseunicorns/pkg/exec"
)

// TasksFile represents the contents of a tasks file
type TasksFile struct {
Includes []map[string]string `json:"includes,omitempty" jsonschema:"description=List of local task files to include"`
Env map[string]string `json:"env,omitempty" jsonschema:"description=Environment variables to set for all tasks"`
Variables []variables.InteractiveVariable[variables.ExtraVariableInfo] `json:"variables,omitempty" jsonschema:"description=Definitions and default values for variables used in run.yaml"`
Tasks []*Task `json:"tasks" jsonschema:"description=The list of tasks that can be run"`

dirPath string
filePath string
taskMap map[string]*Task
}

// Task represents a single task
type Task struct {
Name string `json:"name" jsonschema:"description=Name of the task"`
Expand Down

0 comments on commit d8f1393

Please sign in to comment.