github.com/moleculer-go/moleculer@v0.3.3/cli/cmd/start.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "os" 6 "os/signal" 7 "syscall" 8 9 "github.com/moleculer-go/moleculer" 10 "github.com/moleculer-go/moleculer/broker" 11 "github.com/spf13/cobra" 12 "github.com/spf13/viper" 13 ) 14 15 var environment string 16 17 // startCmd starts the service broker 18 var startCmd = &cobra.Command{ 19 Use: "start", 20 Short: "starts the service broker.", 21 Long: `starts the service broker and publish all microservices added.`, 22 Run: func(cmd *cobra.Command, args []string) { 23 fmt.Println("start called - UserOpts.Config -> ", UserOpts.Config) 24 if UserOpts == nil { 25 panic("No options set!") 26 } 27 28 argsConfig := argsToConfig(cmd) 29 services := viper.GetStringMap("services") 30 if services != nil { 31 UserOpts.Config.Services = services 32 } 33 broker := broker.New(UserOpts.Config, argsConfig) 34 35 signalC := make(chan os.Signal) 36 signal.Notify(signalC, os.Interrupt, syscall.SIGTERM) 37 38 UserOpts.StartHandler(broker, cmd) 39 <-signalC 40 broker.Stop() 41 }, 42 } 43 44 // argsToConfig read args sent ot the CLI and populate a molecule config. 45 func argsToConfig(cmd *cobra.Command) *moleculer.Config { 46 //TODO 47 return &moleculer.Config{} 48 } 49 50 func init() { 51 RootCmd.AddCommand(startCmd) 52 53 // Here you will define your flags and configuration settings. 54 55 // Cobra supports Persistent Flags which will work for this command 56 // and all subcommands, e.g.: 57 startCmd.PersistentFlags().StringVarP(&environment, "env", "e", "ENV", "Environment name.") 58 59 // Cobra supports local flags which will only run when this command 60 // is called directly, e.g.: 61 // helloCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") 62 }