func Execute() { config.GetConfig().Load() checkFleetVersion() var logLevel string var rootCmd = &cobra.Command{ Use: "green-garden", PersistentPreRun: func(cmd *cobra.Command, args []string) { level, err := log.ParseLevel(logLevel) if err != nil { fmt.Printf("Unknown log level : %s\n", logLevel) os.Exit(1) } log.SetLevel(level) }, } rootCmd.PersistentFlags().StringVarP(&logLevel, "loglevel", "L", "info", "Set log level") rootCmd.AddCommand(versionCmd, generateCmd) loadEnvCommands(rootCmd) err := rootCmd.Execute() if err != nil { os.Exit(1) } log.Info("Victory !") }
func (w Work) ListEnvs() []string { path := config.GetConfig().WorkPath + PATH_ENV if _, err := os.Stat(path); os.IsNotExist(err) { log.Error("env directory not found in " + config.GetConfig().WorkPath) os.Exit(1) } files, err := ioutil.ReadDir(path) if err != nil { log.Error("Cannot read env directory : "+path, err) os.Exit(1) } var envs []string for _, file := range files { if !file.IsDir() { continue } envs = append(envs, file.Name()) } return envs }
func loadEnvCommands(rootCmd *cobra.Command) { log.WithField("path", config.GetConfig().WorkPath).Debug("Loading envs") work := work.NewWork(config.GetConfig().WorkPath) for _, f := range work.ListEnvs() { var env = f var envCmd = &cobra.Command{ Use: env, Short: "Run command for " + env, } var check = &cobra.Command{ Use: "check", Short: "Check local units with what is running on fleet on " + env, Run: func(cmd *cobra.Command, args []string) { checkEnv(cmd, args, work, env) }, } var runCmd = &cobra.Command{ Use: "fleetctl", Short: "Run fleetctl command on " + env, Run: func(cmd *cobra.Command, args []string) { fleetctl(cmd, args, work, env) }, } envCmd.AddCommand(runCmd, check) var generateCmd = &cobra.Command{ Use: "generate", Short: "Generate units for " + env, Run: func(cmd *cobra.Command, args []string) { generateEnv(cmd, args, work, env) }, } envCmd.AddCommand(generateCmd) rootCmd.AddCommand(envCmd) for _, g := range work.LoadEnv(env).ListServices() { var service = g var serviceCmd = &cobra.Command{ Use: service, Short: "run command for " + service + " on env " + env, } var checkCmd = &cobra.Command{ Use: "check", Short: "Check local units matches what is running on " + env + " for " + service, Run: func(cmd *cobra.Command, args []string) { checkService(cmd, args, work, env, service) }, } var generateCmd = &cobra.Command{ Use: "generate [manifest...]", Short: "generate units for " + service + " on env :" + env, Long: `generate units using remote resolved or local pod/aci manifests`, Run: func(cmd *cobra.Command, args []string) { generateService(cmd, args, work, env, service) }, } serviceCmd.AddCommand(generateCmd, checkCmd) envCmd.AddCommand(serviceCmd) } } }
package commands import ( "github.com/Sirupsen/logrus" "github.com/blablacar/green-garden/config" "github.com/blablacar/green-garden/work" "github.com/spf13/cobra" ) var generateCmd = &cobra.Command{ Use: "generate", Short: "generate units for all envs", Run: func(cmd *cobra.Command, args []string) { work := work.NewWork(config.GetConfig().WorkPath) for _, envName := range work.ListEnvs() { env := work.LoadEnv(envName) env.Generate() } }, } func generateService(cmd *cobra.Command, args []string, work *work.Work, env string, service string) { work.LoadEnv(env).LoadService(service).GenerateUnits(args) } func generateEnv(cmd *cobra.Command, args []string, work *work.Work, env string) { logrus.WithField("env", env).Debug("Generating units") work.LoadEnv(env).Generate() }