-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
user.go
102 lines (82 loc) · 2.72 KB
/
user.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package config
import (
"os"
"path/filepath"
"gopkg.in/ini.v1"
)
type LaunchSyncConfig struct{}
type PlayLogConfig struct {
SaveEvery int `ini:"save_every,omitempty"`
OnCoreStart string `ini:"on_core_start,omitempty"`
OnCoreStop string `ini:"on_core_stop,omitempty"`
OnGameStart string `ini:"on_game_start,omitempty"`
OnGameStop string `ini:"on_game_stop,omitempty"`
}
type RandomConfig struct{}
type SearchConfig struct {
Filter []string `ini:"filter,omitempty" delim:","`
Sort string `ini:"sort,omitempty"`
}
type LastPlayedConfig struct {
Name string `ini:"name,omitempty"`
LastPlayedName string `ini:"last_played_name,omitempty"`
DisableLastPlayed bool `ini:"disable_last_played,omitempty"`
RecentFolderName string `ini:"recent_folder_name,omitempty"`
DisableRecentFolder bool `ini:"disable_recent_folder,omitempty"`
}
type RemoteConfig struct {
MdnsService bool `ini:"mdns_service,omitempty"`
SyncSSHKeys bool `ini:"sync_ssh_keys,omitempty"`
CustomLogo string `ini:"custom_logo,omitempty"`
AnnounceGameUrl string `ini:"announce_game_url,omitempty"`
}
type NfcConfig struct {
ConnectionString string `ini:"connection_string,omitempty"`
AllowCommands bool `ini:"allow_commands,omitempty"`
DisableSounds bool `ini:"disable_sounds,omitempty"`
ProbeDevice bool `ini:"probe_device,omitempty"`
}
type SystemsConfig struct {
GamesFolder []string `ini:"games_folder,omitempty,allowshadow"`
SetCore []string `ini:"set_core,omitempty,allowshadow"`
}
type UserConfig struct {
AppPath string
IniPath string
LaunchSync LaunchSyncConfig `ini:"launchsync,omitempty"`
PlayLog PlayLogConfig `ini:"playlog,omitempty"`
Random RandomConfig `ini:"random,omitempty"`
Search SearchConfig `ini:"search,omitempty"`
LastPlayed LastPlayedConfig `ini:"lastplayed,omitempty"`
Remote RemoteConfig `ini:"remote,omitempty"`
Nfc NfcConfig `ini:"nfc,omitempty"`
Systems SystemsConfig `ini:"systems,omitempty"`
}
func LoadUserConfig(name string, defaultConfig *UserConfig) (*UserConfig, error) {
iniPath := os.Getenv(UserConfigEnv)
exePath, err := os.Executable()
if err != nil {
return defaultConfig, err
}
appPath := os.Getenv(UserAppPathEnv)
if appPath != "" {
exePath = appPath
}
if iniPath == "" {
iniPath = filepath.Join(filepath.Dir(exePath), name+".ini")
}
defaultConfig.AppPath = exePath
defaultConfig.IniPath = iniPath
if _, err := os.Stat(iniPath); os.IsNotExist(err) {
return defaultConfig, nil
}
cfg, err := ini.ShadowLoad(iniPath)
if err != nil {
return defaultConfig, err
}
err = cfg.StrictMapTo(defaultConfig)
if err != nil {
return defaultConfig, err
}
return defaultConfig, nil
}