Allow msgbus CLI to get config from cli options, env and config (12-factor)
This commit is contained in:
parent
def412687a
commit
fc8df01b2c
|
@ -6,6 +6,7 @@ import (
|
|||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"github.com/prologic/msgbus/client"
|
||||
)
|
||||
|
@ -21,7 +22,7 @@ This is an asynchronous operation and does not wait for a response unless the
|
|||
-w/--wait option is also present.`,
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
uri := cmd.Flag("uri").Value.String()
|
||||
uri := viper.GetString("uri")
|
||||
client := client.NewClient(uri, nil)
|
||||
|
||||
topic := args[0]
|
||||
|
|
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"github.com/prologic/msgbus/client"
|
||||
)
|
||||
|
@ -18,7 +19,7 @@ This is primarily useful in situations where a subscription was lost and you
|
|||
want to "catch up" and pull any messages left in the queue for that topic.`,
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
uri := cmd.Flag("uri").Value.String()
|
||||
uri := viper.GetString("uri")
|
||||
client := client.NewClient(uri, nil)
|
||||
|
||||
topic := args[0]
|
||||
|
|
|
@ -3,6 +3,7 @@ package main
|
|||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/mitchellh/go-homedir"
|
||||
"github.com/spf13/cobra"
|
||||
|
@ -47,6 +48,9 @@ func init() {
|
|||
"uri", "u", "http://localhost:8000",
|
||||
"URI to connect to msgbusd",
|
||||
)
|
||||
|
||||
viper.BindPFlag("uri", RootCmd.PersistentFlags().Lookup("uri"))
|
||||
viper.SetDefault("uri", "http://localhost:8000/")
|
||||
}
|
||||
|
||||
// initConfig reads in config file and ENV variables if set.
|
||||
|
@ -62,11 +66,13 @@ func initConfig() {
|
|||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Search config in home directory with name ".cobra-example" (without extension).
|
||||
viper.AddConfigPath(home)
|
||||
viper.SetConfigName(".cobra-example")
|
||||
viper.SetConfigName(".msgbus.yaml")
|
||||
}
|
||||
|
||||
// from the environment
|
||||
viper.SetEnvPrefix("MSGBUS")
|
||||
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||
viper.AutomaticEnv() // read in environment variables that match
|
||||
|
||||
// If a config file is found, read it in.
|
||||
|
|
|
@ -7,6 +7,7 @@ import (
|
|||
"syscall"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"github.com/prologic/msgbus/client"
|
||||
)
|
||||
|
@ -19,7 +20,7 @@ var subCmd = &cobra.Command{
|
|||
to the topic, the message is printed to standard output.`,
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
uri := cmd.Flag("uri").Value.String()
|
||||
uri := viper.GetString("uri")
|
||||
client := client.NewClient(uri, nil)
|
||||
|
||||
topic := args[0]
|
||||
|
|
Loading…
Reference in New Issue