Skip to content

Commit

Permalink
fix oop approach and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcheung committed Jun 2, 2020
1 parent f931151 commit ee91d85
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
31 changes: 18 additions & 13 deletions internal/config/global_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

var GetCredentialsPath = getCredentialsPath

type ProjectCredentials map[string]*ProjectCredential
type ProjectCredentials map[string]ProjectCredential

type ProjectCredential struct {
ProjectName string `yaml:"-"`
Expand All @@ -35,21 +35,26 @@ type CircleCiResourceConfig struct {
ApiKey string `yaml:"apiKey,omitempty"`
}

func (p *ProjectCredentials) Unmarshal(data []byte) error {
func (p ProjectCredentials) Unmarshal(data []byte) error {
if len(data) == 0 {
return nil
}
err := yaml.NewDecoder(bytes.NewReader(data)).Decode(p)
if err != nil {
return err
}
for k, v := range *p {
for k, v := range p {
v.ProjectName = k
p[k] = v
}
return nil
}

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

projects := ProjectCredentials{}

err := projects.Unmarshal(data)

if err != nil {
Expand All @@ -70,7 +75,7 @@ func getCredentialsPath() string {
return filePath
}

func ReadOrCreateUserCredentialsFile() []byte {
func readOrCreateUserCredentialsFile() []byte {
credPath := GetCredentialsPath()

_, fileStateErr := os.Stat(credPath)
Expand All @@ -88,7 +93,7 @@ func ReadOrCreateUserCredentialsFile() []byte {
return data
}

func GetUserCredentials(targetProjectName string) *ProjectCredential {
func GetUserCredentials(targetProjectName string) ProjectCredential {
projects := LoadUserCredentials()

if val, ok := projects[targetProjectName]; ok {
Expand All @@ -97,22 +102,22 @@ func GetUserCredentials(targetProjectName string) *ProjectCredential {
p := ProjectCredential{
ProjectName: targetProjectName,
}
projects[targetProjectName] = &p
return &p
projects[targetProjectName] = p
return p
}
}

func (project *ProjectCredential) Save() {
func Save(project ProjectCredential) {
projects := LoadUserCredentials()
projects[project.ProjectName] = project
projects.Save()
RewriteCredentialsFile(projects)
}

func (projects *ProjectCredentials) Save() {
func RewriteCredentialsFile(projects ProjectCredentials) {
credsPath := GetCredentialsPath()
content, _ := yaml.Marshal(&projects)
content, _ := yaml.Marshal(projects)
err := ioutil.WriteFile(credsPath, content, 0644)
if err != nil {
log.Panicf("failed to parse config: %v", err)
log.Panicf("failed to write config: %v", err)
}
}
11 changes: 6 additions & 5 deletions internal/config/global_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,16 @@ func copyFile(from string, to string) {
}
func TestReadOrCreateUserCredentialsFile(t *testing.T) {
config.GetCredentialsPath = func() string {
return path.Join("../../tests/test_data/", "configs/does-not-exist.yml")
return path.Join(baseTestFixturesDir, "does-not-exist.yml")
}
credPath := config.GetCredentialsPath()
defer os.RemoveAll(credPath)

defer os.RemoveAll(credPath)
_, fileStateErr := os.Stat(credPath)
assert.True(t, os.IsNotExist(fileStateErr), "File should not exist")
// attempting to read the file should create the file
config.GetUserCredentials("any-project")

config.ReadOrCreateUserCredentialsFile()
stats, err := os.Stat(credPath)
assert.False(t, os.IsNotExist(err), "File should be created")
assert.Equal(t, "does-not-exist.yml", stats.Name(), "Should create yml automatically")
Expand Down Expand Up @@ -88,15 +89,15 @@ func TestEditUserCredentials(t *testing.T) {
projectName := "test-project3"
project := config.GetUserCredentials(projectName)
project.AWSResourceConfig.AccessKeyId = "TEST_KEY_ID_1"
project.Save()
config.Save(project)
newKeyID := config.GetUserCredentials(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 := config.GetUserCredentials(projectName)
project.AWSResourceConfig.AccessKeyId = "EDITED_ACCESS_KEY_ID"
project.Save()
config.Save(project)
newKeyID := config.GetUserCredentials(projectName).AWSResourceConfig.AccessKeyId
assert.Equal(t, "EDITED_ACCESS_KEY_ID", newKeyID)
})
Expand Down
2 changes: 1 addition & 1 deletion internal/context/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func promptGithubPersonalToken(projectName string) string {
// If its different from saved token, update it
if project.GithubResourceConfig.AccessToken != result {
project.GithubResourceConfig.AccessToken = result
project.Save()
config.Save(project)
}
return result
}
Expand Down

0 comments on commit ee91d85

Please sign in to comment.