-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmanager.go
More file actions
177 lines (146 loc) · 4.89 KB
/
manager.go
File metadata and controls
177 lines (146 loc) · 4.89 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
package main
import (
"fmt"
"os"
"path/filepath"
"regexp"
)
func removeExisting(path string) error {
if _, err := os.Lstat(path); err != nil {
if os.IsNotExist(err) {
return nil
}
return fmt.Errorf("failed to check if path exists: %w", err)
}
if err := os.RemoveAll(path); err != nil {
return fmt.Errorf("failed to remove existing path: %w", err)
}
fmt.Printf("Removed existing: %s\n", path)
return nil
}
var versionMatcher = regexp.MustCompile(`(?i)\$version`)
func replaceVersionPlaceholders(url, version string) string {
return versionMatcher.ReplaceAllString(url, version)
}
func ProcessFetchItem(config *Config, item FetchItem) error {
finalURL := replaceVersionPlaceholders(item.URL, item.Version)
fmt.Printf("Downloading: %s\n", finalURL)
downloadResult, err := DownloadFile(finalURL)
if err != nil {
return fmt.Errorf("download failed: %w", err)
}
fmt.Printf("Verifying hash...\n")
if item.Hash != "" {
if err := VerifyHash(downloadResult.Data, item.Hash); err != nil {
return fmt.Errorf("hash verification failed: %w", err)
}
} else if len(item.Hashes) > 0 {
if err := VerifyHashes(downloadResult.Data, item.Hashes); err != nil {
return fmt.Errorf("hash verification failed: %w", err)
}
} else {
return fmt.Errorf("no hash or hashes specified for verification")
}
var filesToWrite []FileToWrite
if item.Extract {
fmt.Printf("Extracting archive...\n")
extractResult, err := ExtractArchive(downloadResult.Data, downloadResult.Filename)
if err != nil {
return fmt.Errorf("extraction failed: %w", err)
}
for _, extractedFile := range extractResult.Files {
// Create files under a directory named after the fetch item
filePath := filepath.Join(item.Name, extractedFile.Name)
filesToWrite = append(filesToWrite, FileToWrite{
Name: filePath,
Data: extractedFile.Data,
})
}
} else {
filesToWrite = append(filesToWrite, FileToWrite{
Name: item.Name,
Data: downloadResult.Data,
})
}
outputDir := item.GetOutputDir(config.OutputDir)
if outputDir != "" {
if err := writeFiles(filesToWrite, outputDir, item.Extract, item.Name); err != nil {
return fmt.Errorf("failed to write files: %w", err)
}
}
binFile, shouldCreateSymlink := item.GetBinFileString()
if shouldCreateSymlink {
binDir := item.GetBinDir(config.BinsDir)
if binDir == "" {
return fmt.Errorf("bins-dir not specified for binary symlink")
}
var targetPath string
if item.Extract {
targetPath = filepath.Join(outputDir, item.Name, binFile)
} else {
if outputDir == "" {
return fmt.Errorf("output-dir required when creating symlink for non-extracted file")
}
targetPath = filepath.Join(outputDir, item.Name)
}
symlinkName := filepath.Base(binFile)
if err := createSymlink(targetPath, binDir, symlinkName); err != nil {
return fmt.Errorf("failed to create symlink: %w", err)
}
}
return nil
}
type FileToWrite struct {
Name string
Data []byte
}
func writeFiles(files []FileToWrite, outputDir string, isExtract bool, itemName string) error {
if err := os.MkdirAll(outputDir, 0755); err != nil {
return fmt.Errorf("failed to create output directory: %w", err)
}
if isExtract {
extractDir := filepath.Join(outputDir, itemName)
if err := removeExisting(extractDir); err != nil {
return fmt.Errorf("failed to remove existing directory %s: %w", extractDir, err)
}
} else {
singleFilePath := filepath.Join(outputDir, itemName)
if err := removeExisting(singleFilePath); err != nil {
return fmt.Errorf("failed to remove existing file %s: %w", singleFilePath, err)
}
}
for _, file := range files {
filePath := filepath.Join(outputDir, file.Name)
fileDir := filepath.Dir(filePath)
if err := os.MkdirAll(fileDir, 0755); err != nil {
return fmt.Errorf("failed to create directory for file %s: %w", filePath, err)
}
if err := os.WriteFile(filePath, file.Data, 0644); err != nil {
return fmt.Errorf("failed to write file %s: %w", filePath, err)
}
fmt.Printf("Written: %s\n", filePath)
}
return nil
}
func createSymlink(targetPath, binDir, symlinkName string) error {
if err := os.MkdirAll(binDir, 0755); err != nil {
return fmt.Errorf("failed to create bin directory: %w", err)
}
symlinkPath := filepath.Join(binDir, symlinkName)
if err := removeExisting(symlinkPath); err != nil {
return fmt.Errorf("failed to remove existing file/directory at symlink location: %w", err)
}
// Convert target path to absolute path to ensure symlink works correctly
absTargetPath, err := filepath.Abs(targetPath)
if err != nil {
return fmt.Errorf("failed to resolve absolute path for target: %w", err)
}
if err := os.Symlink(absTargetPath, symlinkPath); err != nil {
return fmt.Errorf("failed to create symlink: %w", err)
}
if err := os.Chmod(absTargetPath, 0755); err != nil {
fmt.Printf("Warning: failed to make target executable: %v\n", err)
}
fmt.Printf("Created symlink: %s -> %s\n", symlinkPath, absTargetPath)
return nil
}