Просмотр исходного кода

refactor: set config directory path in constants package

ayn2op 1 год назад
Родитель
Сommit
d316eb2133
2 измененных файлов с 17 добавлено и 6 удалено
  1. 1 6
      internal/config/config.go
  2. 16 0
      internal/constants/constants.go

+ 1 - 6
internal/config/config.go

@@ -42,13 +42,8 @@ func DefaultConfig() Config {
 
 
 // Reads the configuration file and parses it.
 // Reads the configuration file and parses it.
 func Load() (*Config, error) {
 func Load() (*Config, error) {
-	path, err := os.UserConfigDir()
-	if err != nil {
-		return nil, err
-	}
-
 	cfg := DefaultConfig()
 	cfg := DefaultConfig()
-	path = filepath.Join(path, constants.Name, "config.toml")
+	path := filepath.Join(constants.ConfigDirPath, "config.toml")
 	f, err := os.Open(path)
 	f, err := os.Open(path)
 	if os.IsNotExist(err) {
 	if os.IsNotExist(err) {
 		return &cfg, nil
 		return &cfg, nil

+ 16 - 0
internal/constants/constants.go

@@ -1,7 +1,23 @@
 package constants
 package constants
 
 
+import (
+	"os"
+	"path/filepath"
+)
+
 const Name = "discordo"
 const Name = "discordo"
 
 
 const UserAgent = Name + "/0.1 (https://github.com/diamondburned/arikawa, v3)"
 const UserAgent = Name + "/0.1 (https://github.com/diamondburned/arikawa, v3)"
 
 
 const TmpFilePattern = Name + "_*.md"
 const TmpFilePattern = Name + "_*.md"
+
+var ConfigDirPath string
+
+func init() {
+	path, err := os.UserConfigDir()
+	if err != nil {
+		path = "."
+	}
+
+	ConfigDirPath = filepath.Join(path, Name)
+}