Skip to content

Commit

Permalink
chore: log possible error while reading config file (#1240)
Browse files Browse the repository at this point in the history
* log actual  error when they exist during config reading process

* chore: refactor error message

---------

Co-authored-by: Abiola Ibrahim <[email protected]>
  • Loading branch information
olamilekan000 and abiosoft authored Jan 5, 2025
1 parent 1f32bf2 commit 4751918
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
5 changes: 5 additions & 0 deletions environment/vm/lima/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lima

import (
"context"
"encoding/json"
"fmt"
"path/filepath"
Expand All @@ -9,9 +10,13 @@ import (
const configFile = "/etc/colima/colima.json"

func (l limaVM) getConf() map[string]string {
log := l.Logger(context.Background())

obj := map[string]string{}
b, err := l.Read(configFile)
if err != nil {
log.Trace(fmt.Errorf("error reading config file: %w", err))

return obj
}

Expand Down
11 changes: 7 additions & 4 deletions environment/vm/lima/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
func (l limaVM) Read(fileName string) (string, error) {
s, err := l.RunOutput("sudo", "cat", fileName)
if err != nil {
return "", fmt.Errorf("cannot read file: %s", fileName)
return "", fmt.Errorf("cannot read file '%s': %w", fileName, err)
}
return s, err
}
Expand Down Expand Up @@ -45,16 +45,15 @@ type fileInfo struct {
}

func newFileInfo(guest environment.GuestActions, filename string) (fileInfo, error) {
statErr := fmt.Errorf("cannot stat file: %s", filename)
info := fileInfo{}
// "%s,%a,%Y,%F" -> size, permission, modified time, type
stat, err := guest.RunOutput("sudo", "stat", "-c", "%s,%a,%Y,%F", filename)
if err != nil {
return info, statErr
return info, statError(filename, err)
}
stats := strings.Split(stat, ",")
if len(stats) < 4 {
return info, statErr
return info, statError(filename, err)
}
info.name = filename
info.size, _ = strconv.ParseInt(stats[0], 10, 64)
Expand All @@ -71,6 +70,10 @@ func newFileInfo(guest environment.GuestActions, filename string) (fileInfo, err
return info, nil
}

func statError(filename string, err error) error {
return fmt.Errorf("cannot stat file '%s': %w", filename, err)
}

// IsDir implements fs.FileInfo
func (f fileInfo) IsDir() bool { return f.isDir }

Expand Down

0 comments on commit 4751918

Please sign in to comment.