chore(errors): Better error management

We must remove all "Fatal" calls and use errors instead, to be returned
and managed globally.
This is the first step, but it is, at this time, a real problem. Tests
are complicated without this.
This commit is contained in:
2024-12-03 14:37:13 +01:00
parent eb760d4299
commit e574a2e2a8
5 changed files with 41 additions and 18 deletions

View File

@@ -1,6 +1,7 @@
package generator
import (
"fmt"
"katenary/generator/labels"
"katenary/generator/labels/labelStructs"
"katenary/utils"
@@ -141,7 +142,7 @@ func NewConfigMapFromDirectory(service types.ServiceConfig, appName, path string
// cumulate the path to the WorkingDir
path = filepath.Join(service.WorkingDir, path)
path = filepath.Clean(path)
cm.AppenddDir(path)
cm.AppendDir(path)
return cm
}
@@ -160,17 +161,17 @@ func (c *ConfigMap) AddBinaryData(key string, value []byte) {
// AddFile adds files from given path to the configmap. It is not recursive, to add all files in a directory,
// you need to call this function for each subdirectory.
func (c *ConfigMap) AppenddDir(path string) {
func (c *ConfigMap) AppendDir(path string) error {
// read all files in the path and add them to the configmap
stat, err := os.Stat(path)
if err != nil {
log.Fatalf("Path %s does not exist\n", path)
return fmt.Errorf("Path %s does not exist, %w\n", path, err)
}
// recursively read all files in the path and add them to the configmap
if stat.IsDir() {
files, err := os.ReadDir(path)
if err != nil {
log.Fatal(err)
return err
}
for _, file := range files {
if file.IsDir() {
@@ -180,7 +181,7 @@ func (c *ConfigMap) AppenddDir(path string) {
path := filepath.Join(path, file.Name())
content, err := os.ReadFile(path)
if err != nil {
log.Fatal(err)
return err
}
// remove the path from the file
filename := filepath.Base(path)
@@ -195,7 +196,7 @@ func (c *ConfigMap) AppenddDir(path string) {
// add the file to the configmap
content, err := os.ReadFile(path)
if err != nil {
log.Fatal(err)
return err
}
filename := filepath.Base(path)
if utf8.Valid(content) {
@@ -204,20 +205,21 @@ func (c *ConfigMap) AppenddDir(path string) {
c.AddBinaryData(filename, content)
}
}
return nil
}
func (c *ConfigMap) AppendFile(path string) {
func (c *ConfigMap) AppendFile(path string) error {
// read all files in the path and add them to the configmap
stat, err := os.Stat(path)
if err != nil {
log.Fatalf("Path %s does not exist\n", path)
return fmt.Errorf("Path %s doesn not exists, %w", path, err)
}
// recursively read all files in the path and add them to the configmap
if !stat.IsDir() {
// add the file to the configmap
content, err := os.ReadFile(path)
if err != nil {
log.Fatal(err)
return err
}
if utf8.Valid(content) {
c.AddData(filepath.Base(path), string(content))
@@ -226,6 +228,7 @@ func (c *ConfigMap) AppendFile(path string) {
}
}
return nil
}
// Filename returns the filename of the configmap. If the configmap is used for files, the filename contains the path.