-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
55 lines (46 loc) · 1.31 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package config
import (
"encoding/json"
"os"
"path/filepath"
)
// Please don't the config values from inside the package.
// It's supposed to be used by the main package.
type Config struct {
LastUsedProfile string `json:"last_used_profile"`
CachedBepInExVersion string `json:"cached_bepinex_version"`
OtherProfilesCloned bool `json:"other_profiles_cloned"`
}
const ConfigFileName = "config.json"
// LoadConfig reads the configuration from the file.
func LoadConfig(configPath string) (*Config, error) {
file, err := os.ReadFile(configPath)
if err != nil {
return nil, err
}
var config Config
err = json.Unmarshal(file, &config)
if err != nil {
return nil, err
}
return &config, nil
}
// SaveConfig writes the configuration to the file.
func SaveConfig(configPath string, config *Config) error {
configFile, err := json.MarshalIndent(config, "", " ")
if err != nil {
return err
}
return os.WriteFile(configPath, configFile, 0644)
}
// InitializeConfig creates a new configuration file with default settings.
func InitializeConfig(basePath string) error {
configPath := filepath.Join(basePath, ConfigFileName)
if _, err := os.Stat(configPath); os.IsNotExist(err) {
defaultConfig := Config{
LastUsedProfile: "Default",
}
return SaveConfig(configPath, &defaultConfig)
}
return nil
}