diff --git a/internal/context/init.go b/internal/context/init.go index 63379d522..4da7a1264 100644 --- a/internal/context/init.go +++ b/internal/context/init.go @@ -15,6 +15,7 @@ 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" @@ -22,8 +23,6 @@ import ( "github.com/manifoldco/promptui" ) -type Registry map[string][]string - // Create cloud provider context func Init(outDir string) *projectconfig.ZeroProjectConfig { projectConfig := defaultProjConfig() @@ -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) @@ -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) { diff --git a/internal/registry/registry.go b/internal/registry/registry.go new file mode 100644 index 000000000..02a304a52 --- /dev/null +++ b/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 +} diff --git a/internal/registry/registry_test.go b/internal/registry/registry_test.go new file mode 100644 index 000000000..4fd8b1bef --- /dev/null +++ b/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"}}, + } +}