From 4751918d2881ed9a240696198a8f456b8e8b0fee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=BB=8Cla=CC=81mile=CC=81kan?= Date: Sun, 5 Jan 2025 16:18:04 +0100 Subject: [PATCH] chore: log possible error while reading config file (#1240) * log actual error when they exist during config reading process * chore: refactor error message --------- Co-authored-by: Abiola Ibrahim --- environment/vm/lima/config.go | 5 +++++ environment/vm/lima/file.go | 11 +++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/environment/vm/lima/config.go b/environment/vm/lima/config.go index c7c055850..f0ad9c9e8 100644 --- a/environment/vm/lima/config.go +++ b/environment/vm/lima/config.go @@ -1,6 +1,7 @@ package lima import ( + "context" "encoding/json" "fmt" "path/filepath" @@ -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 } diff --git a/environment/vm/lima/file.go b/environment/vm/lima/file.go index ded773233..5ee0f1a50 100644 --- a/environment/vm/lima/file.go +++ b/environment/vm/lima/file.go @@ -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 } @@ -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) @@ -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 }