Skip to content

Commit

Permalink
Add option to pass a config file location. closes #7
Browse files Browse the repository at this point in the history
  • Loading branch information
chclaus committed May 27, 2018
1 parent c590631 commit 31b8fe2
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
7 changes: 7 additions & 0 deletions cmd/root.go
Expand Up @@ -25,6 +25,7 @@ import (
"os"

"github.com/spf13/cobra"
"github.com/chclaus/dt/config"
)

// RootCmd represents the base command when called without any subcommands
Expand All @@ -45,3 +46,9 @@ func Execute() {
os.Exit(1)
}
}

func init() {
RootCmd.PersistentFlags().StringVar(&config.CfgFile, "config", "", "config file (default is $HOME/.dt.yaml)")

cobra.OnInitialize(config.InitConfig)
}
2 changes: 0 additions & 2 deletions cmd/server/server.go
Expand Up @@ -77,8 +77,6 @@ var serverCmd = &cobra.Command{
}

func init() {
cobra.OnInitialize(config.InitConfig)

cmd.RootCmd.AddCommand(serverCmd)

serverCmd.Flags().StringP("address", "a", "0.0.0.0", "the hostname or ip address")
Expand Down
1 change: 0 additions & 1 deletion cmd/uuid/uuid.go
Expand Up @@ -96,7 +96,6 @@ var uuidCmd = &cobra.Command{

func init() {
cmd.RootCmd.AddCommand(uuidCmd)
cobra.OnInitialize(config.InitConfig)

uuidCmd.Flags().StringP("namespace", "n", "", "the namespace that should be hashed in UUID v3 or UUID v5. It should be a domain or application specific UUID")
uuidCmd.Flags().IntVarP(&version, "version", "v", 4, "defines the version of the generated uuid")
Expand Down
2 changes: 1 addition & 1 deletion cmd/version/version.go
Expand Up @@ -32,7 +32,7 @@ var versionCmd = &cobra.Command{
Short: "Prints the current version of the dt",
Long: "All software has versions. This is dt's",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("dt - the dev toolbelt v1.0.2 🤓")
fmt.Println("dt - the dev toolbelt v1.0.4 👻")
},
}

Expand Down
16 changes: 14 additions & 2 deletions config/config.go
Expand Up @@ -5,6 +5,8 @@ import (
"github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
"os"
"path/filepath"
"strings"
)

// Config is the root collection of command configurations
Expand Down Expand Up @@ -55,6 +57,7 @@ type JWTConfig struct {
}

// Cfg the root object of the configuration
var CfgFile string
var Cfg *Config

// InitConfig initializes the configuration if provided.
Expand All @@ -65,8 +68,17 @@ func InitConfig() {
os.Exit(1)
}

viper.SetConfigName(".dt")
viper.AddConfigPath(home)
if CfgFile == "" {
viper.SetConfigName(".dt")
viper.AddConfigPath(home)

} else {
base := filepath.Base(strings.TrimSuffix(CfgFile, filepath.Ext(CfgFile)))
viper.SetConfigName(base)
dir := filepath.Dir(CfgFile)
viper.AddConfigPath(strings.Replace(dir, "~", home, 1))
}

viper.ReadInConfig()
viper.Unmarshal(&Cfg)
}

0 comments on commit 31b8fe2

Please sign in to comment.