github.com/moleculer-go/moleculer@v0.3.3/cli/cmd/root.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path"
     7  
     8  	"github.com/moleculer-go/moleculer"
     9  	"github.com/moleculer-go/moleculer/broker"
    10  	"github.com/spf13/cobra"
    11  	"github.com/spf13/viper"
    12  )
    13  
    14  var cfgFile string
    15  
    16  // RootCmd represents the base command when called without any subcommands
    17  var RootCmd = &cobra.Command{
    18  	Use:   "mol",
    19  	Short: "Moleculer Go CLI",
    20  	Long:  `Moleculer CLI allows u to control the lifecycle of your service.`,
    21  	// Uncomment the following line if your bare application
    22  	// has an action associated with it:
    23  	//	Run: func(cmd *cobra.Command, args []string) { },
    24  }
    25  
    26  type RunOpts struct {
    27  	Config       *moleculer.Config
    28  	StartHandler func(*broker.ServiceBroker, *cobra.Command)
    29  }
    30  
    31  var UserOpts *RunOpts
    32  
    33  // Execute adds all child commands to the root command and sets flags appropriately.
    34  // This is called by main.main(). It only needs to happen once to the rootCmd.
    35  func Execute(runOpts RunOpts) {
    36  	UserOpts = &runOpts
    37  	if err := RootCmd.Execute(); err != nil {
    38  		fmt.Println(err)
    39  		os.Exit(1)
    40  	}
    41  }
    42  
    43  func init() {
    44  	cobra.OnInitialize(initConfig)
    45  
    46  	// Here you will define your flags and configuration settings.
    47  	// Cobra supports persistent flags, which, if defined here,
    48  	// will be global for your application.
    49  	RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is <app>/.moleculer-config.yaml)")
    50  
    51  	// RootCmd.PersistentFlags().StringVar(&UserOpts.Config.LogLevel, "log", "l", "Log Level - fatal, error, debug, trace")
    52  	// viper.BindPFlag("log", RootCmd.PersistentFlags().Lookup("log"))
    53  
    54  	// RootCmd.PersistentFlags().StringVar(&UserOpts.Config.LogFormat, "logFormat", "lf", "Log Format - Options: JSON or TEXT")
    55  	// viper.BindPFlag("logFormat", RootCmd.PersistentFlags().Lookup("logFormat"))
    56  
    57  	// RootCmd.PersistentFlags().StringVar(&UserOpts.Config.Transporter, "transporter", "t", "Transporter")
    58  	// viper.BindPFlag("transporter", RootCmd.PersistentFlags().Lookup("transporter"))
    59  
    60  	// RootCmd.PersistentFlags().StringVar(&UserOpts.Config.Namespace, "namespace", "n", "Namespace")
    61  	// viper.BindPFlag("namespace", RootCmd.PersistentFlags().Lookup("namespace"))
    62  }
    63  
    64  // initConfig reads in config file and ENV variables if set.
    65  func initConfig() {
    66  	if cfgFile != "" {
    67  		// Use config file from the flag.
    68  		viper.SetConfigFile(cfgFile)
    69  	} else {
    70  		// Find executable directory.
    71  		basePath, err := os.Executable()
    72  		if err != nil {
    73  			fmt.Println(err)
    74  			os.Exit(1)
    75  		}
    76  		viper.AddConfigPath(path.Dir(basePath))
    77  		viper.AddConfigPath(".")
    78  		viper.SetConfigName("moleculer-config")
    79  	}
    80  
    81  	viper.AutomaticEnv() // read in environment variables that match
    82  
    83  	// If a config file is found, read it in.
    84  	if err := viper.ReadInConfig(); err == nil {
    85  		fmt.Println("Using config file:", viper.ConfigFileUsed())
    86  	} else {
    87  		fmt.Println("Error loading config - Error: ", err)
    88  	}
    89  }