Skip to content

Commit 8693237

Browse files
committed
Better and more consistent error handling
1 parent 868f59e commit 8693237

6 files changed

Lines changed: 51 additions & 52 deletions

File tree

cmd/env-exec/main.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
11
package main
22

33
import (
4+
"log"
45
"os"
56

67
"github.com/polarn/env-exec/internal/provider"
78
"github.com/polarn/env-exec/internal/utils"
89
)
910

1011
func main() {
11-
envVars := make(map[string]string)
12-
config := utils.LoadConfig()
12+
config, err := utils.LoadConfig()
13+
if err != nil {
14+
log.Fatalf("Error: %v", err)
15+
}
1316

17+
envVars := make(map[string]string)
1418
for _, p := range provider.AllProviders() {
15-
p.Provide(config, envVars)
19+
if err := p.Provide(config, envVars); err != nil {
20+
log.Fatalf("Error: %v", err)
21+
}
1622
}
1723

1824
if len(os.Args) < 2 {
1925
utils.PrintEnvVars(envVars)
2026
} else {
21-
utils.SetEnvVars(envVars)
22-
utils.ExecuteCommand()
27+
if err := utils.SetEnvVars(envVars); err != nil {
28+
log.Fatalf("Error: %v", err)
29+
}
30+
if err := utils.ExecuteCommand(); err != nil {
31+
log.Fatalf("Error: %v", err)
32+
}
2333
}
2434
}

internal/provider/gcp.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import (
1111
)
1212

1313
// Provide fetches GCP secrets and adds them to the envVars map.
14-
func (p *GCPProvider) Provide(cfg *config.RootConfig, envVars map[string]string) {
14+
func (p *GCPProvider) Provide(cfg *config.RootConfig, envVars map[string]string) error {
1515
if !hasGCPSecrets(cfg) {
16-
return
16+
return nil
1717
}
1818

1919
ctx := context.Background()
2020
client, err := secretmanager.NewClient(ctx)
2121
if err != nil {
22-
log.Fatalf("Failed to create Secret Manager client: %v", err)
22+
return fmt.Errorf("failed to create Secret Manager client: %w", err)
2323
}
2424
defer client.Close()
2525

@@ -34,7 +34,7 @@ func (p *GCPProvider) Provide(cfg *config.RootConfig, envVars map[string]string)
3434
}
3535

3636
if project == "" && cfg.Defaults.GCP.Project == "" {
37-
log.Printf("Error: No GCP project found for secret '%s'", env.Name)
37+
log.Printf("Warning: No GCP project found for secret '%s', skipping", env.Name)
3838
continue
3939
} else if project == "" {
4040
project = cfg.Defaults.GCP.Project
@@ -46,13 +46,14 @@ func (p *GCPProvider) Provide(cfg *config.RootConfig, envVars map[string]string)
4646
Name: reqName,
4747
})
4848
if err != nil {
49-
log.Printf("Error accessing GCP secret '%s' version '%s': %v", name, version, err)
49+
log.Printf("Warning: Failed to access GCP secret '%s' version '%s': %v", name, version, err)
5050
continue
5151
}
5252

5353
envVars[env.Name] = string(resp.Payload.Data)
5454
}
5555
}
56+
return nil
5657
}
5758

5859
func hasGCPSecrets(cfg *config.RootConfig) bool {

internal/provider/gitlab.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ type GitlabVariable struct {
2121
}
2222

2323
// Provide fetches GitLab variables and adds them to the envVars map.
24-
func (p *GitlabProvider) Provide(cfg *config.RootConfig, envVars map[string]string) {
24+
func (p *GitlabProvider) Provide(cfg *config.RootConfig, envVars map[string]string) error {
2525
if !hasGitlabVariables(cfg) {
26-
return
26+
return nil
2727
}
2828

2929
gitlabToken := os.Getenv("GITLAB_TOKEN")
3030
if gitlabToken == "" {
31-
log.Fatal("GITLAB_TOKEN environment variable not set")
31+
return fmt.Errorf("GITLAB_TOKEN environment variable not set")
3232
}
3333

3434
for _, env := range cfg.Env {
@@ -37,19 +37,20 @@ func (p *GitlabProvider) Provide(cfg *config.RootConfig, envVars map[string]stri
3737
project := env.ValueFrom.GitlabVariableKeyRef.Project
3838

3939
if project == "" {
40-
log.Printf("Error: No GitLab project found for variable '%s'", env.Name)
40+
log.Printf("Warning: No GitLab project found for variable '%s', skipping", env.Name)
4141
continue
4242
}
4343

4444
value, err := getGitlabVariable(gitlabToken, key, project)
4545
if err != nil {
46-
log.Printf("Error getting GitLab variable '%s': %v", key, err)
46+
log.Printf("Warning: Failed to get GitLab variable '%s': %v", key, err)
4747
continue
4848
}
4949

5050
envVars[env.Name] = value
5151
}
5252
}
53+
return nil
5354
}
5455

5556
func hasGitlabVariables(cfg *config.RootConfig) bool {
@@ -62,12 +63,11 @@ func hasGitlabVariables(cfg *config.RootConfig) bool {
6263
}
6364

6465
func getGitlabVariable(gitlabToken, key, project string) (string, error) {
65-
6666
apiURL := fmt.Sprintf("https://gitlab.com/api/v4/projects/%s/variables/%s", project, key)
6767

6868
req, err := http.NewRequest("GET", apiURL, nil)
6969
if err != nil {
70-
log.Fatalf("Failed to create request: %v", err)
70+
return "", fmt.Errorf("failed to create request: %w", err)
7171
}
7272

7373
req.Header.Set("PRIVATE-TOKEN", gitlabToken)

internal/provider/plain.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ package provider
33
import "github.com/polarn/env-exec/internal/config"
44

55
// Provide adds plain env vars to the envVars map.
6-
func (p *PlainProvider) Provide(cfg *config.RootConfig, envVars map[string]string) {
6+
func (p *PlainProvider) Provide(cfg *config.RootConfig, envVars map[string]string) error {
77
for _, env := range cfg.Env {
88
if env.Value != "" {
99
envVars[env.Name] = env.Value
1010
}
1111
}
12+
return nil
1213
}

internal/provider/provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import "github.com/polarn/env-exec/internal/config"
44

55
// Provider defines the interface for environment variable providers.
66
type Provider interface {
7-
Provide(config *config.RootConfig, envVars map[string]string)
7+
Provide(config *config.RootConfig, envVars map[string]string) error
88
}
99

1010
// PlainProvider provides static environment variables.

internal/utils/utils.go

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package utils
22

33
import (
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

4946
func 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

6763
func 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

Comments
 (0)