-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Integrate apply command #175
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bf54870
Refactoring and integration work on apply command
bmonkman de92b7e
Added code to get env vars from project credentials struct
bmonkman 0665b3f
Fixed caps
bmonkman 4f37973
Some cleanup around prompts / credentials
bmonkman 677667d
Mod tidy
bmonkman 46cb194
Use only parent env vars when executing if no others provided
bmonkman d86950a
Merge branch 'master' into integrate-apply-command2
bmonkman 23a4af5
Fixed file name
bmonkman 682d8d0
Removed stray context from test data
bmonkman File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,41 @@ | ||
package apply_test | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/commitdev/zero/internal/apply" | ||
"github.com/commitdev/zero/internal/constants" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/termie/go-shutil" | ||
) | ||
|
||
func TestApply(t *testing.T) { | ||
// @TODO is there a way to do this without relative paths? | ||
dir := "../../tests/test_data/sample_project/" | ||
dir := "../../tests/test_data/apply/" | ||
applyConfigPath := constants.ZeroProjectYml | ||
applyEnvironments := []string{"staging", "production"} | ||
|
||
want := []string{ | ||
"make module zero-aws-eks-stack\n", | ||
"make module zero-deployable-backend\n", | ||
"make module zero-deployable-react-frontend\n", | ||
} | ||
tmpDir := filepath.Join(os.TempDir(), "apply") | ||
|
||
err := os.RemoveAll(tmpDir) | ||
assert.NoError(t, err) | ||
|
||
err = shutil.CopyTree(dir, tmpDir, nil) | ||
assert.NoError(t, err) | ||
|
||
t.Run("Should run apply and execute make on each folder module", func(t *testing.T) { | ||
got := apply.Apply(dir, applyConfigPath, applyEnvironments) | ||
assert.ElementsMatch(t, want, got) | ||
apply.Apply(tmpDir, applyConfigPath, applyEnvironments) | ||
assert.FileExists(t, filepath.Join(tmpDir, "project1/project.out")) | ||
assert.FileExists(t, filepath.Join(tmpDir, "project2/project.out")) | ||
|
||
content, err := ioutil.ReadFile(filepath.Join(tmpDir, "project1/project.out")) | ||
assert.NoError(t, err) | ||
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, "baz: qux\n", string(content)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import ( | |
"os" | ||
"os/user" | ||
"path" | ||
"reflect" | ||
|
||
"github.com/commitdev/zero/internal/constants" | ||
"github.com/commitdev/zero/pkg/util/exit" | ||
|
@@ -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 { | ||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @davidcheung This should help with #173 |
||
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() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be better to return the error in this case and let the caller throw the error for consistency like
init
in #130?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nvm, I just realized your comment was specific to if I wanted to test the error case so I guess that doesn't apply here because it's not a standard we're enforcing.