@@ -2,7 +2,6 @@ package utils
22
33import (
44 "fmt"
5- "log"
65 "os"
76 "os/exec"
87 "strings"
@@ -12,7 +11,7 @@ import (
1211 "gopkg.in/yaml.v3"
1312)
1413
15- func LoadConfigFile () []byte {
14+ func LoadConfigFile () ( []byte , error ) {
1615 filename := ".env-exec.yaml"
1716 if os .Getenv ("ENV_EXEC_YAML" ) != "" {
1817 filename = os .Getenv ("ENV_EXEC_YAML" )
@@ -22,28 +21,26 @@ func LoadConfigFile() []byte {
2221 if err != nil {
2322 if os .IsNotExist (err ) {
2423 // This is OK, means we will not expose any variables
25- return nil
26- } else if os .IsPermission (err ) {
27- log .Fatalf ("Specific error: Permission denied to read file '%s'.\n " , filename )
28- } else {
29- log .Fatalf ("Specific error: An unexpected error occurred while reading file '%s'.\n " , filename )
24+ return nil , nil
3025 }
31- return nil
26+ return nil , fmt . Errorf ( "failed to read config file '%s': %w" , filename , err )
3227 }
33- return data
28+ return data , nil
3429}
3530
36- func LoadConfig () * config.RootConfig {
37- var config config.RootConfig
31+ func LoadConfig () ( * config.RootConfig , error ) {
32+ var cfg config.RootConfig
3833
39- data := LoadConfigFile ()
34+ data , err := LoadConfigFile ()
35+ if err != nil {
36+ return nil , err
37+ }
4038 if data != nil {
41- err := yaml .Unmarshal (data , & config )
42- if err != nil {
43- log .Fatalf ("Error unmarshalling YAML: %v" , err )
39+ if err := yaml .Unmarshal (data , & cfg ); err != nil {
40+ return nil , fmt .Errorf ("failed to parse config: %w" , err )
4441 }
4542 }
46- return & config
43+ return & cfg , nil
4744}
4845
4946func PrintEnvVars (envVars map [string ]string ) {
@@ -54,41 +51,31 @@ func PrintEnvVars(envVars map[string]string) {
5451 }
5552}
5653
57- func SetEnvVars (envVars map [string ]string ) {
54+ func SetEnvVars (envVars map [string ]string ) error {
5855 for key , value := range envVars {
59- err := os .Setenv (key , value )
60- if err != nil {
61- fmt .Println ("Error setting environment variable:" , err )
62- return
56+ if err := os .Setenv (key , value ); err != nil {
57+ return fmt .Errorf ("failed to set environment variable '%s': %w" , key , err )
6358 }
6459 }
60+ return nil
6561}
6662
6763func ExecuteCommand () error {
6864 command := os .Args [1 ]
6965 args := os .Args [2 :]
7066
7167 cmd := exec .Command (command , args ... )
72-
73- // Set the standard input, output, and error streams to the current process's
7468 cmd .Stdin = os .Stdin
7569 cmd .Stdout = os .Stdout
7670 cmd .Stderr = os .Stderr
7771
78- err := cmd .Run ()
79- if err != nil {
80- // If the command exited with a non-zero status, the error will be of type *exec.ExitError
72+ if err := cmd .Run (); err != nil {
8173 if exitError , ok := err .(* exec.ExitError ); ok {
8274 if status , ok := exitError .Sys ().(syscall.WaitStatus ); ok {
83- fmt .Printf ("Command exited with status: %d\n " , status .ExitStatus ())
84- } else {
85- fmt .Printf ("Command failed: %v\n " , err )
75+ os .Exit (status .ExitStatus ())
8676 }
87- os .Exit (1 )
88- } else {
89- fmt .Printf ("Failed to run command: %v\n " , err )
90- os .Exit (1 )
9177 }
78+ return fmt .Errorf ("failed to run command: %w" , err )
9279 }
9380 return nil
9481}
0 commit comments