From 188d8bbb901c3f3aaa63dcf8d1b56f819899f211 Mon Sep 17 00:00:00 2001 From: Marshall Cottrell Date: Tue, 18 Jun 2024 10:54:32 -0400 Subject: [PATCH] cleanup duplicate definitions --- src/pkg/runner/actions.go | 5 ++--- src/pkg/runner/actions_test.go | 21 +++++++++++---------- src/pkg/runner/runner.go | 6 +++--- src/pkg/tasks/runner.go | 25 +++++++++++-------------- src/pkg/tasks/taskfile.go | 20 +------------------- src/types/{steps.go => tasks.go} | 13 ------------- 6 files changed, 28 insertions(+), 62 deletions(-) rename src/types/{steps.go => tasks.go} (79%) diff --git a/src/pkg/runner/actions.go b/src/pkg/runner/actions.go index 026861d..e8f2c02 100644 --- a/src/pkg/runner/actions.go +++ b/src/pkg/runner/actions.go @@ -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" @@ -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] @@ -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 diff --git a/src/pkg/runner/actions_test.go b/src/pkg/runner/actions_test.go index 9b9345c..6717c65 100644 --- a/src/pkg/runner/actions_test.go +++ b/src/pkg/runner/actions_test.go @@ -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" @@ -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] @@ -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(), @@ -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(), @@ -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] @@ -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(), @@ -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(), @@ -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(), @@ -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(), @@ -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", }, diff --git a/src/pkg/runner/runner.go b/src/pkg/runner/runner.go index 68e358f..ae6f75e 100644 --- a/src/pkg/runner/runner.go +++ b/src/pkg/runner/runner.go @@ -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 @@ -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 @@ -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 { diff --git a/src/pkg/tasks/runner.go b/src/pkg/tasks/runner.go index 290db20..5e0a021 100644 --- a/src/pkg/tasks/runner.go +++ b/src/pkg/tasks/runner.go @@ -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 { ctx context.Context - task *Task + task *types.Task steps []*types.Step inputs map[string]string stepOutputs map[string]interface{} @@ -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 @@ -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, @@ -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) { @@ -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: @@ -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) diff --git a/src/pkg/tasks/taskfile.go b/src/pkg/tasks/taskfile.go index 4cdd69f..1729691 100644 --- a/src/pkg/tasks/taskfile.go +++ b/src/pkg/tasks/taskfile.go @@ -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"` -} diff --git a/src/types/steps.go b/src/types/tasks.go similarity index 79% rename from src/types/steps.go rename to src/types/tasks.go index d30fe2e..de796c6 100644 --- a/src/types/steps.go +++ b/src/types/tasks.go @@ -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"`