Skip to content

Commit

Permalink
Added code to get env vars from project credentials struct
Browse files Browse the repository at this point in the history
  • Loading branch information
bmonkman committed Jun 23, 2020
1 parent bf54870 commit de92b7e
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 55 deletions.
11 changes: 0 additions & 11 deletions cmd/apply_test.go

This file was deleted.

30 changes: 0 additions & 30 deletions cmd/init_test.go

This file was deleted.

11 changes: 9 additions & 2 deletions internal/apply/apply.go
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/commitdev/zero/internal/module"
"github.com/commitdev/zero/internal/util"

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

for _, mod := range projectConfig.Modules {
dirArg := fmt.Sprintf("PROJECT_DIR=%s", path.Join(dir, mod.Files.Directory))
envList := []string{environmentArg, dirArg}
envList := []string{
environmentArg,
fmt.Sprintf("PROJECT_DIR=%s", path.Join(dir, mod.Files.Directory)),
fmt.Sprintf("REPOSITORY=%s", mod.Files.Repository),
}

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

credentials := globalconfig.GetProjectCredentials(projectConfig.Name)

envList = util.AppendProjectEnvToCmdEnv(mod.Parameters, envList)
envList = util.AppendProjectEnvToCmdEnv(credentials.AsEnvVars(), envList)
util.ExecuteCommand(exec.Command("make"), modulePath, envList)
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/apply/apply_test.go
Expand Up @@ -32,10 +32,10 @@ func TestApply(t *testing.T) {

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

content, err = ioutil.ReadFile(filepath.Join(tmpDir, "project2/project.out"))
assert.NoError(t, err)
assert.Equal(t, string(content), "baz: qux\n")
assert.Equal(t, "baz: qux\n", string(content))
})
}
38 changes: 34 additions & 4 deletions internal/config/globalconfig/global_config.go
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"os/user"
"path"
"reflect"

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

type AWSResourceConfig struct {
AccessKeyId string `yaml:"accessKeyId,omitempty"`
SecretAccessKey string `yaml:"secretAccessKey,omitempty"`
AccessKeyID string `yaml:"accessKeyId,omitempty" env:"AWS_ACCESS_KEY_ID"`
SecretAccessKey string `yaml:"secretAccessKey,omitempty" env:"AWS_SECRET_ACCESS_KEY"`
}
type GithubResourceConfig struct {
AccessToken string `yaml:"accessToken,omitempty"`
AccessToken string `yaml:"accessToken,omitempty" env:"GITHUB_ACCESS_TOKEN"`
}
type CircleCiResourceConfig struct {
ApiKey string `yaml:"apiKey,omitempty"`
ApiKey string `yaml:"apiKey,omitempty" env:"CIRCLECI_API_KEY"`
}

