Add cluster command

master
kolaente 4 years ago
parent 5efab86f1a
commit ff409401c3
Signed by: kolaente
GPG Key ID: F40E70337AB24C9B
  1. 54
      cmd/cluster.go
  2. 6
      cmd/root.go
  3. 2
      go.mod
  4. 2
      parser/from_config.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())
}

@ -22,10 +22,10 @@ var rootCommand = &cobra.Command{
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
Use: "hal", Use: "hal",
RunE: runRootCommand, RunE: runRootCommand,
PreRun: func(cmd *cobra.Command, args []string) { PersistentPreRun: func(cmd *cobra.Command, args []string) {
startTime = time.Now() startTime = time.Now()
}, },
PostRun: func(cmd *cobra.Command, args []string) { PersistentPostRun: func(cmd *cobra.Command, args []string) {
totalTime := time.Since(startTime) totalTime := time.Since(startTime)
fmt.Printf("Total execution time: %v\n", totalTime) fmt.Printf("Total execution time: %v\n", totalTime)
}, },
@ -36,7 +36,7 @@ var debug bool
var startTime time.Time var startTime time.Time
func init() { 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 { func runRootCommand(cmd *cobra.Command, args []string) error {

@ -7,5 +7,5 @@ require (
github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.5.1 github.com/stretchr/testify v1.5.1
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a
gopkg.in/yaml.v2 v2.3.0 // indirect gopkg.in/yaml.v2 v2.3.0
) )

@ -4,7 +4,7 @@ import (
"git.jfdev.de/JonasFranzDEV/hal/hal" "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 // all modules with their name as key
modules = make(map[string]*hal.Module) modules = make(map[string]*hal.Module)

Loading…
Cancel
Save