-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi18n_github_loader.go
More file actions
525 lines (436 loc) · 13 KB
/
i18n_github_loader.go
File metadata and controls
525 lines (436 loc) · 13 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
package main
import (
"archive/tar"
"bufio"
"compress/gzip"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"time"
)
// I18nGitHubLoader handles downloading i18n files from GitHub releases
type I18nGitHubLoader struct {
httpClient *http.Client
deltaAPIURL string
cacheDir string
tempDir string
}
// I18nReleaseInfo contains information about a release with i18n files
type I18nReleaseInfo struct {
Version string
I18nAssetURL string
ChecksumURL string
AssetName string
AssetSize int64
ExpectedSHA256 string
}
// I18nDownloadResult contains the result of an i18n download operation
type I18nDownloadResult struct {
DownloadedLocales []string
TotalFiles int
DownloadTime time.Duration
InstallPath string
Error error
}
// NewI18nGitHubLoader creates a new GitHub-based i18n loader
func NewI18nGitHubLoader() *I18nGitHubLoader {
// Get cache directory
cacheDir, _ := os.UserCacheDir()
if cacheDir == "" {
cacheDir = "/tmp"
}
cacheDir = filepath.Join(cacheDir, "delta", "i18n")
// Get temp directory
tempDir := filepath.Join(os.TempDir(), "delta-i18n-github")
return &I18nGitHubLoader{
httpClient: &http.Client{
Timeout: 30 * time.Second,
},
deltaAPIURL: "https://deltacli.com/api/github/latest-version",
cacheDir: cacheDir,
tempDir: tempDir,
}
}
// GetLatestI18nRelease fetches information about the latest release with i18n files
func (gl *I18nGitHubLoader) GetLatestI18nRelease() (*I18nReleaseInfo, error) {
// Get latest version from deltacli.com API
version, err := gl.getLatestVersionFromDeltaAPI()
if err != nil {
return nil, fmt.Errorf("failed to get latest version: %v", err)
}
// Construct the release info based on the version
// GitHub releases follow a predictable pattern
assetName := fmt.Sprintf("delta-i18n-%s.tar.gz", version)
return &I18nReleaseInfo{
Version: version,
I18nAssetURL: fmt.Sprintf("https://github.com/deltacli/delta/releases/download/%s/%s", version, assetName),
ChecksumURL: fmt.Sprintf("https://github.com/deltacli/delta/releases/download/%s/checksums.sha256", version),
AssetName: assetName,
AssetSize: 0, // We don't know the size without hitting GitHub API
}, nil
}
// getLatestVersionFromDeltaAPI uses deltacli.com API to get latest version
func (gl *I18nGitHubLoader) getLatestVersionFromDeltaAPI() (string, error) {
resp, err := gl.httpClient.Get(gl.deltaAPIURL)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("deltacli.com API returned status %d", resp.StatusCode)
}
var result struct {
Version string `json:"version"`
URL string `json:"url"`
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return "", err
}
return result.Version, nil
}
// DownloadI18nFilesFromRelease downloads i18n files from a specific release
func (gl *I18nGitHubLoader) DownloadI18nFilesFromRelease() (*I18nDownloadResult, error) {
startTime := time.Now()
result := &I18nDownloadResult{}
// Get latest release info
releaseInfo, err := gl.GetLatestI18nRelease()
if err != nil {
result.Error = fmt.Errorf("failed to get release info: %v", err)
return result, result.Error
}
fmt.Printf("Found i18n files for Delta %s\n", releaseInfo.Version)
// Clean up temp directory if it exists
if err := os.RemoveAll(gl.tempDir); err != nil {
// Non-fatal, continue
}
// Create temp directory
if err := os.MkdirAll(gl.tempDir, 0755); err != nil {
result.Error = fmt.Errorf("failed to create temp directory: %v", err)
return result, result.Error
}
defer os.RemoveAll(gl.tempDir) // Clean up when done
// Download checksums if available
if releaseInfo.ChecksumURL != "" {
fmt.Println("Downloading checksums for verification...")
expectedSHA, err := gl.downloadAndParseChecksums(releaseInfo)
if err != nil {
fmt.Printf("Warning: Could not download checksums: %v\n", err)
fmt.Println("Proceeding without checksum verification...")
} else {
releaseInfo.ExpectedSHA256 = expectedSHA
}
}
// Download the i18n archive
archivePath, err := gl.downloadI18nArchive(releaseInfo)
if err != nil {
result.Error = fmt.Errorf("failed to download i18n archive: %v", err)
return result, result.Error
}
// Verify checksum if available
if releaseInfo.ExpectedSHA256 != "" {
fmt.Println("Verifying file integrity...")
if err := gl.verifyChecksum(archivePath, releaseInfo.ExpectedSHA256); err != nil {
result.Error = fmt.Errorf("checksum verification failed: %v", err)
return result, result.Error
}
fmt.Println("✓ Checksum verified successfully")
}
// Extract i18n files
extractedPath, err := gl.extractI18nArchive(archivePath)
if err != nil {
result.Error = fmt.Errorf("failed to extract i18n files: %v", err)
return result, result.Error
}
// Get install path
homeDir, err := os.UserHomeDir()
if err != nil {
result.Error = fmt.Errorf("failed to get home directory: %v", err)
return result, result.Error
}
installPath := filepath.Join(homeDir, ".config", "delta", "i18n", "locales")
// Install the files
locales, fileCount, err := gl.installExtractedFiles(extractedPath, installPath)
if err != nil {
result.Error = fmt.Errorf("failed to install i18n files: %v", err)
return result, result.Error
}
result.DownloadedLocales = locales
result.TotalFiles = fileCount
result.DownloadTime = time.Since(startTime)
result.InstallPath = installPath
return result, nil
}
// downloadI18nArchive downloads the i18n archive from the release
func (gl *I18nGitHubLoader) downloadI18nArchive(releaseInfo *I18nReleaseInfo) (string, error) {
req, err := http.NewRequest("GET", releaseInfo.I18nAssetURL, nil)
if err != nil {
return "", err
}
// Set user agent
req.Header.Set("User-Agent", "Delta-CLI/"+GetVersionShort())
// Download the file
resp, err := gl.httpClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("download returned status %d", resp.StatusCode)
}
// Create temp file for archive
archivePath := filepath.Join(gl.tempDir, releaseInfo.AssetName)
out, err := os.Create(archivePath)
if err != nil {
return "", err
}
defer out.Close()
// Copy with progress reporting
written, err := io.Copy(out, resp.Body)
if err != nil {
return "", err
}
fmt.Printf("Downloaded %d bytes\n", written)
return archivePath, nil
}
// extractI18nArchive extracts the i18n archive
func (gl *I18nGitHubLoader) extractI18nArchive(archivePath string) (string, error) {
// Open the archive
file, err := os.Open(archivePath)
if err != nil {
return "", err
}
defer file.Close()
// Create gzip reader
gzr, err := gzip.NewReader(file)
if err != nil {
return "", err
}
defer gzr.Close()
// Create tar reader
tr := tar.NewReader(gzr)
// Extract directory path
extractPath := filepath.Join(gl.tempDir, "extracted")
if err := os.MkdirAll(extractPath, 0755); err != nil {
return "", err
}
// Extract files
for {
header, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return "", err
}
// Skip if not a regular file
if header.Typeflag != tar.TypeReg {
continue
}
// Create the full path
fullPath := filepath.Join(extractPath, header.Name)
// Create directory if needed
dir := filepath.Dir(fullPath)
if err := os.MkdirAll(dir, 0755); err != nil {
return "", err
}
// Create the file
outFile, err := os.Create(fullPath)
if err != nil {
return "", err
}
// Copy file contents
if _, err := io.Copy(outFile, tr); err != nil {
outFile.Close()
return "", err
}
outFile.Close()
// Set file permissions
if err := os.Chmod(fullPath, 0644); err != nil {
// Non-fatal, continue
}
}
return extractPath, nil
}
// installExtractedFiles installs extracted files to the target directory
func (gl *I18nGitHubLoader) installExtractedFiles(sourcePath, targetPath string) ([]string, int, error) {
// Look for the locales directory in the extracted files
localesPath := filepath.Join(sourcePath, "locales")
if _, err := os.Stat(localesPath); os.IsNotExist(err) {
// Try without locales prefix
localesPath = sourcePath
}
// Create target directory
if err := os.MkdirAll(targetPath, 0755); err != nil {
return nil, 0, err
}
locales := []string{}
fileCount := 0
// Walk through the extracted files
err := filepath.Walk(localesPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Skip directories
if info.IsDir() {
return nil
}
// Get relative path from source
relPath, err := filepath.Rel(localesPath, path)
if err != nil {
return err
}
// Create target path
targetFile := filepath.Join(targetPath, relPath)
// Create target directory if needed
targetDir := filepath.Dir(targetFile)
if err := os.MkdirAll(targetDir, 0755); err != nil {
return err
}
// Copy the file
if err := gl.copyFile(path, targetFile); err != nil {
return err
}
fileCount++
// Track locale
parts := strings.Split(relPath, string(os.PathSeparator))
if len(parts) > 0 && !i18nContains(locales, parts[0]) {
locales = append(locales, parts[0])
}
return nil
})
return locales, fileCount, err
}
// copyFile copies a file from source to destination
func (gl *I18nGitHubLoader) copyFile(src, dst string) error {
sourceFile, err := os.Open(src)
if err != nil {
return err
}
defer sourceFile.Close()
destFile, err := os.Create(dst)
if err != nil {
return err
}
defer destFile.Close()
_, err = io.Copy(destFile, sourceFile)
return err
}
// LoadSingleLocaleFromGitHub loads a specific locale from GitHub
func (gl *I18nGitHubLoader) LoadSingleLocaleFromGitHub(locale string) (map[string]interface{}, error) {
// Download all i18n files
result, err := gl.DownloadI18nFilesFromRelease()
if err != nil {
return nil, err
}
// Check if the requested locale was downloaded
if !i18nContains(result.DownloadedLocales, locale) {
return nil, fmt.Errorf("locale %s not found in release", locale)
}
// Load the translation files for the locale
localePath := filepath.Join(result.InstallPath, locale)
translations := make(map[string]interface{})
// Read all JSON files in the locale directory
files, err := ioutil.ReadDir(localePath)
if err != nil {
return nil, fmt.Errorf("failed to read locale directory: %v", err)
}
for _, file := range files {
if !strings.HasSuffix(file.Name(), ".json") {
continue
}
filePath := filepath.Join(localePath, file.Name())
content, err := ioutil.ReadFile(filePath)
if err != nil {
continue
}
var data map[string]interface{}
if err := json.Unmarshal(content, &data); err != nil {
continue
}
// Add to translations
fileKey := strings.TrimSuffix(file.Name(), ".json")
translations[fileKey] = data
}
return translations, nil
}
// downloadAndParseChecksums downloads and parses the checksums file
func (gl *I18nGitHubLoader) downloadAndParseChecksums(releaseInfo *I18nReleaseInfo) (string, error) {
req, err := http.NewRequest("GET", releaseInfo.ChecksumURL, nil)
if err != nil {
return "", err
}
req.Header.Set("User-Agent", "Delta-CLI/"+GetVersionShort())
resp, err := gl.httpClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("failed to download checksums: status %d", resp.StatusCode)
}
// Parse checksums file
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
line := scanner.Text()
parts := strings.Fields(line)
if len(parts) == 2 {
checksum, filename := parts[0], parts[1]
// Look for our i18n file
if filename == releaseInfo.AssetName ||
filename == "./"+releaseInfo.AssetName ||
strings.HasSuffix(filename, "/"+releaseInfo.AssetName) {
return checksum, nil
}
}
}
if err := scanner.Err(); err != nil {
return "", err
}
return "", fmt.Errorf("checksum not found for %s", releaseInfo.AssetName)
}
// verifyChecksum verifies the SHA256 checksum of a file
func (gl *I18nGitHubLoader) verifyChecksum(filepath string, expectedSHA256 string) error {
file, err := os.Open(filepath)
if err != nil {
return err
}
defer file.Close()
hasher := sha256.New()
if _, err := io.Copy(hasher, file); err != nil {
return err
}
actualSHA256 := hex.EncodeToString(hasher.Sum(nil))
if actualSHA256 != expectedSHA256 {
return fmt.Errorf("checksum mismatch: expected %s, got %s", expectedSHA256, actualSHA256)
}
return nil
}
// calculateSHA256 calculates the SHA256 checksum of a file
func (gl *I18nGitHubLoader) calculateSHA256(filepath string) (string, error) {
file, err := os.Open(filepath)
if err != nil {
return "", err
}
defer file.Close()
hasher := sha256.New()
if _, err := io.Copy(hasher, file); err != nil {
return "", err
}
return hex.EncodeToString(hasher.Sum(nil)), nil
}
// i18nContains checks if a string slice contains a value
func i18nContains(slice []string, value string) bool {
for _, item := range slice {
if item == value {
return true
}
}
return false
}