Skip to content

Commit de92b7e

Browse files
committed
Added code to get env vars from project credentials struct
1 parent bf54870 commit de92b7e

File tree

8 files changed

+71
-55
lines changed

8 files changed

+71
-55
lines changed

cmd/apply_test.go

Lines changed: 0 additions & 11 deletions
This file was deleted.

cmd/init_test.go

Lines changed: 0 additions & 30 deletions
This file was deleted.

internal/apply/apply.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/commitdev/zero/internal/module"
1313
"github.com/commitdev/zero/internal/util"
1414

15+
"github.com/commitdev/zero/internal/config/globalconfig"
1516
"github.com/commitdev/zero/internal/config/projectconfig"
1617
"github.com/commitdev/zero/pkg/util/exit"
1718
"github.com/commitdev/zero/pkg/util/flog"
@@ -52,8 +53,11 @@ func applyAll(dir string, projectConfig projectconfig.ZeroProjectConfig, applyEn
5253
environmentArg := fmt.Sprintf("ENVIRONMENT=%s", strings.Join(applyEnvironments, ","))
5354

5455
for _, mod := range projectConfig.Modules {
55-
dirArg := fmt.Sprintf("PROJECT_DIR=%s", path.Join(dir, mod.Files.Directory))
56-
envList := []string{environmentArg, dirArg}
56+
envList := []string{
57+
environmentArg,
58+
fmt.Sprintf("PROJECT_DIR=%s", path.Join(dir, mod.Files.Directory)),
59+
fmt.Sprintf("REPOSITORY=%s", mod.Files.Repository),
60+
}
5761

5862
modulePath := module.GetSourceDir(mod.Files.Source)
5963
// Passed in `dir` will only be used to find the project path, not the module path,
@@ -62,7 +66,10 @@ func applyAll(dir string, projectConfig projectconfig.ZeroProjectConfig, applyEn
6266
modulePath = filepath.Join(dir, modulePath)
6367
}
6468

69+
credentials := globalconfig.GetProjectCredentials(projectConfig.Name)
70+
6571
envList = util.AppendProjectEnvToCmdEnv(mod.Parameters, envList)
72+
envList = util.AppendProjectEnvToCmdEnv(credentials.AsEnvVars(), envList)
6673
util.ExecuteCommand(exec.Command("make"), modulePath, envList)
6774
}
6875
}

internal/apply/apply_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ func TestApply(t *testing.T) {
3232

3333
content, err := ioutil.ReadFile(filepath.Join(tmpDir, "project1/project.out"))
3434
assert.NoError(t, err)
35-
assert.Equal(t, string(content), "foo: bar\n")
35+
assert.Equal(t, "foo: bar\nrepo: github.com/commitdev/project1\n", string(content))
3636

3737
content, err = ioutil.ReadFile(filepath.Join(tmpDir, "project2/project.out"))
3838
assert.NoError(t, err)
39-
assert.Equal(t, string(content), "baz: qux\n")
39+
assert.Equal(t, "baz: qux\n", string(content))
4040
})
4141
}

internal/config/globalconfig/global_config.go

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"os/user"
99
"path"
10+
"reflect"
1011

1112
"github.com/commitdev/zero/internal/constants"
1213
"github.com/commitdev/zero/pkg/util/exit"
@@ -25,14 +26,14 @@ type ProjectCredential struct {
2526
}
2627

2728
type AWSResourceConfig struct {
28-
AccessKeyId string `yaml:"accessKeyId,omitempty"`
29-
SecretAccessKey string `yaml:"secretAccessKey,omitempty"`
29+
AccessKeyID string `yaml:"accessKeyId,omitempty" env:"AWS_ACCESS_KEY_ID"`
30+
SecretAccessKey string `yaml:"secretAccessKey,omitempty" env:"AWS_SECRET_ACCESS_KEY"`
3031
}
3132
type GithubResourceConfig struct {
32-
AccessToken string `yaml:"accessToken,omitempty"`
33+
AccessToken string `yaml:"accessToken,omitempty" env:"GITHUB_ACCESS_TOKEN"`
3334
}
3435
type CircleCiResourceConfig struct {
35-
ApiKey string `yaml:"apiKey,omitempty"`
36+
ApiKey string `yaml:"apiKey,omitempty" env:"CIRCLECI_API_KEY"`
3637
}
3738

