Skip to content

Commit a9b3b0e

Browse files
authored
Merge pull request #146 from commitdev/reorganize-config-code
Reorganize config code by moving different config types into better-n…
2 parents a96e4c0 + 71695cd commit a9b3b0e

File tree

18 files changed

+110
-106
lines changed

18 files changed

+110
-106
lines changed

cmd/apply.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"fmt"
55
"log"
66

7-
"github.com/commitdev/zero/configs"
7+
"github.com/commitdev/zero/internal/constants"
88
"github.com/commitdev/zero/pkg/util/exit"
99
"github.com/manifoldco/promptui"
1010
"github.com/spf13/cobra"
@@ -14,7 +14,7 @@ var applyConfigPath string
1414
var applyEnvironments []string
1515

1616
func init() {
17-
applyCmd.PersistentFlags().StringVarP(&applyConfigPath, "config", "c", configs.ZeroProjectYml, "config path")
17+
applyCmd.PersistentFlags().StringVarP(&applyConfigPath, "config", "c", constants.ZeroProjectYml, "config path")
1818
applyCmd.PersistentFlags().StringSliceVarP(&applyEnvironments, "env", "e", []string{}, "environments to set up (staging, production) - specify multiple times for multiple")
1919

2020
rootCmd.AddCommand(applyCmd)

cmd/init.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package cmd
22

33
import (
4-
"github.com/commitdev/zero/internal/config"
4+
"github.com/commitdev/zero/internal/config/projectconfig"
55
"github.com/commitdev/zero/internal/context"
66
"github.com/commitdev/zero/pkg/util/exit"
77
"github.com/spf13/cobra"
@@ -20,7 +20,7 @@ var initCmd = &cobra.Command{
2020
}
2121

2222
projectName := args[0]
23-
projectContext := context.Init(projectName, config.RootDir)
24-
config.Init(config.RootDir, projectName, projectContext)
23+
projectContext := context.Init(projectName, projectconfig.RootDir)
24+
projectconfig.Init(projectconfig.RootDir, projectName, projectContext)
2525
},
2626
}

configs/configs.go

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

internal/api/generate_api.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"log"
66
"net/http"
77

