From ff409401c338312af776923ae4188810bd20dafa Mon Sep 17 00:00:00 2001 From: kolaente Date: Fri, 5 Jun 2020 13:48:27 +0200 Subject: [PATCH] Add cluster command --- cmd/cluster.go | 54 +++++++++++++++++++++++++++++++++++++++++++ cmd/root.go | 6 ++--- go.mod | 2 +- parser/from_config.go | 2 +- 4 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 cmd/cluster.go diff --git a/cmd/cluster.go b/cmd/cluster.go new file mode 100644 index 0000000..fc4f534 --- /dev/null +++ b/cmd/cluster.go @@ -0,0 +1,54 @@ +package cmd + +import ( + "bufio" + "context" + "git.jfdev.de/JonasFranzDEV/hal/hal" + "git.jfdev.de/JonasFranzDEV/hal/parser" + "github.com/spf13/cobra" + "gopkg.in/yaml.v2" + "os" +) + +var clusterCmd = &cobra.Command{ + Args: cobra.ExactArgs(1), + Use: "cluster [config file]", + RunE: runClusterCommand, +} + +func init() { + rootCommand.AddCommand(clusterCmd) +} + +func runClusterCommand(cmd *cobra.Command, args []string) error { + configFile := args[0] + + // Parse config + if stats, err := os.Stat(configFile); err != nil || stats.IsDir() { + return err + } + file, err := os.Open(configFile) + if err != nil { + return err + } + scanner := bufio.NewScanner(file) + config := &hal.ClusterConfig{} + if err := yaml.Unmarshal(scanner.Bytes(), config); err != nil { + return err + } + + // Parse modules + modules, err := parser.GetModulesFromConfig(config, debug) + if err != nil { + return err + } + + // Create the cluster + cluster, err := config.BuildCluster(modules) + if err != nil { + return err + } + + // Run + return cluster.Run(context.Background()) +} diff --git a/cmd/root.go b/cmd/root.go index 2fd375a..0c8f04b 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -22,10 +22,10 @@ var rootCommand = &cobra.Command{ Args: cobra.ExactArgs(1), Use: "hal", RunE: runRootCommand, - PreRun: func(cmd *cobra.Command, args []string) { + PersistentPreRun: func(cmd *cobra.Command, args []string) { startTime = time.Now() }, - PostRun: func(cmd *cobra.Command, args []string) { + PersistentPostRun: func(cmd *cobra.Command, args []string) { totalTime := time.Since(startTime) fmt.Printf("Total execution time: %v\n", totalTime) }, @@ -36,7 +36,7 @@ var debug bool var startTime time.Time func init() { - rootCommand.Flags().BoolVarP(&debug, "debug", "d", false, "Enable debug mode") + rootCommand.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "Enable debug mode") } func runRootCommand(cmd *cobra.Command, args []string) error { diff --git a/go.mod b/go.mod index 593f265..192baf3 100644 --- a/go.mod +++ b/go.mod @@ -7,5 +7,5 @@ require ( github.com/spf13/pflag v1.0.5 // indirect github.com/stretchr/testify v1.5.1 golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a - gopkg.in/yaml.v2 v2.3.0 // indirect + gopkg.in/yaml.v2 v2.3.0 ) diff --git a/parser/from_config.go b/parser/from_config.go index 23f1073..6d52579 100644 --- a/parser/from_config.go +++ b/parser/from_config.go @@ -4,7 +4,7 @@ import ( "git.jfdev.de/JonasFranzDEV/hal/hal" ) -func getModulesFromConfig(config *hal.ClusterConfig, debug bool) (modules map[string]*hal.Module, err error) { +func GetModulesFromConfig(config *hal.ClusterConfig, debug bool) (modules map[string]*hal.Module, err error) { // all modules with their name as key modules = make(map[string]*hal.Module)