-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpattern_update.go
More file actions
573 lines (476 loc) · 15.7 KB
/
pattern_update.go
File metadata and controls
573 lines (476 loc) · 15.7 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"time"
)
// PatternVersionInfo stores version information for pattern files
type PatternVersionInfo struct {
Version string `json:"version"`
UpdatedAt string `json:"updated_at"`
Size int `json:"size"`
}
// PatternVersionResponse represents the API response for pattern versions
type PatternVersionResponse struct {
Patterns struct {
ErrorPatterns PatternVersionInfo `json:"error_patterns"`
CommonCommands PatternVersionInfo `json:"common_commands"`
} `json:"patterns"`
}
// PatternUpdateManager handles updating pattern files from the API
type PatternUpdateManager struct {
apiBaseURL string
lastUpdateCheck time.Time
checkInterval time.Duration
patternDir string
embeddedDir string
isInitialized bool
}
// PatternUpdateConfig holds the configuration for the pattern update system
type PatternUpdateConfig struct {
Enabled bool `json:"enabled"`
AutoUpdate bool `json:"auto_update"`
APIBaseURL string `json:"api_base_url"`
UpdateCheckInterval int `json:"update_check_interval"` // In hours
LastUpdateCheck string `json:"last_update_check"`
ErrorPatternsVersion string `json:"error_patterns_version"`
CommandsVersion string `json:"commands_version"`
}
// NewPatternUpdateManager creates a new pattern update manager
func NewPatternUpdateManager() (*PatternUpdateManager, error) {
// Get pattern directory
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("failed to get home directory: %v", err)
}
patternDir := filepath.Join(homeDir, ".config", "delta", "patterns")
if err := os.MkdirAll(patternDir, 0755); err != nil {
return nil, fmt.Errorf("failed to create pattern directory: %v", err)
}
// Find embedded patterns directory
execDir, err := filepath.Abs(filepath.Dir(os.Args[0]))
embeddedDir := filepath.Join(execDir, "embedded_patterns")
if _, err := os.Stat(embeddedDir); os.IsNotExist(err) {
// Try alternative paths
alternativePaths := []string{
"/home/bleepbloop/deltacli/embedded_patterns",
"/usr/local/share/delta/embedded_patterns",
"/usr/share/delta/embedded_patterns",
}
for _, path := range alternativePaths {
if _, err := os.Stat(path); err == nil {
embeddedDir = path
break
}
}
}
return &PatternUpdateManager{
apiBaseURL: "https://api.delta-cli.org",
lastUpdateCheck: time.Time{},
checkInterval: time.Hour * 24, // Default to daily checks
patternDir: patternDir,
embeddedDir: embeddedDir,
isInitialized: false,
}, nil
}
// Initialize initializes the pattern update manager
func (pm *PatternUpdateManager) Initialize() error {
// Load configuration or create default config
config, err := pm.loadConfig()
if err != nil {
// Create default config
config = PatternUpdateConfig{
Enabled: true,
AutoUpdate: true,
APIBaseURL: "https://api.delta-cli.org",
UpdateCheckInterval: 24, // Daily check
LastUpdateCheck: time.Now().Format(time.RFC3339),
ErrorPatternsVersion: "1.0",
CommandsVersion: "1.0",
}
// Save default config
if err := pm.saveConfig(config); err != nil {
return fmt.Errorf("failed to save default config: %v", err)
}
}
// Update configuration
pm.apiBaseURL = config.APIBaseURL
pm.checkInterval = time.Duration(config.UpdateCheckInterval) * time.Hour
if t, err := time.Parse(time.RFC3339, config.LastUpdateCheck); err == nil {
pm.lastUpdateCheck = t
}
// Copy embedded patterns to user directory if they don't exist
if err := pm.initializePatternFiles(); err != nil {
return fmt.Errorf("failed to initialize pattern files: %v", err)
}
// If auto update is enabled and it's time to check, do it in the background
if config.Enabled && config.AutoUpdate && time.Since(pm.lastUpdateCheck) > pm.checkInterval {
go func() {
if updatesAvailable, _ := pm.CheckForUpdates(); updatesAvailable {
pm.DownloadUpdates()
}
}()
}
pm.isInitialized = true
return nil
}
// initializePatternFiles copies embedded patterns to user directory if needed
func (pm *PatternUpdateManager) initializePatternFiles() error {
// Check for error patterns
errorPatternsPath := filepath.Join(pm.patternDir, "error_patterns.json")
if _, err := os.Stat(errorPatternsPath); os.IsNotExist(err) {
// Copy from embedded
embeddedPath := filepath.Join(pm.embeddedDir, "error_patterns.json")
if err := pm.copyFile(embeddedPath, errorPatternsPath); err != nil {
return fmt.Errorf("failed to copy error patterns: %v", err)
}
}
// Check for common commands
commandsPath := filepath.Join(pm.patternDir, "common_commands.json")
if _, err := os.Stat(commandsPath); os.IsNotExist(err) {
// Copy from embedded
embeddedPath := filepath.Join(pm.embeddedDir, "common_commands.json")
if err := pm.copyFile(embeddedPath, commandsPath); err != nil {
return fmt.Errorf("failed to copy common commands: %v", err)
}
}
return nil
}
// copyFile copies a file from src to dst
func (pm *PatternUpdateManager) copyFile(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
return err
}
// loadConfig loads the pattern update configuration
func (pm *PatternUpdateManager) loadConfig() (PatternUpdateConfig, error) {
configPath := filepath.Join(pm.patternDir, "config.json")
data, err := os.ReadFile(configPath)
if err != nil {
return PatternUpdateConfig{}, err
}
var config PatternUpdateConfig
if err := json.Unmarshal(data, &config); err != nil {
return PatternUpdateConfig{}, err
}
return config, nil
}
// saveConfig saves the pattern update configuration
func (pm *PatternUpdateManager) saveConfig(config PatternUpdateConfig) error {
configPath := filepath.Join(pm.patternDir, "config.json")
data, err := json.MarshalIndent(config, "", " ")
if err != nil {
return err
}
return os.WriteFile(configPath, data, 0644)
}
// CheckForUpdates checks if pattern updates are available
func (pm *PatternUpdateManager) CheckForUpdates() (bool, error) {
if !pm.isInitialized {
return false, fmt.Errorf("pattern update manager not initialized")
}
// Update last check time
pm.lastUpdateCheck = time.Now()
// Update config with new check time
config, err := pm.loadConfig()
if err == nil {
config.LastUpdateCheck = pm.lastUpdateCheck.Format(time.RFC3339)
pm.saveConfig(config)
}
// Get current versions
currentVersions, err := pm.getCurrentVersions()
if err != nil {
return false, fmt.Errorf("failed to get current versions: %v", err)
}
// Get latest versions from API
latestVersions, err := pm.getLatestVersions()
if err != nil {
return false, fmt.Errorf("failed to get latest versions: %v", err)
}
// Check if updates are available
errorPatternsNeedsUpdate := latestVersions.Patterns.ErrorPatterns.Version != currentVersions.ErrorPatterns
commandsNeedsUpdate := latestVersions.Patterns.CommonCommands.Version != currentVersions.CommandsVersion
return errorPatternsNeedsUpdate || commandsNeedsUpdate, nil
}
// DownloadUpdates downloads the latest pattern files
func (pm *PatternUpdateManager) DownloadUpdates() error {
if !pm.isInitialized {
return fmt.Errorf("pattern update manager not initialized")
}
// Get latest versions from API
latestVersions, err := pm.getLatestVersions()
if err != nil {
return fmt.Errorf("failed to get latest versions: %v", err)
}
// Get current versions
currentVersions, err := pm.getCurrentVersions()
if err != nil {
return fmt.Errorf("failed to get current versions: %v", err)
}
updatedFiles := 0
// Download error patterns if needed
if latestVersions.Patterns.ErrorPatterns.Version != currentVersions.ErrorPatterns {
if err := pm.downloadFile("error_patterns.json", latestVersions.Patterns.ErrorPatterns.Version); err != nil {
return fmt.Errorf("failed to download error patterns: %v", err)
}
fmt.Println("Updated error patterns to version", latestVersions.Patterns.ErrorPatterns.Version)
updatedFiles++
// Update config with new version
config, _ := pm.loadConfig()
config.ErrorPatternsVersion = latestVersions.Patterns.ErrorPatterns.Version
pm.saveConfig(config)
}
// Download common commands if needed
if latestVersions.Patterns.CommonCommands.Version != currentVersions.CommandsVersion {
if err := pm.downloadFile("common_commands.json", latestVersions.Patterns.CommonCommands.Version); err != nil {
return fmt.Errorf("failed to download common commands: %v", err)
}
fmt.Println("Updated common commands to version", latestVersions.Patterns.CommonCommands.Version)
updatedFiles++
// Update config with new version
config, _ := pm.loadConfig()
config.CommandsVersion = latestVersions.Patterns.CommonCommands.Version
pm.saveConfig(config)
}
if updatedFiles == 0 {
fmt.Println("All pattern files are already up to date.")
}
return nil
}
// getCurrentVersions gets the current versions from the config
func (pm *PatternUpdateManager) getCurrentVersions() (struct{ ErrorPatterns, CommandsVersion string }, error) {
config, err := pm.loadConfig()
if err != nil {
return struct{ ErrorPatterns, CommandsVersion string }{}, err
}
return struct{ ErrorPatterns, CommandsVersion string }{
ErrorPatterns: config.ErrorPatternsVersion,
CommandsVersion: config.CommandsVersion,
}, nil
}
// getLatestVersions gets the latest versions from the API
func (pm *PatternUpdateManager) getLatestVersions() (*PatternVersionResponse, error) {
// Build API URL
url := fmt.Sprintf("%s/api/patterns/versions", pm.apiBaseURL)
// Send request
resp, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("failed to send request: %v", err)
}
defer resp.Body.Close()
// Check response status
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("API request failed with status: %d", resp.StatusCode)
}
// Read response body
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %v", err)
}
// Parse response
var versions PatternVersionResponse
if err := json.Unmarshal(body, &versions); err != nil {
return nil, fmt.Errorf("failed to parse response: %v", err)
}
return &versions, nil
}
// downloadFile downloads a pattern file from the API
func (pm *PatternUpdateManager) downloadFile(fileName, version string) error {
// Build API URL
url := fmt.Sprintf("%s/api/patterns/%s?version=%s", pm.apiBaseURL, fileName, version)
// Send request
resp, err := http.Get(url)
if err != nil {
return fmt.Errorf("failed to send request: %v", err)
}
defer resp.Body.Close()
// Check response status
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("API request failed with status: %d", resp.StatusCode)
}
// Read response body
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read response body: %v", err)
}
// Write to file
filePath := filepath.Join(pm.patternDir, fileName)
if err := os.WriteFile(filePath, body, 0644); err != nil {
return fmt.Errorf("failed to write file: %v", err)
}
return nil
}
// IsEnabled returns whether pattern updates are enabled
func (pm *PatternUpdateManager) IsEnabled() bool {
if !pm.isInitialized {
return false
}
config, err := pm.loadConfig()
if err != nil {
return false
}
return config.Enabled
}
// Enable enables pattern updates
func (pm *PatternUpdateManager) Enable() error {
if !pm.isInitialized {
return fmt.Errorf("pattern update manager not initialized")
}
config, err := pm.loadConfig()
if err != nil {
return err
}
config.Enabled = true
return pm.saveConfig(config)
}
// Disable disables pattern updates
func (pm *PatternUpdateManager) Disable() error {
if !pm.isInitialized {
return fmt.Errorf("pattern update manager not initialized")
}
config, err := pm.loadConfig()
if err != nil {
return err
}
config.Enabled = false
return pm.saveConfig(config)
}
// EnableAutoUpdate enables automatic updates
func (pm *PatternUpdateManager) EnableAutoUpdate() error {
if !pm.isInitialized {
return fmt.Errorf("pattern update manager not initialized")
}
config, err := pm.loadConfig()
if err != nil {
return err
}
config.AutoUpdate = true
return pm.saveConfig(config)
}
// DisableAutoUpdate disables automatic updates
func (pm *PatternUpdateManager) DisableAutoUpdate() error {
if !pm.isInitialized {
return fmt.Errorf("pattern update manager not initialized")
}
config, err := pm.loadConfig()
if err != nil {
return err
}
config.AutoUpdate = false
return pm.saveConfig(config)
}
// UpdateCheckInterval updates the check interval
func (pm *PatternUpdateManager) UpdateCheckInterval(hours int) error {
if !pm.isInitialized {
return fmt.Errorf("pattern update manager not initialized")
}
if hours <= 0 {
return fmt.Errorf("check interval must be positive")
}
config, err := pm.loadConfig()
if err != nil {
return err
}
config.UpdateCheckInterval = hours
pm.checkInterval = time.Duration(hours) * time.Hour
return pm.saveConfig(config)
}
// GetStats returns statistics about pattern files
func (pm *PatternUpdateManager) GetStats() (map[string]interface{}, error) {
if !pm.isInitialized {
return nil, fmt.Errorf("pattern update manager not initialized")
}
stats := make(map[string]interface{})
// Get current versions
config, err := pm.loadConfig()
if err != nil {
return nil, err
}
stats["enabled"] = config.Enabled
stats["auto_update"] = config.AutoUpdate
stats["api_base_url"] = config.APIBaseURL
stats["update_check_interval"] = config.UpdateCheckInterval
stats["last_update_check"] = config.LastUpdateCheck
stats["error_patterns_version"] = config.ErrorPatternsVersion
stats["commands_version"] = config.CommandsVersion
// Check if files exist
errorPatternsPath := filepath.Join(pm.patternDir, "error_patterns.json")
errorPatternsExists := fileExists(errorPatternsPath)
stats["error_patterns_exists"] = errorPatternsExists
commandsPath := filepath.Join(pm.patternDir, "common_commands.json")
commandsExists := fileExists(commandsPath)
stats["commands_exists"] = commandsExists
// Get file sizes
if errorPatternsExists {
if info, err := os.Stat(errorPatternsPath); err == nil {
stats["error_patterns_size"] = info.Size()
}
}
if commandsExists {
if info, err := os.Stat(commandsPath); err == nil {
stats["commands_size"] = info.Size()
}
}
// Get pattern counts
if errorPatternsExists {
data, err := os.ReadFile(errorPatternsPath)
if err == nil {
var patterns struct {
Patterns []interface{} `json:"patterns"`
}
if err := json.Unmarshal(data, &patterns); err == nil {
stats["error_patterns_count"] = len(patterns.Patterns)
}
}
}
if commandsExists {
data, err := os.ReadFile(commandsPath)
if err == nil {
var patterns struct {
Commands []interface{} `json:"commands"`
}
if err := json.Unmarshal(data, &patterns); err == nil {
stats["commands_count"] = len(patterns.Commands)
}
}
}
return stats, nil
}
// Global instance of the pattern update manager
var globalPatternUpdateManager *PatternUpdateManager
// GetPatternUpdateManager returns the global pattern update manager
func GetPatternUpdateManager() *PatternUpdateManager {
if globalPatternUpdateManager == nil {
var err error
globalPatternUpdateManager, err = NewPatternUpdateManager()
if err != nil {
fmt.Printf("Error creating pattern update manager: %v\n", err)
return nil
}
// Initialize in the background to avoid blocking
go func() {
if err := globalPatternUpdateManager.Initialize(); err != nil {
fmt.Printf("Error initializing pattern update manager: %v\n", err)
}
}()
}
return globalPatternUpdateManager
}
// patternFileExists checks if a pattern file exists
func patternFileExists(path string) bool {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}