Skip to content

Commit

Permalink
Merge pull request #168 from commitdev/registry-constant-order
Browse files Browse the repository at this point in the history
registry to retain order of declaration
  • Loading branch information
davidcheung committed Jun 19, 2020
2 parents 6f1b217 + f2634c5 commit 12c0eb4
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 44 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/config.yml
Expand Up @@ -11,9 +11,12 @@ jobs:
- uses: actions/setup-go@v2
with:
go-version: 1.12
- name: Download Go Packages
run: |
make deps
- uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Run Go Tests
run: |
make check
8 changes: 4 additions & 4 deletions go.mod
Expand Up @@ -10,8 +10,6 @@ require (
github.com/coreos/go-semver v0.2.0
github.com/google/go-cmp v0.3.0
github.com/google/uuid v1.1.1
github.com/gorilla/handlers v1.4.2
github.com/gorilla/mux v1.7.3
github.com/hashicorp/go-getter v1.4.0
github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a // indirect
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
Expand All @@ -23,10 +21,12 @@ require (
github.com/manifoldco/promptui v0.3.0
github.com/matryer/is v1.3.0 // indirect
github.com/mattn/go-colorable v0.1.2 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/spf13/cobra v0.0.6
github.com/stretchr/testify v1.4.0
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b // indirect
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v2 v2.2.5
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/yaml.v2 v2.2.2

)
13 changes: 4 additions & 9 deletions go.sum
Expand Up @@ -76,10 +76,6 @@ github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5 h1:sjZBwGj9Jlw33ImPtvFviGYvseOtDM7hkSKB7+Tv3SM=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/gorilla/handlers v1.4.2 h1:0QniY0USkHQ1RGCLfKxeNHK9bkDHGRYGNDFBCS+YARg=
github.com/gorilla/handlers v1.4.2/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/OAirnOIQ=
github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
Expand Down Expand Up @@ -147,6 +143,8 @@ github.com/mitchellh/go-testing-interface v1.0.0 h1:fzU/JVNcaqHQEcVFAKeR41fkiLdI
github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
Expand Down Expand Up @@ -293,17 +291,14 @@ google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ij
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/cheggaaa/pb.v1 v1.0.27/go.mod h1:V/YB90LKu/1FcN3WVnfiiE5oMCibMjukxqG/qStrOgw=
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.5 h1:ymVxjfMaHvXD8RqPRmzHHsB3VvucivSkIAvJFDI5O3c=
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
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 12c0eb4

Please sign in to comment.