diff --git a/cmd/init.go b/cmd/init.go index b5b4bdbc..237f094d 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -11,9 +11,11 @@ import ( ) var localModulePath string +var registryFilePath string func init() { initCmd.PersistentFlags().StringVarP(&localModulePath, "local-module-path", "m", "github.com/commitdev", "local module path - for using local modules instead of downloading from github") + initCmd.PersistentFlags().StringVarP(®istryFilePath, "registry-file-path", "r", "https://raw.githubusercontent.com/commitdev/zero/main/registry.yaml", "registry file path - for using a custom list of stacks") rootCmd.AddCommand(initCmd) } @@ -22,7 +24,7 @@ var initCmd = &cobra.Command{ Short: "Create new project with provided name and initialize configuration based on user input.", Run: func(cmd *cobra.Command, args []string) { flog.Debugf("Root directory is %s", projectconfig.RootDir) - projectContext := initPrompts.Init(projectconfig.RootDir, localModulePath) + projectContext := initPrompts.Init(projectconfig.RootDir, localModulePath, registryFilePath) projectConfigErr := projectconfig.CreateProjectConfigFile(projectconfig.RootDir, projectContext.Name, projectContext) if projectConfigErr != nil { diff --git a/internal/constants/constants.go b/internal/constants/constants.go index 9abab1db..8ade9ff2 100644 --- a/internal/constants/constants.go +++ b/internal/constants/constants.go @@ -1,6 +1,7 @@ package constants const ( + TmpRegistryYml = "tmp/registry.yaml" TemplatesDir = "tmp/templates" ZeroProjectYml = "zero-project.yml" ZeroModuleYml = "zero-module.yml" diff --git a/internal/init/init.go b/internal/init/init.go index 04520e49..119c34fa 100644 --- a/internal/init/init.go +++ b/internal/init/init.go @@ -17,7 +17,7 @@ import ( ) // Create cloud provider context -func Init(outDir string, localModulePath string) *projectconfig.ZeroProjectConfig { +func Init(outDir, localModulePath, registryFilePath string) *projectconfig.ZeroProjectConfig { projectConfig := defaultProjConfig() projectRootParams := map[string]string{} @@ -36,7 +36,13 @@ func Init(outDir string, localModulePath string) *projectconfig.ZeroProjectConfi exit.Fatal("Error creating root: %v ", err) } - moduleSources := chooseStack(registry.GetRegistry(localModulePath)) + registry, err := registry.GetRegistry(localModulePath, registryFilePath) + + if err != nil { + exit.Fatal("Error getting registry: %v ", err) + } + + moduleSources := chooseStack(registry) moduleConfigs, mappedSources := loadAllModules(moduleSources) prompts := getProjectPrompts(projectConfig.Name, moduleConfigs) diff --git a/internal/registry/registry.go b/internal/registry/registry.go index 1631342b..ee4faad3 100644 --- a/internal/registry/registry.go +++ b/internal/registry/registry.go @@ -1,33 +1,46 @@ package registry +import ( + "io/ioutil" + + "github.com/commitdev/zero/internal/constants" + "github.com/hashicorp/go-getter" + + yaml "gopkg.in/yaml.v2" +) + type Registry []Stack + type Stack struct { - Name string - ModuleSources []string + Name string `yaml:"name"` + ModuleSources []string `yaml:"moduleSources"` } -func GetRegistry(path string) Registry { - return Registry{ - // TODO: better place to store these options as configuration file or any source - { - "EKS + Go + React + Gatsby", - []string{ - path + "/zero-aws-eks-stack", - path + "/zero-static-site-gatsby", - path + "/zero-backend-go", - path + "/zero-frontend-react", - }, - }, - { - "EKS + NodeJS + React + Gatsby", - []string{ - path + "/zero-aws-eks-stack", - path + "/zero-static-site-gatsby", - path + "/zero-backend-node", - path + "/zero-frontend-react", - }, - }, +func GetRegistry(localModulePath, registryFilePath string) (Registry, error) { + registry := Registry{} + + err := getter.GetFile(constants.TmpRegistryYml, registryFilePath) + if err != nil { + return nil, err + } + + data, err := ioutil.ReadFile(constants.TmpRegistryYml) + if err != nil { + return nil, err } + + err = yaml.Unmarshal(data, ®istry) + if err != nil { + return nil, err + } + + for i := 0; i < len(registry); i++ { + for j := 0; j < len(registry[i].ModuleSources); j++ { + registry[i].ModuleSources[j] = localModulePath + registry[i].ModuleSources[j] + } + } + + return registry, nil } func GetModulesByName(registry Registry, name string) []string { diff --git a/registry.yaml b/registry.yaml new file mode 100644 index 00000000..cdad7113 --- /dev/null +++ b/registry.yaml @@ -0,0 +1,13 @@ +- name: EKS + Go + React + Gatsby + moduleSources: + - /zero-aws-eks-stack + - /zero-static-site-gatsby + - /zero-backend-go + - /zero-frontend-react + +- name: EKS + NodeJS + React + Gatsby + moduleSources: + - /zero-aws-eks-stack + - /zero-static-site-gatsby + - /zero-backend-node + - /zero-frontend-react