Skip to content

Commit 8163a32

Browse files
committed
Better error handling
1 parent 868f59e commit 8163a32

6 files changed

Lines changed: 25 additions & 26 deletions

File tree

cmd/env-exec/main.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

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

67
"github.com/polarn/env-exec/internal/provider"
@@ -12,13 +13,17 @@ func main() {
1213
config := utils.LoadConfig()
1314

1415
for _, p := range provider.AllProviders() {
15-
p.Provide(config, envVars)
16+
if err := p.Provide(config, envVars); err != nil {
17+
log.Fatalf("Error: %v", err)
18+
}
1619
}
1720

1821
if len(os.Args) < 2 {
1922
utils.PrintEnvVars(envVars)
2023
} else {
2124
utils.SetEnvVars(envVars)
22-
utils.ExecuteCommand()
25+
if err := utils.ExecuteCommand(); err != nil {
26+
os.Exit(1)
27+
}
2328
}
2429
}

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: 6 additions & 5 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 {

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: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,26 +69,17 @@ func ExecuteCommand() error {
6969
args := os.Args[2:]
7070

7171
cmd := exec.Command(command, args...)
72-
73-
// Set the standard input, output, and error streams to the current process's
7472
cmd.Stdin = os.Stdin
7573
cmd.Stdout = os.Stdout
7674
cmd.Stderr = os.Stderr
7775

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
76+
if err := cmd.Run(); err != nil {
8177
if exitError, ok := err.(*exec.ExitError); ok {
8278
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)
79+
os.Exit(status.ExitStatus())
8680
}
87-
os.Exit(1)
88-
} else {
89-
fmt.Printf("Failed to run command: %v\n", err)
90-
os.Exit(1)
9181
}
82+
return fmt.Errorf("failed to run command: %w", err)
9283
}
9384
return nil
9485
}

0 commit comments

Comments
 (0)