Skip to content

Commit

Permalink
registry to retain order of declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcheung committed Jun 18, 2020
1 parent 6f1b217 commit 64d25e5
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 28 deletions.
33 changes: 5 additions & 28 deletions internal/context/init.go
Expand Up @@ -15,15 +15,14 @@ import (
"github.com/commitdev/zero/internal/config/moduleconfig"
"github.com/commitdev/zero/internal/config/projectconfig"
"github.com/commitdev/zero/internal/module"
"github.com/commitdev/zero/internal/registry"
project "github.com/commitdev/zero/pkg/credentials"
"github.com/commitdev/zero/pkg/util/exit"
"github.com/commitdev/zero/pkg/util/flog"
"github.com/k0kubun/pp"
"github.com/manifoldco/promptui"
)

type Registry map[string][]string

// Create cloud provider context
func Init(outDir string) *projectconfig.ZeroProjectConfig {
projectConfig := defaultProjConfig()
Expand All @@ -40,7 +39,7 @@ func Init(outDir string) *projectconfig.ZeroProjectConfig {
exit.Fatal("Error creating root: %v ", err)
}

moduleSources := chooseStack(getRegistry())
moduleSources := chooseStack(registry.GetRegistry())
moduleConfigs, mappedSources := loadAllModules(moduleSources)

prompts := getProjectPrompts(projectConfig.Name, moduleConfigs)
Expand Down Expand Up @@ -256,39 +255,17 @@ func chooseCloudProvider(projectConfig *projectconfig.ZeroProjectConfig) {
}
}

func getRegistry() Registry {
return Registry{
// TODO: better place to store these options as configuration file or any source
"EKS + Go + React": []string{
"github.com/commitdev/zero-aws-eks-stack",
"github.com/commitdev/zero-deployable-backend",
"github.com/commitdev/zero-deployable-react-frontend",
},
"Custom": []string{},
}
}

func (registry Registry) availableLabels() []string {
labels := make([]string, len(registry))
i := 0
for label := range registry {
labels[i] = label
i++
}
return labels
}

func chooseStack(registry Registry) []string {
func chooseStack(reg registry.Registry) []string {
providerPrompt := promptui.Select{
Label: "Pick a stack you'd like to use",
Items: registry.availableLabels(),
Items: registry.AvailableLabels(reg),
}
_, providerResult, err := providerPrompt.Run()
if err != nil {
exit.Fatal("Prompt failed %v\n", err)
}

return registry[providerResult]
return registry.GetModulesByName(reg, providerResult)
}

func fillProviderDetails(projectConfig *projectconfig.ZeroProjectConfig, s project.Secrets) {
Expand Down
43 changes: 43 additions & 0 deletions internal/registry/registry.go
@@ -0,0 +1,43 @@
package registry

type Registry []Stack
type Stack struct {
Name string
ModuleSources []string
}

func GetRegistry() Registry {
return Registry{
// TODO: better place to store these options as configuration file or any source
{
"EKS + Go + React",
[]string{
"github.com/commitdev/zero-aws-eks-stack",
"github.com/commitdev/zero-deployable-backend",
"github.com/commitdev/zero-deployable-react-frontend",
},
},
{
"Custom", []string{},
},
}
}

func GetModulesByName(registry Registry, name string) []string {
for _, v := range registry {
if v.Name == name {
return v.ModuleSources
}
}
return []string{}
}

func AvailableLabels(registry Registry) []string {
labels := make([]string, len(registry))
i := 0
for _, stack := range registry {
labels[i] = stack.Name
i++
}
return labels
}
47 changes: 47 additions & 0 deletions internal/registry/registry_test.go
@@ -0,0 +1,47 @@
package registry_test

import (
"testing"

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

func TestAvailableLabels(t *testing.T) {
reg := testRegistry()

t.Run("should be same order as declared", func(t *testing.T) {
labels := registry.AvailableLabels(reg)
assert.Equal(t, labels, []string{
"EKS + Go + React",
"foo",
"bar",
"lorem",
"ipsum",
"Custom",
})
})
}

func TestGetModulesByName(t *testing.T) {
reg := testRegistry()
t.Run("should return modules of specified stack", func(t *testing.T) {

assert.Equal(t, registry.GetModulesByName(reg, "EKS + Go + React"),
[]string{"module-source 1", "module-source 2"})
assert.Equal(t, registry.GetModulesByName(reg, "lorem"), []string{"module-source 5"})
assert.Equal(t, registry.GetModulesByName(reg, "ipsum"), []string{"module-source 6"})
assert.Equal(t, registry.GetModulesByName(reg, "Custom"), []string{"module-source 7"})
})
}

func testRegistry() registry.Registry {
return registry.Registry{
{"EKS + Go + React", []string{"module-source 1", "module-source 2"}},
{"foo", []string{"module-source 3"}},
{"bar", []string{"module-source 4"}},
{"lorem", []string{"module-source 5"}},
{"ipsum", []string{"module-source 6"}},
{"Custom", []string{"module-source 7"}},
}
}

0 comments on commit 64d25e5

Please sign in to comment.