-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagefile.go
188 lines (158 loc) · 4.69 KB
/
magefile.go
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
//go:build mage
// +build mage
package main
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"regexp"
)
// Whitelisted files for download
var filesMap = map[string]struct {
Files []string
SourcePath string
DestPath string
}{
"semconv": {
Files: []string{
"bench_test.go.tmpl",
"env.go.tmpl",
"env_test.go.tmpl",
"httpconv.go.tmpl",
"httpconv_test.go.tmpl",
"util.go.tmpl",
"util_test.go.tmpl",
"v1.20.0.go.tmpl",
},
SourcePath: "internal/shared/semconv",
DestPath: "internal/shared/semconv",
},
"semconvutil": {
Files: []string{
"httpconv.go.tmpl",
"httpconv_test.go.tmpl",
"netconv.go.tmpl",
"netconv_test.go.tmpl",
},
SourcePath: "internal/shared/semconvutil",
DestPath: "internal/shared/semconvutil",
},
}
const (
branchURLPattern = "https://raw.githubusercontent.com/open-telemetry/opentelemetry-go-contrib/refs/heads/%s"
tagURLPattern = "https://raw.githubusercontent.com/open-telemetry/opentelemetry-go-contrib/refs/tags/%s"
)
// DownloadSemConv downloads files from semconv directory
// using the provided reference (tag or branch name)
func DownloadSemConv(ref string) error {
if ref == "" {
return fmt.Errorf("reference (tag or branch name) is required")
}
return downloadFiles("semconv", ref)
}
// DownloadSemConvUtil downloads files from semconvutil directory
// using the provided reference (tag or branch name)
func DownloadSemConvUtil(ref string) error {
if ref == "" {
return fmt.Errorf("reference (tag or branch name) is required")
}
return downloadFiles("semconvutil", ref)
}
// DownloadAll downloads all files from both directories
// using the provided reference (tag or branch name)
func DownloadAll(ref string) error {
if ref == "" {
return fmt.Errorf("reference (tag or branch name) is required")
}
for dir := range filesMap {
if err := downloadFiles(dir, ref); err != nil {
return err
}
}
return nil
}
// Helper function to download files
func downloadFiles(dirKey string, ref string) error {
dirInfo, exists := filesMap[dirKey]
if !exists {
return fmt.Errorf("unknown directory key: %s", dirKey)
}
if err := os.RemoveAll(dirInfo.DestPath); err != nil {
fmt.Printf("Failed to delete target directory: %s", err)
}
if err := os.MkdirAll(dirInfo.DestPath, 0755); err != nil {
return fmt.Errorf("failed to create target directory: %w", err)
}
var baseURL string
var refType string
if isTag(ref) {
baseURL = fmt.Sprintf(tagURLPattern, ref)
refType = "tag"
} else {
baseURL = fmt.Sprintf(branchURLPattern, ref)
refType = "branch"
}
fmt.Printf("Downloading files from %s/%s (%s: %s)\n", baseURL, dirInfo.SourcePath, refType, ref)
for _, file := range dirInfo.Files {
url := fmt.Sprintf("%s/%s/%s", baseURL, dirInfo.SourcePath, file)
targetPath := filepath.Join(dirInfo.DestPath, file)
fmt.Printf("Downloading %s to %s\n", url, targetPath)
resp, err := http.Get(url)
if err != nil {
return fmt.Errorf("failed to download %s: %w", file, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("received non-200 response code: %d for %s", resp.StatusCode, url)
}
out, err := os.Create(targetPath)
if err != nil {
return fmt.Errorf("failed to create file %s: %w", targetPath, err)
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
return fmt.Errorf("failed to write to file %s: %w", targetPath, err)
}
}
fmt.Printf("Successfully downloaded all files from %s\n", dirInfo.SourcePath)
return nil
}
// ListTargetFiles lists all files that will be downloaded
// for the provided reference (tag or branch name)
func ListTargetFiles(ref string) error {
if ref == "" {
return fmt.Errorf("reference (tag or branch name) is required")
}
totalFiles := 0
var baseURL string
var refType string
if isTag(ref) {
baseURL = fmt.Sprintf(tagURLPattern, ref)
refType = "tag"
} else {
baseURL = fmt.Sprintf(branchURLPattern, ref)
refType = "branch"
}
sourceInfo := fmt.Sprintf("(%s: %s)", refType, ref)
for dirKey, dirInfo := range filesMap {
fmt.Printf("Files from %s to be downloaded from: %s/%s %s\n", dirKey, baseURL, dirInfo.SourcePath, sourceInfo)
fmt.Printf("Destination: %s\n", dirInfo.DestPath)
fmt.Println("=================================")
for _, file := range dirInfo.Files {
fmt.Println(file)
}
fmt.Printf("Total files in %s: %d\n", dirKey, len(dirInfo.Files))
totalFiles += len(dirInfo.Files)
fmt.Println("=================================")
}
fmt.Printf("Total files to be downloaded: %d\n", totalFiles)
return nil
}
// isTag checks if the string is a version tag (vX.Y.Z format)
func isTag(ref string) bool {
matched, _ := regexp.MatchString(`^v\d+\.\d+\.\d+.*$`, ref)
return matched
}