courgette/main.go

76 lines
1.7 KiB
Go
Raw Normal View History

package main
import (
"context"
2024-08-02 23:49:56 +00:00
commands "courgette/internal/commands"
2024-08-03 17:10:01 +00:00
logger "courgette/internal/logging"
"github.com/spf13/cobra"
"os"
)
var cli = &cobra.Command{
Use: "runner",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
2024-08-03 17:10:01 +00:00
logger.ConfigureLogger()
configPath, err := cmd.Flags().GetString("config")
ctx := cmd.Context()
if err == nil {
configuration, err := commands.NewConfigFromFile(configPath)
if err != nil {
2024-08-03 17:10:01 +00:00
logger.Error(logger.Red("Failed to parse configuration (%s)!"), configPath)
os.Exit(1)
}
ctx = context.WithValue(ctx, "config", configuration)
}
cmd.SetContext(ctx)
},
}
var execute = &cobra.Command{
Use: "execute [workflow-file]",
Short: "Executes the provided workflow.",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
config := cmd.Context().Value("config").(*commands.Configuration)
if err := commands.ExecuteWorkflow(*config, args[0]); err != nil {
2024-08-03 17:10:01 +00:00
logger.Error(logger.Red("%s"), err)
os.Exit(1)
}
},
}
var validate = &cobra.Command{
Use: "validate [workflow-file]",
Short: "Validates the structure of the provided workflow.",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
config := cmd.Context().Value("config").(*commands.Configuration)
if err := commands.ValidateWorkflow(*config, args[0]); err != nil {
2024-08-03 17:10:01 +00:00
logger.Error(logger.Red("%s"), err)
os.Exit(1)
}
},
}
var knownCommands = []*cobra.Command{
execute,
validate,
}
func main() {
cli.PersistentFlags().StringP("config", "c", "./config.yml", "Declarative runner configuration.")
for _, command := range knownCommands {
cli.AddCommand(command)
}
cli.Execute()
}