Skip to content

Commit 868f59e

Browse files
committed
Refactored to a provider interface
1 parent 438f7c4 commit 868f59e

6 files changed

Lines changed: 62 additions & 35 deletions

File tree

cmd/env-exec/main.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ package main
33
import (
44
"os"
55

6-
"github.com/polarn/env-exec/internal/process"
6+
"github.com/polarn/env-exec/internal/provider"
77
"github.com/polarn/env-exec/internal/utils"
88
)
99

1010
func main() {
11-
var envVars = make(map[string]string)
12-
11+
envVars := make(map[string]string)
1312
config := utils.LoadConfig()
14-
process.EnvVars(config, &envVars)
15-
process.EnvVarsGCP(config, &envVars)
16-
process.EnvVarsGitlab(config, &envVars)
13+
14+
for _, p := range provider.AllProviders() {
15+
p.Provide(config, envVars)
16+
}
1717

1818
if len(os.Args) < 2 {
1919
utils.PrintEnvVars(envVars)

internal/process/plain.go

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package process
1+
package provider
22

33
import (
44
"context"
@@ -10,9 +10,9 @@ import (
1010
"github.com/polarn/env-exec/internal/config"
1111
)
1212

13-
// Find GCP secret env vars and add them to the envVars map
14-
func EnvVarsGCP(config *config.RootConfig, envVars *map[string]string) {
15-
if !checkIfGCPSecretKeyRefExists(config) {
13+
// Provide fetches GCP secrets and adds them to the envVars map.
14+
func (p *GCPProvider) Provide(cfg *config.RootConfig, envVars map[string]string) {
15+
if !hasGCPSecrets(cfg) {
1616
return
1717
}
1818

@@ -23,7 +23,7 @@ func EnvVarsGCP(config *config.RootConfig, envVars *map[string]string) {
2323
}
2424
defer client.Close()
2525

26-
for _, env := range config.Env {
26+
for _, env := range cfg.Env {
2727
if env.ValueFrom.GCPSecretKeyRef.Name != "" {
2828
name := env.ValueFrom.GCPSecretKeyRef.Name
2929
version := env.ValueFrom.GCPSecretKeyRef.Version
@@ -33,11 +33,11 @@ func EnvVarsGCP(config *config.RootConfig, envVars *map[string]string) {
3333
version = "latest"
3434
}
3535

36-
if project == "" && config.Defaults.GCP.Project == "" {
36+
if project == "" && cfg.Defaults.GCP.Project == "" {
3737
log.Printf("Error: No GCP project found for secret '%s'", env.Name)
3838
continue
3939
} else if project == "" {
40-
project = config.Defaults.GCP.Project
40+
project = cfg.Defaults.GCP.Project
4141
}
4242

4343
reqName := fmt.Sprintf("projects/%s/secrets/%s/versions/%s", project, name, version)
@@ -50,13 +50,13 @@ func EnvVarsGCP(config *config.RootConfig, envVars *map[string]string) {
5050
continue
5151
}
5252

53-
(*envVars)[env.Name] = string(resp.Payload.Data)
53+
envVars[env.Name] = string(resp.Payload.Data)
5454
}
5555
}
5656
}
5757

58-
func checkIfGCPSecretKeyRefExists(config *config.RootConfig) bool {
59-
for _, env := range config.Env {
58+
func hasGCPSecrets(cfg *config.RootConfig) bool {
59+
for _, env := range cfg.Env {
6060
if env.ValueFrom.GCPSecretKeyRef.Name != "" {
6161
return true
6262
}
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package process
1+
package provider
22

33
import (
44
"encoding/json"
@@ -20,8 +20,9 @@ type GitlabVariable struct {
2020
EnvironmentScope string `json:"environment_scope"`
2121
}
2222

23-
func EnvVarsGitlab(config *config.RootConfig, envVars *map[string]string) {
24-
if !checkIfGitlabVariableKeyRefExists(config) {
23+
// Provide fetches GitLab variables and adds them to the envVars map.
24+
func (p *GitlabProvider) Provide(cfg *config.RootConfig, envVars map[string]string) {
25+
if !hasGitlabVariables(cfg) {
2526
return
2627
}
2728

@@ -30,7 +31,7 @@ func EnvVarsGitlab(config *config.RootConfig, envVars *map[string]string) {
3031
log.Fatal("GITLAB_TOKEN environment variable not set")
3132
}
3233

33-
for _, env := range config.Env {
34+
for _, env := range cfg.Env {
3435
if env.ValueFrom.GitlabVariableKeyRef.Key != "" {
3536
key := env.ValueFrom.GitlabVariableKeyRef.Key
3637
project := env.ValueFrom.GitlabVariableKeyRef.Project
@@ -46,13 +47,13 @@ func EnvVarsGitlab(config *config.RootConfig, envVars *map[string]string) {
4647
continue
4748
}
4849

49-
(*envVars)[env.Name] = value
50+
envVars[env.Name] = value
5051
}
5152
}
5253
}
5354

54-
func checkIfGitlabVariableKeyRefExists(config *config.RootConfig) bool {
55-
for _, env := range config.Env {
55+
func hasGitlabVariables(cfg *config.RootConfig) bool {
56+
for _, env := range cfg.Env {
5657
if env.ValueFrom.GitlabVariableKeyRef.Key != "" {
5758
return true
5859
}

internal/provider/plain.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package provider
2+
3+
import "github.com/polarn/env-exec/internal/config"
4+
5+
// Provide adds plain env vars to the envVars map.
6+
func (p *PlainProvider) Provide(cfg *config.RootConfig, envVars map[string]string) {
7+
for _, env := range cfg.Env {
8+
if env.Value != "" {
9+
envVars[env.Name] = env.Value
10+
}
11+
}
12+
}

internal/provider/provider.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package provider
2+
3+
import "github.com/polarn/env-exec/internal/config"
4+
5+
// Provider defines the interface for environment variable providers.
6+
type Provider interface {
7+
Provide(config *config.RootConfig, envVars map[string]string)
8+
}
9+
10+
// PlainProvider provides static environment variables.
11+
type PlainProvider struct{}
12+
13+
// GCPProvider provides environment variables from GCP Secret Manager.
14+
type GCPProvider struct{}
15+
16+
// GitlabProvider provides environment variables from GitLab CI/CD variables.
17+
type GitlabProvider struct{}
18+
19+
// AllProviders returns all available providers in execution order.
20+
func AllProviders() []Provider {
21+
return []Provider{
22+
&PlainProvider{},
23+
&GCPProvider{},
24+
&GitlabProvider{},
25+
}
26+
}

0 commit comments

Comments
 (0)