2018-03-01 00:17:22 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2018-03-25 23:08:27 +00:00
|
|
|
"strings"
|
2018-03-01 00:17:22 +00:00
|
|
|
|
|
|
|
"github.com/mitchellh/go-homedir"
|
2018-03-26 00:03:56 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2018-03-01 00:17:22 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
2018-03-25 21:37:32 +00:00
|
|
|
|
2021-07-12 21:57:54 +00:00
|
|
|
"git.mills.io/prologic/msgbus"
|
2018-03-01 00:17:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var configFile string
|
|
|
|
|
|
|
|
// RootCmd represents the base command when called without any subcommands
|
|
|
|
var RootCmd = &cobra.Command{
|
2018-03-25 21:37:32 +00:00
|
|
|
Use: "msgbus",
|
|
|
|
Version: msgbus.FullVersion(),
|
|
|
|
Short: "Command-line client for msgbus",
|
2018-03-01 00:17:22 +00:00
|
|
|
Long: `This is the command-line client for the msgbus daemon msgbusd.
|
|
|
|
|
|
|
|
This lets you publish, subscribe and pull messages from a running msgbusd
|
|
|
|
instance. This is the reference implementation of using the msgbus client
|
|
|
|
library for publishing and subscribing to topics.`,
|
2018-05-07 19:55:01 +00:00
|
|
|
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
|
|
|
// set logging level
|
|
|
|
if viper.GetBool("debug") {
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
} else {
|
|
|
|
log.SetLevel(log.InfoLevel)
|
|
|
|
}
|
|
|
|
},
|
2018-03-01 00:17:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute adds all child commands to the root command
|
|
|
|
// and sets flags appropriately.
|
|
|
|
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
|
|
|
func Execute() {
|
|
|
|
if err := RootCmd.Execute(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cobra.OnInitialize(initConfig)
|
|
|
|
|
|
|
|
RootCmd.PersistentFlags().StringVar(
|
|
|
|
&configFile, "config", "",
|
|
|
|
"config file (default is $HOME/.msgbus.yaml)",
|
|
|
|
)
|
|
|
|
|
2018-03-26 00:03:56 +00:00
|
|
|
RootCmd.PersistentFlags().BoolP(
|
|
|
|
"debug", "d", false,
|
|
|
|
"Enable debug logging",
|
|
|
|
)
|
|
|
|
|
2018-03-01 00:17:22 +00:00
|
|
|
RootCmd.PersistentFlags().StringP(
|
|
|
|
"uri", "u", "http://localhost:8000",
|
|
|
|
"URI to connect to msgbusd",
|
|
|
|
)
|
2018-03-25 23:08:27 +00:00
|
|
|
|
|
|
|
viper.BindPFlag("uri", RootCmd.PersistentFlags().Lookup("uri"))
|
|
|
|
viper.SetDefault("uri", "http://localhost:8000/")
|
2018-03-26 00:03:56 +00:00
|
|
|
|
|
|
|
viper.BindPFlag("debug", RootCmd.PersistentFlags().Lookup("debug"))
|
|
|
|
viper.SetDefault("debug", false)
|
2018-03-01 00:17:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// initConfig reads in config file and ENV variables if set.
|
|
|
|
func initConfig() {
|
|
|
|
if configFile != "" {
|
|
|
|
// Use config file from the flag.
|
|
|
|
viper.SetConfigFile(configFile)
|
|
|
|
} else {
|
|
|
|
// Find home directory.
|
|
|
|
home, err := homedir.Dir()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
viper.AddConfigPath(home)
|
2018-03-25 23:08:27 +00:00
|
|
|
viper.SetConfigName(".msgbus.yaml")
|
2018-03-01 00:17:22 +00:00
|
|
|
}
|
|
|
|
|
2018-03-25 23:08:27 +00:00
|
|
|
// from the environment
|
|
|
|
viper.SetEnvPrefix("MSGBUS")
|
|
|
|
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
2018-03-01 00:17:22 +00:00
|
|
|
viper.AutomaticEnv() // read in environment variables that match
|
|
|
|
|
|
|
|
// If a config file is found, read it in.
|
|
|
|
if err := viper.ReadInConfig(); err == nil {
|
|
|
|
fmt.Println("Using config file:", viper.ConfigFileUsed())
|
|
|
|
}
|
|
|
|
}
|