3839
func (p ProjectCredentials) Unmarshal(data []byte) error {
@@ -50,6 +51,35 @@ func (p ProjectCredentials) Unmarshal(data []byte) error {
5051
return nil
5152
}
5253

54+
// AsEnvVars marshals ProjectCredential as a map of key/value strings suitable for environment variables
55+
func (p ProjectCredential) AsEnvVars() map[string]string {
56+
t := reflect.ValueOf(p)
57+
58+
list := make(map[string]string)
59+
list = gatherFieldTags(t, list)
60+
61+
return list
62+
}
63+
64+
func gatherFieldTags(t reflect.Value, list map[string]string) map[string]string {
65+
reflectType := t.Type()
66+
67+
for i := 0; i < t.NumField(); i++ {
68+
fieldValue := t.Field(i)
69+
fieldType := reflectType.Field(i)
70+
71+
if fieldType.Type.Kind() == reflect.Struct {
72+
list = gatherFieldTags(fieldValue, list)
73+
continue
74+
}
75+
76+
if env := fieldType.Tag.Get("env"); env != "" {
77+
list[env] = fieldValue.String()
78+
}
79+
}
80+
return list
81+
}
82+
5383
func LoadUserCredentials() ProjectCredentials {
5484
data := readOrCreateUserCredentialsFile()
5585

internal/config/globalconfig/global_config_test.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func TestGetUserCredentials(t *testing.T) {
6666
project := globalconfig.GetProjectCredentials(projectName)
6767

6868
// Reading from fixtures: tests/test_data/configs/credentials.yml
69-
assert.Equal(t, "AKIAABCD", project.AWSResourceConfig.AccessKeyId)
69+
assert.Equal(t, "AKIAABCD", project.AWSResourceConfig.AccessKeyID)
7070
assert.Equal(t, "ZXCV", project.AWSResourceConfig.SecretAccessKey)
7171
assert.Equal(t, "0987", project.GithubResourceConfig.AccessToken)
7272
assert.Equal(t, "SOME_API_KEY", project.CircleCiResourceConfig.ApiKey)
@@ -87,17 +87,36 @@ func TestEditUserCredentials(t *testing.T) {
8787
t.Run("Should create new project if not exist", func(t *testing.T) {
8888
projectName := "test-project3"
8989
project := globalconfig.GetProjectCredentials(projectName)
90-
project.AWSResourceConfig.AccessKeyId = "TEST_KEY_ID_1"
90+
project.AWSResourceConfig.AccessKeyID = "TEST_KEY_ID_1"
9191
globalconfig.Save(project)
92-
newKeyID := globalconfig.GetProjectCredentials(projectName).AWSResourceConfig.AccessKeyId
92+
newKeyID := globalconfig.GetProjectCredentials(projectName).AWSResourceConfig.AccessKeyID
9393
assert.Equal(t, "TEST_KEY_ID_1", newKeyID)
9494
})
9595
t.Run("Should edit old project if already exist", func(t *testing.T) {
9696
projectName := "my-project"
9797
project := globalconfig.GetProjectCredentials(projectName)
98-
project.AWSResourceConfig.AccessKeyId = "EDITED_ACCESS_KEY_ID"
98+
project.AWSResourceConfig.AccessKeyID = "EDITED_ACCESS_KEY_ID"
9999
globalconfig.Save(project)
100-
newKeyID := globalconfig.GetProjectCredentials(projectName).AWSResourceConfig.AccessKeyId
100+
newKeyID := globalconfig.GetProjectCredentials(projectName).AWSResourceConfig.AccessKeyID
101101
assert.Equal(t, "EDITED_ACCESS_KEY_ID", newKeyID)
102102
})
103103
}
104+
105+
func TestMarshalProjectCredentialAsEnvVars(t *testing.T) {
106+
t.Run("Should be able to marshal a ProjectCredential into env vars", func(t *testing.T) {
107+
pc := globalconfig.ProjectCredential{
108+
AWSResourceConfig: globalconfig.AWSResourceConfig{
109+
AccessKeyID: "AKID",
110+
SecretAccessKey: "SAK",
111+
},
112+
CircleCiResourceConfig: globalconfig.CircleCiResourceConfig{
113+
ApiKey: "APIKEY",
114+
},
115+
}
116+
117+
envVars := pc.AsEnvVars()
118+
assert.Equal(t, "AKID", envVars["AWS_ACCESS_KEY_ID"])
119+
assert.Equal(t, "SAK", envVars["AWS_SECRET_ACCESS_KEY"])
120+
assert.Equal(t, "APIKEY", envVars["CIRCLECI_API_KEY"])
121+
})
122+
}

internal/init/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ func mapVendorToPrompts(projectCred globalconfig.ProjectCredential, vendor strin
239239
moduleconfig.Parameter{
240240
Field: "accessKeyId",
241241
Label: "AWS Access Key ID",
242-
Default: projectCred.AWSResourceConfig.AccessKeyId,
242+
Default: projectCred.AWSResourceConfig.AccessKeyID,
243243
},
244244
CustomCondition(customAwsMustInputCondition),
245245
project.ValidateAKID,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
current_dir:
22
@echo "foo: ${foo}" > project.out
3+
@echo "repo: ${REPOSITORY}" >> project.out

0 commit comments

Comments
 (0)