-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
284 lines (225 loc) · 6.44 KB
/
config.go
File metadata and controls
284 lines (225 loc) · 6.44 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"gopkg.in/yaml.v3"
)
type Config struct {
QtCreatorPath string `yaml:"qtcreator_path"`
PluginPath string `yaml:"plugin_path"`
mu sync.RWMutex
qtcVersion *Version
versionFetched bool
}
func LoadConfig(path string) (*Config, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read config file: %w", err)
}
var config Config
if err := yaml.Unmarshal(data, &config); err != nil {
return nil, fmt.Errorf("failed to parse config file: %w", err)
}
if err := config.Validate(); err != nil {
return nil, fmt.Errorf("invalid configuration: %w", err)
}
return &config, nil
}
func findQtCreator() (string, error) {
platformConfig, err := GetPlatformConfig()
if err != nil {
return "", err
}
for _, path := range platformConfig.QtCreatorPaths {
if info, err := os.Stat(path); err == nil && info.IsDir() {
if _, err := GetQtCreatorVersion(path); err == nil {
return path, nil
}
}
}
return "", fmt.Errorf("Qt Creator not found in any default location")
}
func findPlugin(qtCreatorPath string) (string, error) {
version, err := GetQtCreatorVersion(qtCreatorPath)
if err != nil {
return "", fmt.Errorf("failed to get Qt Creator version: %w", err)
}
searchDirs, err := GetPluginSearchDirs(qtCreatorPath, version)
if err != nil {
return "", err
}
platformConfig, err := GetPlatformConfig()
if err != nil {
return "", err
}
versionStr := version.String()
targetName := platformConfig.PluginFileName
for _, searchDir := range searchDirs {
if !PathExists(searchDir) {
continue
}
foundPath, err := FindFileInDirectory(searchDir, targetName, MaxSearchDepth, nil)
if err == nil && foundPath != "" {
configPath := foundPath
if strings.HasSuffix(foundPath, "Qt Creator.app") {
configPath = filepath.Dir(foundPath)
}
configPath = strings.Replace(configPath, versionStr, "{qtc_version}", 1)
return filepath.ToSlash(configPath), nil
}
}
if len(platformConfig.PluginPaths) > 0 {
defaultPath := platformConfig.PluginPaths[0]
return defaultPath, nil
}
return "", fmt.Errorf("no default plugin path available for this system")
}
func CreateDefaultConfig(path string) error {
PrintStep("Creating configuration...")
var qtCreatorPath, pluginPath string
var foundQtCreator, foundPlugin bool
qtcPath, err := findQtCreator()
if err != nil {
foundQtCreator = false
platformConfig, cfgErr := GetPlatformConfig()
if cfgErr != nil {
return fmt.Errorf("failed to get platform config: %w", cfgErr)
}
if len(platformConfig.QtCreatorPaths) > 0 {
qtCreatorPath = platformConfig.QtCreatorPaths[0]
}
} else {
qtCreatorPath = qtcPath
foundQtCreator = true
plugPath, err := findPlugin(qtCreatorPath)
if err != nil {
foundPlugin = false
platformConfig, cfgErr := GetPlatformConfig()
if cfgErr != nil {
return fmt.Errorf("failed to get platform config: %w", cfgErr)
}
if len(platformConfig.PluginPaths) > 0 {
pluginPath = platformConfig.PluginPaths[0]
}
} else {
pluginPath = plugPath
foundPlugin = true
}
}
qtCreatorPath = filepath.ToSlash(qtCreatorPath)
pluginPath = filepath.ToSlash(pluginPath)
configContent := fmt.Sprintf(`qtcreator_path: "%s"
plugin_path: "%s"
`, qtCreatorPath, pluginPath)
if err := os.WriteFile(path, []byte(configContent), DefaultFilePermissions); err != nil {
return fmt.Errorf("failed to write config file: %w", err)
}
if !foundQtCreator || !foundPlugin {
PrintWarning("Could not auto-detect all paths")
if !foundQtCreator {
PrintError("Qt Creator not found in default locations")
fmt.Println()
}
if !foundPlugin {
PrintWarning("Using default plugin path")
fmt.Println()
}
platformConfig, _ := GetPlatformConfig()
if platformConfig != nil {
PrintVerbose(Gray("Please verify the configuration file and update paths if needed:"))
PrintVerbose(Gray(fmt.Sprintf(" Config: %s", path)))
fmt.Println()
PrintVerbose(Gray("Default Qt Creator paths:"))
for _, p := range platformConfig.QtCreatorPaths {
PrintVerbose(Gray(fmt.Sprintf(" • %s", p)))
}
}
return fmt.Errorf("configuration incomplete - please edit the config file with correct paths")
}
PrintSuccess("Configuration created successfully")
PrintField("Config file", path)
fmt.Println()
return nil
}
func (c *Config) expandVariables(path string) (string, error) {
c.mu.RLock()
qtcVersion := c.qtcVersion
c.mu.RUnlock()
if qtcVersion == nil {
return path, nil
}
path = strings.ReplaceAll(path, "{qtc_version}", qtcVersion.String())
return path, nil
}
func (c *Config) GetQtCreatorVersion() (*Version, error) {
c.mu.RLock()
if c.versionFetched {
version := c.qtcVersion
c.mu.RUnlock()
return version, nil
}
c.mu.RUnlock()
c.mu.Lock()
defer c.mu.Unlock()
if c.versionFetched {
return c.qtcVersion, nil
}
version, err := GetQtCreatorVersion(c.QtCreatorPath)
if err != nil {
return nil, err
}
c.qtcVersion = version
c.versionFetched = true
return version, nil
}
func (c *Config) GetPluginPath() (string, error) {
if !c.versionFetched {
if _, err := c.GetQtCreatorVersion(); err != nil {
return "", fmt.Errorf("failed to get Qt Creator version: %w", err)
}
}
return c.expandVariables(c.PluginPath)
}
func (c *Config) validateQtCreatorPath() error {
if c.QtCreatorPath == "" {
return fmt.Errorf("qtcreator_path is required")
}
expandedPath, err := ExpandPath(c.QtCreatorPath)
if err != nil {
return fmt.Errorf("failed to expand qtcreator_path: %w", err)
}
c.QtCreatorPath = expandedPath
qtcInfo, err := os.Stat(c.QtCreatorPath)
if err != nil {
return fmt.Errorf("qtcreator_path does not exist or is not accessible: %w", err)
}
if !qtcInfo.IsDir() {
return fmt.Errorf("qtcreator_path should be a directory")
}
return nil
}
func (c *Config) validatePluginPath() error {
if c.PluginPath == "" {
return fmt.Errorf("plugin_path is required")
}
expandedPath, err := ExpandPath(c.PluginPath)
if err != nil {
return fmt.Errorf("failed to expand plugin_path: %w", err)
}
if filepath.IsAbs(expandedPath) || strings.Contains(expandedPath, "{qtc_version}") || strings.HasPrefix(c.PluginPath, "~") {
return nil
}
return fmt.Errorf("plugin_path should be an absolute path or contain {qtc_version} variable")
}
func (c *Config) Validate() error {
if err := c.validateQtCreatorPath(); err != nil {
return err
}
if err := c.validatePluginPath(); err != nil {
return err
}
return nil
}