func (p ProjectCredentials) Unmarshal(data []byte) error {
Expand All @@ -50,6 +51,35 @@ func (p ProjectCredentials) Unmarshal(data []byte) error {
return nil
}

// AsEnvVars marshals ProjectCredential as a map of key/value strings suitable for environment variables
func (p ProjectCredential) AsEnvVars() map[string]string {
t := reflect.ValueOf(p)

list := make(map[string]string)
list = gatherFieldTags(t, list)

return list
}

func gatherFieldTags(t reflect.Value, list map[string]string) map[string]string {
reflectType := t.Type()

for i := 0; i < t.NumField(); i++ {
fieldValue := t.Field(i)
fieldType := reflectType.Field(i)

if fieldType.Type.Kind() == reflect.Struct {
list = gatherFieldTags(fieldValue, list)
continue
}

if env := fieldType.Tag.Get("env"); env != "" {
list[env] = fieldValue.String()
}
}
return list
}

func LoadUserCredentials() ProjectCredentials {
data := readOrCreateUserCredentialsFile()

Expand Down
29 changes: 24 additions & 5 deletions internal/config/globalconfig/global_config_test.go
Expand Up @@ -66,7 +66,7 @@ func TestGetUserCredentials(t *testing.T) {
project := globalconfig.GetProjectCredentials(projectName)

// Reading from fixtures: tests/test_data/configs/credentials.yml
assert.Equal(t, "AKIAABCD", project.AWSResourceConfig.AccessKeyId)
assert.Equal(t, "AKIAABCD", project.AWSResourceConfig.AccessKeyID)
assert.Equal(t, "ZXCV", project.AWSResourceConfig.SecretAccessKey)
assert.Equal(t, "0987", project.GithubResourceConfig.AccessToken)
assert.Equal(t, "SOME_API_KEY", project.CircleCiResourceConfig.ApiKey)
Expand All @@ -87,17 +87,36 @@ func TestEditUserCredentials(t *testing.T) {
t.Run("Should create new project if not exist", func(t *testing.T) {
projectName := "test-project3"
project := globalconfig.GetProjectCredentials(projectName)
project.AWSResourceConfig.AccessKeyId = "TEST_KEY_ID_1"
project.AWSResourceConfig.AccessKeyID = "TEST_KEY_ID_1"
globalconfig.Save(project)
newKeyID := globalconfig.GetProjectCredentials(projectName).AWSResourceConfig.AccessKeyId
newKeyID := globalconfig.GetProjectCredentials(projectName).AWSResourceConfig.AccessKeyID
assert.Equal(t, "TEST_KEY_ID_1", newKeyID)
})
t.Run("Should edit old project if already exist", func(t *testing.T) {
projectName := "my-project"
project := globalconfig.GetProjectCredentials(projectName)
project.AWSResourceConfig.AccessKeyId = "EDITED_ACCESS_KEY_ID"
project.AWSResourceConfig.AccessKeyID = "EDITED_ACCESS_KEY_ID"
globalconfig.Save(project)
newKeyID := globalconfig.GetProjectCredentials(projectName).AWSResourceConfig.AccessKeyId
newKeyID := globalconfig.GetProjectCredentials(projectName).AWSResourceConfig.AccessKeyID
assert.Equal(t, "EDITED_ACCESS_KEY_ID", newKeyID)
})
}

func TestMarshalProjectCredentialAsEnvVars(t *testing.T) {
t.Run("Should be able to marshal a ProjectCredential into env vars", func(t *testing.T) {
pc := globalconfig.ProjectCredential{
AWSResourceConfig: globalconfig.AWSResourceConfig{
AccessKeyID: "AKID",
SecretAccessKey: "SAK",
},
CircleCiResourceConfig: globalconfig.CircleCiResourceConfig{
ApiKey: "APIKEY",
},
}

envVars := pc.AsEnvVars()
assert.Equal(t, "AKID", envVars["AWS_ACCESS_KEY_ID"])
assert.Equal(t, "SAK", envVars["AWS_SECRET_ACCESS_KEY"])
assert.Equal(t, "APIKEY", envVars["CIRCLECI_API_KEY"])
})
}
2 changes: 1 addition & 1 deletion internal/init/init.go
Expand Up @@ -239,7 +239,7 @@ func mapVendorToPrompts(projectCred globalconfig.ProjectCredential, vendor strin
moduleconfig.Parameter{
Field: "accessKeyId",
Label: "AWS Access Key ID",
Default: projectCred.AWSResourceConfig.AccessKeyId,
Default: projectCred.AWSResourceConfig.AccessKeyID,
},
CustomCondition(customAwsMustInputCondition),
project.ValidateAKID,
Expand Down
1 change: 1 addition & 0 deletions tests/test_data/apply/project1/Makefile
@@ -1,2 +1,3 @@
current_dir:
@echo "foo: ${foo}" > project.out
@echo "repo: ${REPOSITORY}" >> project.out

0 comments on commit de92b7e

Please sign in to comment.