-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.go
More file actions
291 lines (240 loc) · 6.53 KB
/
storage.go
File metadata and controls
291 lines (240 loc) · 6.53 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
package main
import (
"archive/tar"
"compress/gzip"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
// ANSI color codes
const (
colorReset = "\033[0m"
colorGreen = "\033[32m"
colorRed = "\033[31m"
colorYellow = "\033[33m"
)
// Check if output supports colors
func supportsColor() bool {
if os.Getenv("NO_COLOR") != "" {
return false
}
if os.Getenv("TERM") == "dumb" {
return false
}
return isatty()
}
// Simple TTY check (works on Unix-like systems)
func isatty() bool {
fd := os.Stdout.Fd()
return fd == 1 || fd == 2 // stdout or stderr
}
// Colorize text if colors are supported
func colorize(color, text string) string {
if supportsColor() {
return color + text + colorReset
}
return text
}
type LockFile struct {
Dependencies map[string]Dependency `json:"dependencies"`
}
type Dependency struct {
Ref string `json:"ref"`
SHA string `json:"sha"`
Hash string `json:"hash,omitempty"`
}
type CheckResult struct {
Status string // "ok", "missing", "update_available"
LatestSHA string // populated when Status == "update_available"
}
func loadLockFile() *LockFile {
lockFile := &LockFile{
Dependencies: make(map[string]Dependency),
}
data, err := os.ReadFile(".deps.lock")
if err != nil {
// File doesn't exist, return empty lock file
return lockFile
}
err = json.Unmarshal(data, lockFile)
if err != nil {
fmt.Printf("Warning: could not parse existing .deps.lock: %v\n", err)
return &LockFile{
Dependencies: make(map[string]Dependency),
}
}
return lockFile
}
func saveLockFile(lockFile *LockFile) error {
data, err := json.MarshalIndent(lockFile, "", " ")
if err != nil {
return err
}
return os.WriteFile(".deps.lock", data, 0644)
}
func checkDependency(repoURL string, dep Dependency) (CheckResult, error) {
// Check if directory exists
depPath := getDepPath(repoURL)
if _, err := os.Stat(depPath); os.IsNotExist(err) {
return CheckResult{Status: "missing"}, nil
}
// Resolve the current SHA for the tracked ref to detect updates
owner, repo, err := parseGitHubURL(repoURL)
if err != nil {
return CheckResult{}, fmt.Errorf("parsing URL: %v", err)
}
currentSHA, _, err := resolveRef(owner, repo, dep.Ref)
if err != nil {
return CheckResult{}, fmt.Errorf("resolving ref %s: %v", dep.Ref, err)
}
if currentSHA != dep.SHA {
return CheckResult{Status: "update_available", LatestSHA: currentSHA}, nil
}
return CheckResult{Status: "ok"}, nil
}
func updateDependency(repoURL string, dep Dependency, lockFile *LockFile) bool {
owner, repo, err := parseGitHubURL(repoURL)
if err != nil {
fmt.Printf("✗ Error parsing URL %s: %v\n", repoURL, err)
return false
}
// Resolve current state of the original ref
currentSHA, currentRef, err := resolveRef(owner, repo, dep.Ref)
if err != nil {
fmt.Printf("%s Error resolving %s@%s: %v\n", colorize(colorRed, "✗"), repoURL, dep.Ref, err)
return false
}
if currentSHA == dep.SHA {
fmt.Printf("%s %s@%s (%s) - no update available\n", colorize(colorGreen, "✓"), repoURL, dep.Ref, dep.SHA[:8])
return false
}
fmt.Printf("Update available for %s:\n", repoURL)
fmt.Printf(" Current: %s (%s)\n", dep.SHA[:8], dep.Ref)
fmt.Printf(" Latest: %s (%s)\n", currentSHA[:8], currentRef)
// Download updated version
hash, err := downloadRepo(owner, repo, currentSHA, repoURL)
if err != nil {
fmt.Printf("%s Error downloading update: %v\n", colorize(colorRed, "✗"), err)
return false
}
// Update lock file entry
lockFile.Dependencies[repoURL] = Dependency{
Ref: dep.Ref,
SHA: currentSHA,
Hash: hash,
}
fmt.Printf("%s Updated %s to %s (%s)\n", colorize(colorGreen, "✓"), repoURL, currentRef, currentSHA[:8])
return true
}
func downloadRepo(owner, repo, sha, repoURL string) (string, error) {
// Create .deps directory if it doesn't exist
err := os.MkdirAll(".deps", 0755)
if err != nil {
return "", err
}
// Download tarball
tarballURL := fmt.Sprintf("%s/repos/%s/%s/tarball/%s", githubAPIBaseURL, owner, repo, sha)
resp, err := httpClient.Get(tarballURL)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return "", fmt.Errorf("GitHub API returned status %d", resp.StatusCode)
}
// Hash the tarball content as we stream it through
hasher := sha256.New()
reader := io.TeeReader(resp.Body, hasher)
// Extract tarball
depPath := getDepPath(repoURL)
err = extractTarball(reader, depPath)
if err != nil {
return "", err
}
hash := hex.EncodeToString(hasher.Sum(nil))
fmt.Printf("Downloaded to %s\n", depPath)
return hash, nil
}
func extractTarball(r io.Reader, destPath string) error {
// Remove existing directory
os.RemoveAll(destPath)
// Create destination directory
err := os.MkdirAll(destPath, 0755)
if err != nil {
return err
}
// Open gzip reader
gzr, err := gzip.NewReader(r)
if err != nil {
return err
}
defer gzr.Close()
// Open tar reader
tr := tar.NewReader(gzr)
// Track the root directory name (GitHub adds a prefix like "repo-sha/")
var rootDir string
for {
header, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
// Skip pax headers and other non-standard entries
if header.Typeflag == tar.TypeXGlobalHeader || header.Name == "pax_global_header" {
continue
}
// Detect the root directory from the first real directory entry
if rootDir == "" && strings.Contains(header.Name, "/") {
parts := strings.Split(header.Name, "/")
if len(parts) > 0 && strings.Contains(parts[0], "-") {
rootDir = parts[0] + "/"
}
}
// Skip the root directory entry itself
if header.Typeflag == tar.TypeDir && rootDir != "" && header.Name == rootDir[:len(rootDir)-1] {
continue
}
// Skip entries that don't start with our detected root directory
if rootDir == "" || !strings.HasPrefix(header.Name, rootDir) {
continue
}
// Remove the root directory prefix to flatten the structure
name := strings.TrimPrefix(header.Name, rootDir)
if name == "" {
continue
}
target := filepath.Join(destPath, name)
switch header.Typeflag {
case tar.TypeDir:
err = os.MkdirAll(target, os.FileMode(header.Mode))
if err != nil {
return err
}
case tar.TypeReg:
err = os.MkdirAll(filepath.Dir(target), 0755)
if err != nil {
return err
}
f, err := os.OpenFile(target, os.O_CREATE|os.O_WRONLY, os.FileMode(header.Mode))
if err != nil {
return err
}
_, err = io.Copy(f, tr)
f.Close()
if err != nil {
return err
}
}
}
return nil
}
func getDepPath(repoURL string) string {
return filepath.Join(".deps", repoURL)
}