2024-08-01 22:43:18 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-08-02 21:32:41 +00:00
|
|
|
"context"
|
2024-08-02 23:49:56 +00:00
|
|
|
commands "courgette/internal/commands"
|
2024-10-06 14:58:14 +00:00
|
|
|
daemon "courgette/internal/daemon"
|
2024-08-03 17:10:01 +00:00
|
|
|
logger "courgette/internal/logging"
|
2024-08-01 22:43:18 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
var cli = &cobra.Command{
|
|
|
|
Use: "runner",
|
2024-08-02 21:32:41 +00:00
|
|
|
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
2024-08-03 17:10:01 +00:00
|
|
|
logger.ConfigureLogger()
|
2024-08-02 21:32:41 +00:00
|
|
|
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)
|
2024-08-02 21:32:41 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = context.WithValue(ctx, "config", configuration)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.SetContext(ctx)
|
|
|
|
},
|
2024-08-01 22:43:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var execute = &cobra.Command{
|
|
|
|
Use: "execute [workflow-file]",
|
|
|
|
Short: "Executes the provided workflow.",
|
2024-08-02 21:38:00 +00:00
|
|
|
Args: cobra.ExactArgs(1),
|
2024-08-01 22:43:18 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2024-08-02 21:32:41 +00:00
|
|
|
config := cmd.Context().Value("config").(*commands.Configuration)
|
2024-08-01 22:43:18 +00:00
|
|
|
|
2024-08-02 21:32:41 +00:00
|
|
|
if err := commands.ExecuteWorkflow(*config, args[0]); err != nil {
|
2024-08-03 17:10:01 +00:00
|
|
|
logger.Error(logger.Red("%s"), err)
|
2024-08-01 22:43:18 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var validate = &cobra.Command{
|
|
|
|
Use: "validate [workflow-file]",
|
|
|
|
Short: "Validates the structure of the provided workflow.",
|
2024-08-02 21:38:00 +00:00
|
|
|
Args: cobra.ExactArgs(1),
|
2024-08-01 22:43:18 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2024-08-02 21:32:41 +00:00
|
|
|
config := cmd.Context().Value("config").(*commands.Configuration)
|
2024-08-01 22:43:18 +00:00
|
|
|
|
2024-08-02 21:32:41 +00:00
|
|
|
if err := commands.ValidateWorkflow(*config, args[0]); err != nil {
|
2024-08-03 17:10:01 +00:00
|
|
|
logger.Error(logger.Red("%s"), err)
|
2024-08-01 22:43:18 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-10-06 14:58:14 +00:00
|
|
|
var daemonCmd = &cobra.Command{
|
|
|
|
Use: "daemon",
|
|
|
|
Short: "Starts the daemon process",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
// FIXME: Sample daemon setup.
|
|
|
|
d := daemon.NewDaemon()
|
|
|
|
d.Start(":8080")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-08-01 22:43:18 +00:00
|
|
|
var knownCommands = []*cobra.Command{
|
|
|
|
execute,
|
|
|
|
validate,
|
2024-10-06 14:58:14 +00:00
|
|
|
daemonCmd,
|
2024-08-01 22:43:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
cli.PersistentFlags().StringP("config", "c", "./config.yml", "Declarative runner configuration.")
|
|
|
|
|
|
|
|
for _, command := range knownCommands {
|
|
|
|
cli.AddCommand(command)
|
|
|
|
}
|
|
|
|
|
|
|
|
cli.Execute()
|
|
|
|
}
|