-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.go
More file actions
184 lines (157 loc) · 5.65 KB
/
utils.go
File metadata and controls
184 lines (157 loc) · 5.65 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
package codegen
import (
"archive/zip"
"encoding/json"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/streamingfast/substreams-codegen/loop"
)
func Cmd(msg any) loop.Cmd {
return func() loop.Msg {
return msg
}
}
func ZipFiles(files map[string][]byte) ([]byte, error) {
tempDir, err := os.MkdirTemp(os.TempDir(), "zipper")
if err != nil {
return nil, fmt.Errorf("mkdir temp: %w", err)
}
if os.Getenv("GENERATOR_KEEP_FILES") != "true" {
defer os.RemoveAll(tempDir)
} else {
fmt.Println("Keeping files in", tempDir)
}
zipFilepath := filepath.Join(tempDir, "source.zip")
// write the content of the zip file here
zipFile, err := os.Create(zipFilepath)
if err != nil {
return nil, fmt.Errorf("creating zip file: %w", err)
}
defer zipFile.Close()
zipWriter := zip.NewWriter(zipFile)
for relativeFile, content := range files {
fullFilepath := strings.ReplaceAll(relativeFile, "/", string(os.PathSeparator))
fh := &zip.FileHeader{
Name: fullFilepath,
Method: zip.Deflate,
}
if strings.HasSuffix(fullFilepath, ".sh") {
fh.SetMode(0755)
}
// Create a writer for each file in the zip archive
writer, err := zipWriter.CreateHeader(fh)
if err != nil {
return nil, fmt.Errorf("creating zip writer: %w", err)
}
// Write the file data to the zip archive
_, err = writer.Write(content)
if err != nil {
return nil, fmt.Errorf("writing to zip: %w", err)
}
}
// Close the zip archive
err = zipWriter.Close()
if err != nil {
return nil, fmt.Errorf("closing zip: %w", err)
}
// Open the zip file to send the bytes
zipFileB, err := os.ReadFile(zipFilepath)
if err != nil {
return nil, fmt.Errorf("opening zip file: %w", err)
}
return zipFileB, nil
}
// PrettifyJSON takes raw JSON bytes and returns prettified JSON with proper indentation.
// If the input is not valid JSON, it returns the original content unchanged.
func PrettifyJSON(rawJSON []byte) []byte {
if len(rawJSON) == 0 {
return rawJSON
}
var jsonData interface{}
if err := json.Unmarshal(rawJSON, &jsonData); err != nil {
// If it's not valid JSON, return the original content
return rawJSON
}
prettified, err := json.MarshalIndent(jsonData, "", " ")
if err != nil {
// If prettification fails, return the original content
return rawJSON
}
return prettified
}
// errorPattern defines a pattern for matching and transforming error messages
type errorPattern struct {
pattern *regexp.Regexp
// transform takes the matched groups and returns the user-friendly message
// The first element of matches is always the full match, subsequent elements are capture groups
transform func(matches []string) string
}
var (
// Compiled once at init time for performance
wrappedFileErrorPattern = regexp.MustCompile(`^could not read file "([^"]+)":.+:\s*(.+)$`)
fileNotFoundPattern = regexp.MustCompile(`no such file or directory`)
permissionDeniedPattern = regexp.MustCompile(`permission denied`)
isDirectoryPattern = regexp.MustCompile(`is a directory`)
// errorPatterns is the list of error patterns to check, in order
errorPatterns = []errorPattern{
// Wrapped file errors with filename extraction
{
pattern: wrappedFileErrorPattern,
transform: func(matches []string) string {
filename := matches[1]
innerErr := matches[2]
// Check the inner error and provide context-specific message
if fileNotFoundPattern.MatchString(innerErr) {
return fmt.Sprintf("File not found: %s", filename)
}
if permissionDeniedPattern.MatchString(innerErr) {
return fmt.Sprintf("Permission denied for file: %s", filename)
}
if isDirectoryPattern.MatchString(innerErr) {
return fmt.Sprintf("Path is a directory, not a file: %s", filename)
}
// Unknown inner error, still show filename
return fmt.Sprintf("%s (file: %s)", innerErr, filename)
},
},
}
// Simple string-based mappings (no regex needed)
simpleErrorMappings = map[string]string{
"no such file or directory": "File not found - please check the path and try again",
"permission denied": "Permission denied - please check file permissions",
"is a directory": "Path points to a directory, not a file - please provide a file path",
"contract source code is not verified": "Contract source code is not verified on the block explorer - you'll need to provide the ABI manually",
"invalid contract address or contract does not exist": "Invalid contract address or contract does not exist at this address",
}
)
// MapClientSideErrorToMessage converts technical errors from the CLI client into user-friendly messages.
//
// This function handles errors that originate from the CLI client (file reading, HTTP requests, etc.)
// which are transmitted as strings. It uses regex patterns to extract context (like filenames) and
// provides clear, actionable error messages.
//
// Note: While working with error strings is somewhat brittle (dependent on error message formats),
// it's acceptable for improving user experience with common error cases from client-side operations.
func MapClientSideErrorToMessage(err error) string {
if err == nil {
return ""
}
errStr := err.Error()
// Try regex patterns first (they may extract additional context like filenames)
for _, ep := range errorPatterns {
if matches := ep.pattern.FindStringSubmatch(errStr); matches != nil {
return ep.transform(matches)
}
}
// Try simple substring mappings
for errorSubstring, userMessage := range simpleErrorMappings {
if strings.Contains(errStr, errorSubstring) {
return userMessage
}
}
// Return original error if no mapping found
return errStr
}