@@ -3,6 +3,7 @@ package config
3
3
import (
4
4
"encoding/json"
5
5
"errors"
6
+ "fmt"
6
7
"os"
7
8
"path/filepath"
8
9
"sync"
@@ -32,9 +33,46 @@ type Config struct {
32
33
ServerPort string `json:"server_port"` // port to run the server on
33
34
}
34
35
36
+ // Merges file config with default config
37
+ func mergeConfigs (config , defaultConfig * Config ) * Config {
38
+ if config .RetroPath == "" {
39
+ config .RetroPath = defaultConfig .RetroPath
40
+ }
41
+ if config .PathYTDL == "" {
42
+ config .PathYTDL = defaultConfig .PathYTDL
43
+ }
44
+ if config .PathFFmpeg == "" {
45
+ config .PathFFmpeg = defaultConfig .PathFFmpeg
46
+ }
47
+ if config .PathFFprobe == "" {
48
+ config .PathFFprobe = defaultConfig .PathFFprobe
49
+ }
50
+ if config .SearchTimeout == 0 {
51
+ config .SearchTimeout = defaultConfig .SearchTimeout
52
+ }
53
+ if config .Theme == "" {
54
+ config .Theme = defaultConfig .Theme
55
+ }
56
+ if config .LogFile == "" {
57
+ config .LogFile = defaultConfig .LogFile
58
+ }
59
+ if config .DBPath == "" {
60
+ config .DBPath = defaultConfig .DBPath
61
+ }
62
+ if config .ServerPort == "" {
63
+ config .ServerPort = defaultConfig .ServerPort
64
+ }
65
+ // No need to check boolean field (DiscordRPC) since false is a meaningful value
66
+ return config
67
+ }
68
+
35
69
func initConfig () * Config {
36
70
retro_path := os .Getenv ("HOME" ) + "/.retro/"
37
- config := & Config {
71
+ configPath := filepath .Join (retro_path , "config.json" )
72
+ var config * Config
73
+
74
+ // Load default config
75
+ defaultConfig := & Config {
38
76
RetroPath : retro_path ,
39
77
PathYTDL : "yt-dlp" ,
40
78
PathFFmpeg : "ffmpeg" ,
@@ -49,12 +87,18 @@ func initConfig() *Config {
49
87
50
88
// Attempt to load from file
51
89
if jsonFile , err := os .ReadFile (configPath ); err == nil {
52
- if err = json .Unmarshal (jsonFile , config ); err != nil {
53
- return config // Return default config if unmarshaling fails
90
+ config = & Config {}
91
+ if err = json .Unmarshal (jsonFile , config ); err == nil {
92
+ // Merge file config with default config
93
+ config = mergeConfigs (config , defaultConfig )
94
+ return config
95
+ } else {
96
+ fmt .Println ("Error loading config file:" , err )
54
97
}
55
98
}
56
99
57
- return config
100
+ // If we can't load from file, return the default config
101
+ return defaultConfig
58
102
}
59
103
60
104
func GetConfig () * Config {
@@ -63,7 +107,6 @@ func GetConfig() *Config {
63
107
})
64
108
return cfg
65
109
}
66
-
67
110
func EditConfigField (field , value string ) error {
68
111
config := GetConfig ()
69
112
switch field {
0 commit comments