Skip to content

Commit 31b8fe2

Browse files
committed
Add option to pass a config file location. closes #7
1 parent c590631 commit 31b8fe2

File tree

5 files changed

+22
-6
lines changed

5 files changed

+22
-6
lines changed

cmd/root.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"os"
2626

2727
"github.com/spf13/cobra"
28+
"github.com/chclaus/dt/config"
2829
)
2930

3031
// RootCmd represents the base command when called without any subcommands
@@ -45,3 +46,9 @@ func Execute() {
4546
os.Exit(1)
4647
}
4748
}
49+
50+
func init() {
51+
RootCmd.PersistentFlags().StringVar(&config.CfgFile, "config", "", "config file (default is $HOME/.dt.yaml)")
52+
53+
cobra.OnInitialize(config.InitConfig)
54+
}

cmd/server/server.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ var serverCmd = &cobra.Command{
7777
}
7878

7979
func init() {
80-
cobra.OnInitialize(config.InitConfig)
81-
8280
cmd.RootCmd.AddCommand(serverCmd)
8381

8482
serverCmd.Flags().StringP("address", "a", "0.0.0.0", "the hostname or ip address")

cmd/uuid/uuid.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ var uuidCmd = &cobra.Command{
9696

9797
func init() {
9898
cmd.RootCmd.AddCommand(uuidCmd)
99-
cobra.OnInitialize(config.InitConfig)
10099

101100
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")
102101
uuidCmd.Flags().IntVarP(&version, "version", "v", 4, "defines the version of the generated uuid")

cmd/version/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var versionCmd = &cobra.Command{
3232
Short: "Prints the current version of the dt",
3333
Long: "All software has versions. This is dt's",
3434
Run: func(cmd *cobra.Command, args []string) {
35-
fmt.Println("dt - the dev toolbelt v1.0.2 🤓")
35+
fmt.Println("dt - the dev toolbelt v1.0.4 👻")
3636
},
3737
}
3838

config/config.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"github.com/mitchellh/go-homedir"
66
"github.com/spf13/viper"
77
"os"
8+
"path/filepath"
9+
"strings"
810
)
911

1012
// Config is the root collection of command configurations
@@ -55,6 +57,7 @@ type JWTConfig struct {
5557
}
5658

5759
// Cfg the root object of the configuration
60+
var CfgFile string
5861
var Cfg *Config
5962

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

68-
viper.SetConfigName(".dt")
69-
viper.AddConfigPath(home)
71+
if CfgFile == "" {
72+
viper.SetConfigName(".dt")
73+
viper.AddConfigPath(home)
74+
75+
} else {
76+
base := filepath.Base(strings.TrimSuffix(CfgFile, filepath.Ext(CfgFile)))
77+
viper.SetConfigName(base)
78+
dir := filepath.Dir(CfgFile)
79+
viper.AddConfigPath(strings.Replace(dir, "~", home, 1))
80+
}
81+
7082
viper.ReadInConfig()
7183
viper.Unmarshal(&Cfg)
7284
}

0 commit comments

Comments
 (0)