Skip to content
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

Reorganize config code by moving different config types into better-n… #146

Merged
merged 2 commits into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"log"

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

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

rootCmd.AddCommand(applyCmd)
Expand Down
6 changes: 3 additions & 3 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cmd

import (
"github.com/commitdev/zero/internal/config"
"github.com/commitdev/zero/internal/config/projectconfig"
"github.com/commitdev/zero/internal/context"
"github.com/commitdev/zero/pkg/util/exit"
"github.com/spf13/cobra"
Expand All @@ -20,7 +20,7 @@ var initCmd = &cobra.Command{
}

projectName := args[0]
projectContext := context.Init(projectName, config.RootDir)
config.Init(config.RootDir, projectName, projectContext)
projectContext := context.Init(projectName, projectconfig.RootDir)
projectconfig.Init(projectconfig.RootDir, projectName, projectContext)
},
}
10 changes: 0 additions & 10 deletions configs/configs.go

This file was deleted.

10 changes: 5 additions & 5 deletions internal/api/generate_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"net/http"

"github.com/commitdev/zero/internal/config"
"github.com/commitdev/zero/internal/config/projectconfig"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
)
Expand All @@ -15,13 +15,13 @@ func generateProject(w http.ResponseWriter, req *http.Request) {
switch req.Method {
case "POST":
decoder := json.NewDecoder(req.Body)
var projectConfig config.ZeroProjectConfig
err := decoder.Decode(&projectConfig)
var config projectconfig.ZeroProjectConfig
err := decoder.Decode(&config)
if err != nil {
panic(err)
}
log.Println(projectConfig.Name)
// createProject(projectConfig)
log.Println(config.Name)
// createProject(config)
w.WriteHeader(http.StatusCreated)
w.Write([]byte(`{"message": "Post successful"}`))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package config
package globalconfig

import (
"bytes"
Expand All @@ -8,7 +8,7 @@ import (
"os/user"
"path"

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

rootDir := path.Join(usr.HomeDir, configs.ZeroHomeDirectory)
rootDir := path.Join(usr.HomeDir, constants.ZeroHomeDirectory)
os.MkdirAll(rootDir, os.ModePerm)
filePath := path.Join(rootDir, configs.UserCredentials)
filePath := path.Join(rootDir, constants.UserCredentialsYml)
return filePath
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package config_test
package globalconfig_test

import (
"fmt"
Expand All @@ -8,11 +8,11 @@ import (
"path"
"testing"

"github.com/commitdev/zero/internal/config"
"github.com/commitdev/zero/internal/config/globalconfig"
"github.com/stretchr/testify/assert"
)

const baseTestFixturesDir = "../../tests/test_data/configs/"
const baseTestFixturesDir = "../../../tests/test_data/configs/"

var testCredentialFile = func() (func() string, func()) {
tmpConfigPath := getTmpConfig()
Expand Down Expand Up @@ -40,16 +40,16 @@ func copyFile(from string, to string) {
}
}
func TestReadOrCreateUserCredentialsFile(t *testing.T) {
config.GetCredentialsPath = func() string {
globalconfig.GetCredentialsPath = func() string {
return path.Join(baseTestFixturesDir, "does-not-exist.yml")
}
credPath := config.GetCredentialsPath()
credPath := globalconfig.GetCredentialsPath()

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")
globalconfig.GetUserCredentials("any-project")

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

func TestGetUserCredentials(t *testing.T) {
var teardownFn func()
config.GetCredentialsPath, teardownFn = testCredentialFile()
globalconfig.GetCredentialsPath, teardownFn = testCredentialFile()
defer teardownFn()

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

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

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

func TestEditUserCredentials(t *testing.T) {
var teardownFn func()
config.GetCredentialsPath, teardownFn = testCredentialFile()
globalconfig.GetCredentialsPath, teardownFn = testCredentialFile()
defer teardownFn()

t.Run("Should create new project if not exist", func(t *testing.T) {
projectName := "test-project3"
project := config.GetUserCredentials(projectName)
project := globalconfig.GetUserCredentials(projectName)
project.AWSResourceConfig.AccessKeyId = "TEST_KEY_ID_1"
config.Save(project)
newKeyID := config.GetUserCredentials(projectName).AWSResourceConfig.AccessKeyId
globalconfig.Save(project)
newKeyID := globalconfig.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 := globalconfig.GetUserCredentials(projectName)
project.AWSResourceConfig.AccessKeyId = "EDITED_ACCESS_KEY_ID"
config.Save(project)
newKeyID := config.GetUserCredentials(projectName).AWSResourceConfig.AccessKeyId
globalconfig.Save(project)
newKeyID := globalconfig.GetUserCredentials(projectName).AWSResourceConfig.AccessKeyId
assert.Equal(t, "EDITED_ACCESS_KEY_ID", newKeyID)
})
}
32 changes: 0 additions & 32 deletions internal/config/init_test.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package config
package moduleconfig

import (
"io/ioutil"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package config
package projectconfig

import (
"fmt"
"io/ioutil"
"path"

"github.com/commitdev/zero/configs"
"github.com/commitdev/zero/internal/constants"
"github.com/commitdev/zero/pkg/util/exit"
)

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

err := ioutil.WriteFile(path.Join(dir, projectName, configs.ZeroProjectYml), content, 0644)
err := ioutil.WriteFile(path.Join(dir, projectName, constants.ZeroProjectYml), content, 0644)
if err != nil {
exit.Fatal(fmt.Sprintf("Failed to create example %s", configs.ZeroProjectYml))
exit.Fatal(fmt.Sprintf("Failed to create example %s", constants.ZeroProjectYml))
}
}
32 changes: 32 additions & 0 deletions internal/config/projectconfig/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package projectconfig_test

import (
"os"
"path"
"testing"

"github.com/commitdev/zero/internal/config/projectconfig"
"github.com/commitdev/zero/internal/constants"
)

func TestInit(t *testing.T) {
const testDir = "../../test-sandbox"
projectName := "test-project"

projectconfig.SetRootDir(testDir)
defer os.RemoveAll(testDir)

testDirPath := path.Join(projectconfig.RootDir, projectName)
// create sandbox dir
err := os.MkdirAll(testDirPath, os.ModePerm)
if err != nil {
t.Fatal(err)
}

config := projectconfig.ZeroProjectConfig{}
projectconfig.Init(projectconfig.RootDir, projectName, &config)

if _, err := os.Stat(path.Join(testDirPath, constants.ZeroProjectYml)); err != nil {
t.Fatal(err)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package config
package projectconfig

import (
"io/ioutil"
Expand Down
11 changes: 11 additions & 0 deletions internal/constants/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package constants

const (
TemplatesDir = "tmp/templates"
ZeroProjectYml = "zero-project.yml"
ZeroModuleYml = "zero-module.yml"
UserCredentialsYml = "credentials.yml"
ZeroHomeDirectory = ".zero"
IgnoredPaths = "(?i)zero.module.yml|.git/"
TemplateExtn = ".tmpl"
)
20 changes: 11 additions & 9 deletions internal/context/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/commitdev/zero/internal/config"
"github.com/commitdev/zero/internal/config/globalconfig"
"github.com/commitdev/zero/internal/config/projectconfig"
"github.com/commitdev/zero/internal/module"
project "github.com/commitdev/zero/pkg/credentials"
"github.com/commitdev/zero/pkg/util/exit"
Expand All @@ -20,7 +22,7 @@ import (
type Registry map[string][]string

// Create cloud provider context
func Init(projectName string, outDir string) *config.ZeroProjectConfig {
func Init(projectName string, outDir string) *projectconfig.ZeroProjectConfig {
rootDir := path.Join(outDir, projectName)
flog.Infof(":tada: Creating project %s.", projectName)

Expand Down Expand Up @@ -51,7 +53,7 @@ func Init(projectName string, outDir string) *config.ZeroProjectConfig {
return &projectConfig
}

func promptAllModules(projectConfig *config.ZeroProjectConfig) {
func promptAllModules(projectConfig *projectconfig.ZeroProjectConfig) {
// TODO: do we need to run through the modules and extract first
// or we need to run through twice, potentially still need to pre-process for global auths
for _, moduleSource := range projectConfig.Modules {
Expand Down Expand Up @@ -93,7 +95,7 @@ func promptGithubRootOrg() string {
func promptGithubPersonalToken(projectName string) string {
defaultToken := ""

project := config.GetUserCredentials(projectName)
project := globalconfig.GetUserCredentials(projectName)
if project.GithubResourceConfig.AccessToken != "" {
defaultToken = project.GithubResourceConfig.AccessToken
}
Expand All @@ -110,7 +112,7 @@ func promptGithubPersonalToken(projectName string) string {
// If its different from saved token, update it
if project.GithubResourceConfig.AccessToken != result {
project.GithubResourceConfig.AccessToken = result
config.Save(project)
globalconfig.Save(project)
}
return result
}
Expand All @@ -128,7 +130,7 @@ func promptProjectName(projectName string) string {
return result
}

func chooseCloudProvider(projectConfig *config.ZeroProjectConfig) {
func chooseCloudProvider(projectConfig *projectconfig.ZeroProjectConfig) {
// @TODO move options into configs
providerPrompt := promptui.Select{
Label: "Select Cloud Provider",
Expand Down Expand Up @@ -180,7 +182,7 @@ func chooseStack(registry Registry) []string {

}

func fillProviderDetails(projectConfig *config.ZeroProjectConfig, s project.Secrets) {
func fillProviderDetails(projectConfig *projectconfig.ZeroProjectConfig, s project.Secrets) {
if projectConfig.Infrastructure.AWS != nil {
sess, err := session.NewSession(&aws.Config{
Region: aws.String(projectConfig.Infrastructure.AWS.Region),
Expand Down Expand Up @@ -208,10 +210,10 @@ func fillProviderDetails(projectConfig *config.ZeroProjectConfig, s project.Secr
}
}

func defaultProjConfig(projectName string) config.ZeroProjectConfig {
return config.ZeroProjectConfig{
func defaultProjConfig(projectName string) projectconfig.ZeroProjectConfig {
return projectconfig.ZeroProjectConfig{
Name: projectName,
Infrastructure: config.Infrastructure{
Infrastructure: projectconfig.Infrastructure{
AWS: nil,
},
Context: map[string]string{},
Expand Down