Skip to content

Commit 150661e

Browse files
committed
Move config file location and names into its own package
1 parent 84c2689 commit 150661e

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

cmd/root.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ package cmd
33
import (
44
"encoding/json"
55
"fmt"
6+
"log"
7+
"os"
8+
"path"
9+
10+
"github.com/msp301/zb/config"
611
"github.com/msp301/zb/editor"
712
"github.com/msp301/zb/graph"
813
"github.com/spf13/cobra"
914
"github.com/spf13/viper"
10-
"log"
11-
"os"
12-
"path"
1315
)
1416

1517
var cfgFile string
@@ -51,14 +53,11 @@ func initConfig() {
5153
if cfgFile != "" {
5254
viper.SetConfigFile(cfgFile)
5355
} else {
54-
home, err := os.UserHomeDir()
55-
cobra.CheckErr(err)
56-
5756
viper.AddConfigPath(".")
58-
viper.AddConfigPath(home)
57+
viper.AddConfigPath(config.GlobalConfigDir)
5958

60-
viper.SetConfigType("toml")
61-
viper.SetConfigName(".zb")
59+
viper.SetConfigType(config.CONFIG_TYPE)
60+
viper.SetConfigName(config.CONFIG_NAME)
6261
}
6362

6463
if err := viper.ReadInConfig(); err != nil {

config/config.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package config
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"strings"
7+
)
8+
9+
const CONFIG_NAME = ".zb"
10+
const CONFIG_TYPE = "toml"
11+
12+
var ConfigFile string
13+
var GlobalConfigDir string
14+
var GlobalConfigFile string
15+
16+
func init() {
17+
ConfigFile = strings.Join([]string{CONFIG_NAME, CONFIG_TYPE}, ".")
18+
19+
GlobalConfigDir = ""
20+
home, err := os.UserHomeDir()
21+
if err == nil {
22+
GlobalConfigDir = home
23+
}
24+
25+
GlobalConfigFile = filepath.Join(GlobalConfigDir, ConfigFile)
26+
}

0 commit comments

Comments
 (0)