-
Notifications
You must be signed in to change notification settings - Fork 6
/
configurator.go
227 lines (181 loc) · 5.28 KB
/
configurator.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package configurator
import (
"io/ioutil"
"os"
"path/filepath"
"github.com/ghodss/yaml"
"github.com/jhekasoft/insteadman/core/utils"
)
type InsteadmanConfig struct {
Repositories []Repository `json:"repositories"`
InterpreterCommand string `json:"interpreter_command"`
Version string `json:"version"`
UseBuiltinInterpreter bool `json:"use_builtin_interpreter"`
Lang string `json:"lang"`
CheckUpdateOnStart bool `json:"check_update_on_start"`
GamesPath string `json:"games_path"`
InsteadManPath string `json:"insteadman_path"`
Gtk Gtk `json:"gtk"`
CalculatedGamesPath string `json:"-"`
CalculatedInsteadManPath string `json:"-"`
}
func ExpandInterpreterCommand(command string) string {
if command == "" {
return ""
}
path, e := filepath.Abs(command)
if e != nil {
return command
}
return path
}
type Repository struct {
Name string `json:"name"`
Url string `json:"url"`
}
type Gtk struct {
HideSidebar bool `json:"hide_sidebar"`
MainWidth int `json:"main_width"`
MainHeight int `json:"main_height"`
}
const (
configName = "config.yml"
skeletonDir = "skeleton"
gamesDirName = "games"
insteadManDirName = "insteadman"
localeDir = "locale"
)
type Configurator struct {
FilePath string
CurrentDir string
DataPath string
LocalePath string
Version string
}
func (c *Configurator) insteadManDir() string {
localPath := filepath.Join(c.CurrentDir, configName)
_, e := os.Stat(localPath)
exists := !os.IsNotExist(e)
if exists && e == nil {
return c.CurrentDir
}
insteadManDir := filepath.Join(insteadDir(), insteadManDirName)
os.MkdirAll(insteadManDir, os.ModePerm)
return insteadManDir
}
func (c *Configurator) findConfigFileName() string {
return filepath.Join(c.insteadManDir(), configName)
}
func (c *Configurator) gamesDir() string {
localPath := filepath.Join(c.CurrentDir, gamesDirName)
_, e := os.Stat(localPath)
exists := !os.IsNotExist(e)
if exists && e == nil {
return localPath
}
gamesDir := filepath.Join(insteadDir(), gamesDirName)
os.MkdirAll(gamesDir, os.ModePerm)
return gamesDir
}
func (c *Configurator) sceletonConfigPath() string {
return c.DataResourcePath(filepath.Join(skeletonDir, configName))
}
func (c *Configurator) DataResourcePath(relPath string) string {
var dataPathList []string
if c.DataPath != "" {
dataPathList = append(dataPathList, c.DataPath)
} else { // default path if empty
// Add current dir to search
dataPathList = append(dataPathList, c.CurrentDir)
// Add UNIX-path to search
const unixDataPath = "share/insteadman"
unixDataDir, e := filepath.Abs(filepath.Join(c.CurrentDir, "..", unixDataPath))
if e == nil {
dataPathList = append(dataPathList, unixDataDir)
}
}
// Search resource in all the path
for _, sharePath := range dataPathList {
absPath := filepath.Join(sharePath, relPath)
_, e := os.Stat(absPath)
exists := !os.IsNotExist(e)
if exists && e == nil {
return absPath
}
}
// If resource hasn't found then return relative path of resource
return relPath
}
func (c *Configurator) DataLocalePath() string {
resourcesLocaleDir, e := filepath.Abs(filepath.Join(c.CurrentDir, "resources", localeDir))
if e == nil && utils.PathExist(resourcesLocaleDir) {
return resourcesLocaleDir
}
if c.LocalePath != "" {
return c.LocalePath
}
unixLocaleDir, _ := filepath.Abs(filepath.Join(c.CurrentDir, "..", "share", "locale"))
return unixLocaleDir
}
func (c *Configurator) GetSkeletonConfig() (config *InsteadmanConfig, e error) {
configData, e := ioutil.ReadFile(c.sceletonConfigPath())
if e != nil {
return
}
e = yaml.Unmarshal(configData, &config)
return
}
func (c *Configurator) writeSkeleton() error {
configData, e := ioutil.ReadFile(c.sceletonConfigPath())
if e != nil {
return e
}
return ioutil.WriteFile(c.FilePath, configData, 0644)
}
func (c *Configurator) GetConfig() (*InsteadmanConfig, error) {
if c.FilePath == "" {
c.FilePath = c.findConfigFileName()
}
// Write skeleton config if it isn't existing
_, e := os.Stat(c.FilePath)
exists := !os.IsNotExist(e)
if !exists || e != nil {
e = c.writeSkeleton()
if e != nil {
return nil, e
}
}
file, e := ioutil.ReadFile(c.FilePath)
if e != nil {
return nil, e
}
// fmt.Printf("%s\n", string(file))
var config *InsteadmanConfig
yaml.Unmarshal(file, &config)
// Fix default language for old config
// Default language was "ru", set it to empty value (system language)
if config.Version == "3.0.0" {
config.Lang = ""
}
// TODO: make Calculated* fields like GetInterpreterCommand() func, but like "lazy vars"
config.CalculatedGamesPath = config.GamesPath
if config.CalculatedGamesPath == "" {
config.CalculatedGamesPath = c.gamesDir()
}
config.CalculatedInsteadManPath = config.InsteadManPath
if config.CalculatedInsteadManPath == "" {
config.CalculatedInsteadManPath = c.insteadManDir()
}
return config, nil
}
func (c *Configurator) SaveConfig(config *InsteadmanConfig) error {
// Write InsteadMan version to config
if c.Version != "" {
config.Version = c.Version
}
bytes, e := yaml.Marshal(config)
if e != nil {
return e
}
return ioutil.WriteFile(c.FilePath, bytes, 0644)
}