package cmd import ( "fmt" "os" "git.rodere.systems/stream-service/server" "github.com/spf13/cobra" "github.com/spf13/viper" ) var ( // Used for flags. cfgFile string userLicense string versionCmd = &cobra.Command{ Use: "version", Short: "Print the version number of stream", Long: `All software has versions. This is stream's`, Run: func(cmd *cobra.Command, args []string) { fmt.Println("Stream Dummy gRPC/REST API v0.1 -- HEAD") }, } serverCmd = &cobra.Command{ Use: "server", Short: "Run stream server", Long: `Run the stream server.`, Run: func(cmd *cobra.Command, args []string) { server.runServers() }, } rootCmd = &cobra.Command{ Use: "stream", Short: "A dummy service that provides a gRPC/REST API providing Stream Data", Long: "Stream is a dummy service that serves a streaming data from a sink via gRPC/REST API.", } ) // Execute executes the root command. func Execute() error { return rootCmd.Execute() } func init() { cobra.OnInitialize(initConfig) rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $STREAM_PATH/.stream.toml)") rootCmd.PersistentFlags().StringP("author", "a", "Carlos Sosa", "author name for copyright attribution") rootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "AGPL-3", "name of license for the project") rootCmd.PersistentFlags().Bool("viper", true, "use Viper for configuration") viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author")) viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper")) viper.SetDefault("author", "Carlos Sosa ") viper.SetDefault("license", "AGPL-3") //rootCmd.AddCommand(addCmd) rootCmd.AddCommand(versionCmd) //rootCmd.AddCommand(serverCmd) } func er(msg interface{}) { fmt.Println("Error:", msg) os.Exit(1) } func initConfig() { if cfgFile != "" { // Use config file from the flag. viper.SetConfigFile(cfgFile) } else { // Search config in home directory with name ".cobra" (without extension). viper.AddConfigPath("./") viper.SetConfigName(".stream.toml") } viper.AutomaticEnv() if err := viper.ReadInConfig(); err == nil { fmt.Println("Using config file:", viper.ConfigFileUsed()) } else { fmt.Println("Error loading config file:", viper.ConfigFileUsed()) fmt.Println("Error message from loading config file:", err) } }