8-
"github.com/commitdev/zero/internal/config"
8+
"github.com/commitdev/zero/internal/config/projectconfig"
99
"github.com/gorilla/handlers"
1010
"github.com/gorilla/mux"
1111
)
@@ -15,13 +15,13 @@ func generateProject(w http.ResponseWriter, req *http.Request) {
1515
switch req.Method {
1616
case "POST":
1717
decoder := json.NewDecoder(req.Body)
18-
var projectConfig config.ZeroProjectConfig
19-
err := decoder.Decode(&projectConfig)
18+
var config projectconfig.ZeroProjectConfig
19+
err := decoder.Decode(&config)
2020
if err != nil {
2121
panic(err)
2222
}
23-
log.Println(projectConfig.Name)
24-
// createProject(projectConfig)
23+
log.Println(config.Name)
24+
// createProject(config)
2525
w.WriteHeader(http.StatusCreated)
2626
w.Write([]byte(`{"message": "Post successful"}`))
2727

internal/config/global_config.go renamed to internal/config/globalconfig/global_config.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package config
1+
package globalconfig
22

33
import (
44
"bytes"
@@ -8,7 +8,7 @@ import (
88
"os/user"
99
"path"
1010

11-
"github.com/commitdev/zero/configs"
11+
"github.com/commitdev/zero/internal/constants"
1212
"github.com/commitdev/zero/pkg/util/exit"
1313
yaml "gopkg.in/yaml.v2"
1414
)
@@ -69,9 +69,9 @@ func getCredentialsPath() string {
6969
exit.Fatal("Failed to get user directory path: %v", err)
7070
}
7171

72-
rootDir := path.Join(usr.HomeDir, configs.ZeroHomeDirectory)
72+
rootDir := path.Join(usr.HomeDir, constants.ZeroHomeDirectory)
7373
os.MkdirAll(rootDir, os.ModePerm)
74-
filePath := path.Join(rootDir, configs.UserCredentials)
74+
filePath := path.Join(rootDir, constants.UserCredentialsYml)
7575
return filePath
7676
}
7777

internal/config/global_config_test.go renamed to internal/config/globalconfig/global_config_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package config_test
1+
package globalconfig_test
22

33
import (
44
"fmt"
@@ -8,11 +8,11 @@ import (
88
"path"
99
"testing"
1010

11-
"github.com/commitdev/zero/internal/config"
11+
"github.com/commitdev/zero/internal/config/globalconfig"
1212
"github.com/stretchr/testify/assert"
1313
)
1414

15-
const baseTestFixturesDir = "../../tests/test_data/configs/"
15+
const baseTestFixturesDir = "../../../tests/test_data/configs/"
1616

1717
var testCredentialFile = func() (func() string, func()) {
1818
tmpConfigPath := getTmpConfig()
@@ -40,16 +40,16 @@ func copyFile(from string, to string) {
4040
}
4141
}
4242
func TestReadOrCreateUserCredentialsFile(t *testing.T) {
43-
config.GetCredentialsPath = func() string {
43+
globalconfig.GetCredentialsPath = func() string {
4444
return path.Join(baseTestFixturesDir, "does-not-exist.yml")
4545
}
46-
credPath := config.GetCredentialsPath()
46+
credPath := globalconfig.GetCredentialsPath()
4747

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

5454
stats, err := os.Stat(credPath)
5555
assert.False(t, os.IsNotExist(err), "File should be created")
@@ -58,12 +58,12 @@ func TestReadOrCreateUserCredentialsFile(t *testing.T) {
5858

5959
func TestGetUserCredentials(t *testing.T) {
6060
var teardownFn func()
61-
config.GetCredentialsPath, teardownFn = testCredentialFile()
61+
globalconfig.GetCredentialsPath, teardownFn = testCredentialFile()
6262
defer teardownFn()
6363

6464
t.Run("Fixture file should have existing project with creds", func(t *testing.T) {
6565
projectName := "my-project"
66-
project := config.GetUserCredentials(projectName)
66+
project := globalconfig.GetUserCredentials(projectName)
6767

6868
// Reading from fixtures: tests/test_data/configs/credentials.yml
6969
assert.Equal(t, "AKIAABCD", project.AWSResourceConfig.AccessKeyId)
@@ -74,30 +74,30 @@ func TestGetUserCredentials(t *testing.T) {
7474

7575
t.Run("Fixture file should support multiple projects", func(t *testing.T) {
7676
projectName := "another-project"
77-
project := config.GetUserCredentials(projectName)
77+
project := globalconfig.GetUserCredentials(projectName)
7878
assert.Equal(t, "654", project.GithubResourceConfig.AccessToken)
7979
})
8080
}
8181

8282
func TestEditUserCredentials(t *testing.T) {
8383
var teardownFn func()
84-
config.GetCredentialsPath, teardownFn = testCredentialFile()
84+
globalconfig.GetCredentialsPath, teardownFn = testCredentialFile()
8585
defer teardownFn()
8686

8787
t.Run("Should create new project if not exist", func(t *testing.T) {
8888
projectName := "test-project3"
89-
project := config.GetUserCredentials(projectName)
89+
project := globalconfig.GetUserCredentials(projectName)
9090
project.AWSResourceConfig.AccessKeyId = "TEST_KEY_ID_1"
91-
config.Save(project)
92-
newKeyID := config.GetUserCredentials(projectName).AWSResourceConfig.AccessKeyId
91+
globalconfig.Save(project)
92+
newKeyID := globalconfig.GetUserCredentials(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"
97-
project := config.GetUserCredentials(projectName)
97+
project := globalconfig.GetUserCredentials(projectName)
9898
project.AWSResourceConfig.AccessKeyId = "EDITED_ACCESS_KEY_ID"
99-
config.Save(project)
100-
newKeyID := config.GetUserCredentials(projectName).AWSResourceConfig.AccessKeyId
99+
globalconfig.Save(project)
100+
newKeyID := globalconfig.GetUserCredentials(projectName).AWSResourceConfig.AccessKeyId
101101
assert.Equal(t, "EDITED_ACCESS_KEY_ID", newKeyID)
102102
})
103103
}

internal/config/init_test.go

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

internal/config/module_config.go renamed to internal/config/moduleconfig/module_config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package config
1+
package moduleconfig
22

33
import (
44
"io/ioutil"
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
package config
1+
package projectconfig
22

33
import (
44
"fmt"
55
"io/ioutil"
66
"path"
77

8-
"github.com/commitdev/zero/configs"
8+
"github.com/commitdev/zero/internal/constants"
99
"github.com/commitdev/zero/pkg/util/exit"
1010
)
1111

@@ -36,8 +36,8 @@ func Init(dir string, projectName string, projectContext *ZeroProjectConfig) {
3636
// TODO: template the zero-project.yml with projectContext
3737
content := []byte(fmt.Sprintf(exampleConfig, projectName))
3838

39-
err := ioutil.WriteFile(path.Join(dir, projectName, configs.ZeroProjectYml), content, 0644)
39+
err := ioutil.WriteFile(path.Join(dir, projectName, constants.ZeroProjectYml), content, 0644)
4040
if err != nil {
41-
exit.Fatal(fmt.Sprintf("Failed to create example %s", configs.ZeroProjectYml))
41+
exit.Fatal(fmt.Sprintf("Failed to create example %s", constants.ZeroProjectYml))
4242
}
4343
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package projectconfig_test
2+
3+
import (
4+
"os"
5+
"path"
6+
"testing"
7+
8+
"github.com/commitdev/zero/internal/config/projectconfig"
9+
"github.com/commitdev/zero/internal/constants"
10+
)
11+
12+
func TestInit(t *testing.T) {
13+
const testDir = "../../test-sandbox"
14+
projectName := "test-project"
15+
16+
projectconfig.SetRootDir(testDir)
17+
defer os.RemoveAll(testDir)
18+
19+
testDirPath := path.Join(projectconfig.RootDir, projectName)
20+
// create sandbox dir
21+
err := os.MkdirAll(testDirPath, os.ModePerm)
22+
if err != nil {
23+
t.Fatal(err)
24+
}
25+
26+
config := projectconfig.ZeroProjectConfig{}
27+
projectconfig.Init(projectconfig.RootDir, projectName, &config)
28+
29+
if _, err := os.Stat(path.Join(testDirPath, constants.ZeroProjectYml)); err != nil {
30+
t.Fatal(err)
31+
}
32+
}

0 commit comments

Comments
 (0)