Skip to content

Commit 67c1f36

Browse files
committed
fix: switched to meshkit logger and other fixes
Signed-off-by: SHIGRAF SALIK <[email protected]>
1 parent eefc216 commit 67c1f36

File tree

3 files changed

+35
-19
lines changed

3 files changed

+35
-19
lines changed

utils/cue.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func Lookup(rootVal cue.Value, path string) (cue.Value, error) {
111111
return res, ErrCueLookup(res.Err())
112112
}
113113
if !res.Exists() {
114-
return res, ErrCueLookup(fmt.Errorf("Could not find the value at the path: %s", path))
114+
return res, ErrCueLookup(fmt.Errorf("could not find the value at the path: %s", path))
115115
}
116116

117117
return res.Value(), nil

utils/utils.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,23 +136,33 @@ func GetHome() string {
136136
return usr.HomeDir
137137
}
138138

139-
// CreateFile creates a file with the given content on the given location with
140-
// the given filename
139+
// CreateFile creates a file with the given content at the specified location.
141140
func CreateFile(contents []byte, filename string, location string) error {
142-
// Create file in -rw-r--r-- mode
143-
fd, err := os.OpenFile(filepath.Join(location, filename), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
141+
log, err := logger.New("utils", logger.Options{})
144142
if err != nil {
143+
// Cannot create logger, return the underlying error.
145144
return err
146145
}
147146

147+
filePath := filepath.Join(location, filename)
148+
fd, err := os.Create(filePath)
149+
if err != nil {
150+
return err // Or a meshkit-style error wrapper
151+
}
152+
148153
if _, err = fd.Write(contents); err != nil {
149-
fd.Close() // Close the file before returning the error
154+
// Attempt to close the file descriptor, but prioritize the original write error.
155+
// Log the closing error if it occurs, so the information is not lost.
156+
if closeErr := fd.Close(); closeErr != nil {
157+
log.Warn(fmt.Errorf("failed to close file descriptor for %s after a write error: %w", filename, closeErr))
158+
}
150159
return err
151160
}
152161

153-
// Check error while closing the file
162+
// Check for an error while closing the file.
154163
if err := fd.Close(); err != nil {
155-
return fmt.Errorf("failed to close file descriptor: %w", err)
164+
// Return a standardized meshkit-style error.
165+
return ErrFileClose(err, filename)
156166
}
157167

158168
return nil
@@ -890,8 +900,8 @@ func TruncateErrorMessage(err error, wordLimit int) error {
890900
words := strings.Fields(err.Error())
891901
if len(words) > wordLimit {
892902
words = words[:wordLimit]
893-
return fmt.Errorf("%s...", strings.Join(words, " "))
903+
return fmt.Errorf("%s", strings.Join(words, " "))
894904
}
895905

896906
return err
897-
}
907+
}

utils/walker/git.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/go-git/go-git/v5"
1616
"github.com/go-git/go-git/v5/plumbing"
17+
"github.com/meshery/meshkit/logger"
1718
)
1819

1920
// Git represents the Git Walker
@@ -136,6 +137,11 @@ func (g *Git) RegisterDirInterceptor(i DirInterceptor) *Git {
136137
return g
137138
}
138139
func clonewalk(g *Git) error {
140+
log, err := logger.New("walker-git", logger.Options{})
141+
if err != nil {
142+
return err
143+
}
144+
139145
if g.maxFileSizeInBytes == 0 {
140146
return ErrInvalidSizeFile(errors.New("max file size passed as 0. Will not read any file"))
141147
}
@@ -145,10 +151,9 @@ func clonewalk(g *Git) error {
145151
defer func() {
146152
wg.Wait()
147153
if err := os.RemoveAll(path); err != nil {
148-
fmt.Println(fmt.Errorf("failed to remove path %s: %w", path, err))
154+
log.Error(fmt.Errorf("failed to remove path %s: %w", path, err))
149155
}
150156
}()
151-
var err error
152157
cloneOptions := &git.CloneOptions{
153158
URL: fmt.Sprintf("%s/%s/%s", g.baseURL, g.owner, g.repo),
154159
SingleBranch: true,
@@ -177,7 +182,7 @@ func clonewalk(g *Git) error {
177182
}
178183

179184
if !info.IsDir() {
180-
err = g.readFile(info, rootPath)
185+
err = g.readFile(info, rootPath, log)
181186
if err != nil {
182187
return ErrCloningRepo(err)
183188
}
@@ -199,7 +204,7 @@ func clonewalk(g *Git) error {
199204
if err != nil {
200205
return errInfo
201206
}
202-
return g.readFile(f, path)
207+
return g.readFile(f, path, log)
203208
})
204209
if err != nil {
205210
return ErrCloningRepo(err)
@@ -233,31 +238,32 @@ func clonewalk(g *Git) error {
233238
Path: fPath,
234239
})
235240
if err != nil {
236-
fmt.Println(err.Error())
241+
log.Error(err)
237242
}
238243
}(name, fPath, f.Name())
239244
continue
240245
}
241246
if f.IsDir() {
242247
continue
243248
}
244-
err := g.readFile(f, fPath)
249+
err := g.readFile(f, fPath, log)
245250
if err != nil {
246-
fmt.Println(err.Error())
251+
log.Error(err)
247252
}
248253
}
249254

250255
return nil
251256
}
252257

253-
func (g *Git) readFile(f fs.FileInfo, path string) error {
258+
func (g *Git) readFile(f fs.FileInfo, path string, log logger.Handler) error {
254259
if f.Size() > g.maxFileSizeInBytes {
255260
return ErrInvalidSizeFile(errors.New("File exceeding size limit"))
256261
}
257262
filename, err := os.Open(path)
258263
if err != nil {
259264
return err
260265
}
266+
defer filename.Close()
261267
content, err := io.ReadAll(filename)
262268
if err != nil {
263269
return err
@@ -268,7 +274,7 @@ func (g *Git) readFile(f fs.FileInfo, path string) error {
268274
Content: string(content),
269275
})
270276
if err != nil {
271-
fmt.Println("Could not intercept the file ", f.Name())
277+
log.Warnf("Could not intercept the file %s: %v", f.Name(), err)
272278
}
273279
return err
274280
}

0 commit comments

Comments
 (0)