From d519aa4cd185641f309ec4d3b286fdc2c29b4ec2 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Fri, 2 Aug 2024 17:32:41 -0400 Subject: [PATCH] refactor: hoist config handling in root command prerun --- internal/commands/execute_workflow.go | 8 +------- internal/commands/validate.go | 2 +- main.go | 27 +++++++++++++++++++++++---- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/internal/commands/execute_workflow.go b/internal/commands/execute_workflow.go index f933dc7..ae15d6d 100644 --- a/internal/commands/execute_workflow.go +++ b/internal/commands/execute_workflow.go @@ -8,13 +8,7 @@ import ( workflow "runner/internal/workflow" ) -func ExecuteWorkflow(configurationPath string, workflowFile string) error { - configuration, err := NewConfigFromFile(configurationPath) - - if err != nil { - return err - } - +func ExecuteWorkflow(configuration Configuration, workflowFile string) error { driver, err := runner.NewDriver(configuration.Containers.Driver) if err != nil { diff --git a/internal/commands/validate.go b/internal/commands/validate.go index 1fe9c28..39a6409 100644 --- a/internal/commands/validate.go +++ b/internal/commands/validate.go @@ -6,7 +6,7 @@ import ( workflow "runner/internal/workflow" ) -func ValidateWorkflow(configurationPath string, workflowPath string) error { +func ValidateWorkflow(configuration Configuration, workflowPath string) error { workflow, err := workflow.FromYamlFile(workflowPath) if err != nil { diff --git a/main.go b/main.go index 11ebe6a..aa428cb 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "context" "github.com/spf13/cobra" "log" "os" @@ -9,6 +10,24 @@ import ( var cli = &cobra.Command{ Use: "runner", + PersistentPreRun: func(cmd *cobra.Command, args []string) { + configPath, err := cmd.Flags().GetString("config") + + ctx := cmd.Context() + + if err == nil { + configuration, err := commands.NewConfigFromFile(configPath) + + if err != nil { + log.Printf("Failed to parse configuration (%s)!", configPath) + os.Exit(1) + } + + ctx = context.WithValue(ctx, "config", configuration) + } + + cmd.SetContext(ctx) + }, } var execute = &cobra.Command{ @@ -16,9 +35,9 @@ var execute = &cobra.Command{ Short: "Executes the provided workflow.", Args: cobra.MinimumNArgs(1), Run: func(cmd *cobra.Command, args []string) { - configPath, _ := cmd.Flags().GetString("config") + config := cmd.Context().Value("config").(*commands.Configuration) - if err := commands.ExecuteWorkflow(configPath, args[0]); err != nil { + if err := commands.ExecuteWorkflow(*config, args[0]); err != nil { log.Printf("Failure: %s", err) os.Exit(1) } @@ -30,9 +49,9 @@ var validate = &cobra.Command{ Short: "Validates the structure of the provided workflow.", Args: cobra.MinimumNArgs(1), Run: func(cmd *cobra.Command, args []string) { - configPath, _ := cmd.Flags().GetString("config") + config := cmd.Context().Value("config").(*commands.Configuration) - if err := commands.ValidateWorkflow(configPath, args[0]); err != nil { + if err := commands.ValidateWorkflow(*config, args[0]); err != nil { log.Printf("Failure: %s", err) os.Exit(1) }