diff options
| author | Carlos Sosa <gnusosa@gnusosa.net> | 2020-06-22 11:33:39 -0700 |
|---|---|---|
| committer | Carlos Sosa <gnusosa@gnusosa.net> | 2020-06-22 11:33:39 -0700 |
| commit | 0b609156b184d00ab1a6e742b9b998be4457345e (patch) | |
| tree | 9283646735cf0f7a984ff0fe4013f6abc7a8be4f /cmd/cmd.go | |
First Commit
Diffstat (limited to 'cmd/cmd.go')
| -rw-r--r-- | cmd/cmd.go | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/cmd/cmd.go b/cmd/cmd.go new file mode 100644 index 0000000..ea69e29 --- /dev/null +++ b/cmd/cmd.go @@ -0,0 +1,88 @@ +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 <gnusosa@gnusosa.net>") + 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) + } + +} |
