refactor: hoist config handling in root command prerun
This commit is contained in:
parent
bed10050dd
commit
d519aa4cd1
3 changed files with 25 additions and 12 deletions
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
27
main.go
27
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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue