Skip to content

Commit 12c0eb4

Browse files
authored
Merge pull request #168 from commitdev/registry-constant-order
registry to retain order of declaration
2 parents 6f1b217 + f2634c5 commit 12c0eb4

File tree

6 files changed

+109
-44
lines changed

6 files changed

+109
-44
lines changed

.github/workflows/config.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ jobs:
1111
- uses: actions/setup-go@v2
1212
with:
1313
go-version: 1.12
14-
- name: Download Go Packages
15-
run: |
16-
make deps
14+
- uses: actions/cache@v2
15+
with:
16+
path: ~/go/pkg/mod
17+
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
18+
restore-keys: |
19+
${{ runner.os }}-go-
1720
- name: Run Go Tests
1821
run: |
1922
make check

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ require (
1010
github.com/coreos/go-semver v0.2.0
1111
github.com/google/go-cmp v0.3.0
1212
github.com/google/uuid v1.1.1
13-
github.com/gorilla/handlers v1.4.2
14-
github.com/gorilla/mux v1.7.3
1513
github.com/hashicorp/go-getter v1.4.0
1614
github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a // indirect
1715
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
@@ -23,10 +21,12 @@ require (
2321
github.com/manifoldco/promptui v0.3.0
2422
github.com/matryer/is v1.3.0 // indirect
2523
github.com/mattn/go-colorable v0.1.2 // indirect
24+
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
2625
github.com/spf13/cobra v0.0.6
2726
github.com/stretchr/testify v1.4.0
2827
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b // indirect
2928
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 // indirect
30-
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
31-
gopkg.in/yaml.v2 v2.2.5
29+
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
30+
gopkg.in/yaml.v2 v2.2.2
31+
3232
)

go.sum

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,6 @@ github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
7676
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
7777
github.com/googleapis/gax-go/v2 v2.0.5 h1:sjZBwGj9Jlw33ImPtvFviGYvseOtDM7hkSKB7+Tv3SM=
7878
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
79-
github.com/gorilla/handlers v1.4.2 h1:0QniY0USkHQ1RGCLfKxeNHK9bkDHGRYGNDFBCS+YARg=
80-
github.com/gorilla/handlers v1.4.2/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/OAirnOIQ=
81-
github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw=
82-
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
8379
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
8480
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
8581
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
@@ -147,6 +143,8 @@ github.com/mitchellh/go-testing-interface v1.0.0 h1:fzU/JVNcaqHQEcVFAKeR41fkiLdI
147143
github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
148144
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
149145
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
146+
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
147+
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
150148
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
151149
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
152150
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
@@ -293,17 +291,14 @@ google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ij
293291
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
294292
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
295293
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
296-
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
297-
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
294+
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
295+
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
298296
gopkg.in/cheggaaa/pb.v1 v1.0.27/go.mod h1:V/YB90LKu/1FcN3WVnfiiE5oMCibMjukxqG/qStrOgw=
299297
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
300298
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
301299
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
302300
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
303301
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
304-
gopkg.in/yaml.v2 v2.2.5 h1:ymVxjfMaHvXD8RqPRmzHHsB3VvucivSkIAvJFDI5O3c=
305-
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
306-
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
307302
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
308303
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
309304
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

internal/context/init.go

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@ import (
1515
"github.com/commitdev/zero/internal/config/moduleconfig"
1616
"github.com/commitdev/zero/internal/config/projectconfig"
1717
"github.com/commitdev/zero/internal/module"
18+
"github.com/commitdev/zero/internal/registry"
1819
project "github.com/commitdev/zero/pkg/credentials"
1920
"github.com/commitdev/zero/pkg/util/exit"
2021
"github.com/commitdev/zero/pkg/util/flog"
2122
"github.com/k0kubun/pp"
2223
"github.com/manifoldco/promptui"
2324
)
2425

25-
type Registry map[string][]string
26-
2726
// Create cloud provider context
2827
func Init(outDir string) *projectconfig.ZeroProjectConfig {
2928
projectConfig := defaultProjConfig()
@@ -40,7 +39,7 @@ func Init(outDir string) *projectconfig.ZeroProjectConfig {
4039
exit.Fatal("Error creating root: %v ", err)
4140
}
4241

43-
moduleSources := chooseStack(getRegistry())
42+
moduleSources := chooseStack(registry.GetRegistry())
4443
moduleConfigs, mappedSources := loadAllModules(moduleSources)
4544

4645
prompts := getProjectPrompts(projectConfig.Name, moduleConfigs)
@@ -256,39 +255,17 @@ func chooseCloudProvider(projectConfig *projectconfig.ZeroProjectConfig) {
256255
}
257256
}
258257

259-
func getRegistry() Registry {
260-
return Registry{
261-
// TODO: better place to store these options as configuration file or any source
262-
"EKS + Go + React": []string{
263-
"github.com/commitdev/zero-aws-eks-stack",
264-
"github.com/commitdev/zero-deployable-backend",
265-
"github.com/commitdev/zero-deployable-react-frontend",
266-
},
267-
"Custom": []string{},
268-
}
269-
}
270-
271-
func (registry Registry) availableLabels() []string {
272-
labels := make([]string, len(registry))
273-
i := 0
274-
for label := range registry {
275-
labels[i] = label
276-
i++
277-
}
278-
return labels
279-
}
280-
281-
func chooseStack(registry Registry) []string {
258+
func chooseStack(reg registry.Registry) []string {
282259
providerPrompt := promptui.Select{
283260
Label: "Pick a stack you'd like to use",
284-
Items: registry.availableLabels(),
261+
Items: registry.AvailableLabels(reg),
285262
}
286263
_, providerResult, err := providerPrompt.Run()
287264
if err != nil {
288265
exit.Fatal("Prompt failed %v\n", err)
289266
}
290267

291-
return registry[providerResult]
268+
return registry.GetModulesByName(reg, providerResult)
292269
}
293270

294271
func fillProviderDetails(projectConfig *projectconfig.ZeroProjectConfig, s project.Secrets) {

internal/registry/registry.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package registry
2+
3+
type Registry []Stack
4+
type Stack struct {
5+
Name string
6+
ModuleSources []string
7+
}
8+
9+
func GetRegistry() Registry {
10+
return Registry{
11+
// TODO: better place to store these options as configuration file or any source
12+
{
13+
"EKS + Go + React",
14+
[]string{
15+
"github.com/commitdev/zero-aws-eks-stack",
16+
"github.com/commitdev/zero-deployable-backend",
17+
"github.com/commitdev/zero-deployable-react-frontend",
18+
},
19+
},
20+
{
21+
"Custom", []string{},
22+
},
23+
}
24+
}
25+
26+
func GetModulesByName(registry Registry, name string) []string {
27+
for _, v := range registry {
28+
if v.Name == name {
29+
return v.ModuleSources
30+
}
31+
}
32+
return []string{}
33+
}
34+
35+
func AvailableLabels(registry Registry) []string {
36+
labels := make([]string, len(registry))
37+
i := 0
38+
for _, stack := range registry {
39+
labels[i] = stack.Name
40+
i++
41+
}
42+
return labels
43+
}

internal/registry/registry_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package registry_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/commitdev/zero/internal/registry"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestAvailableLabels(t *testing.T) {
11+
reg := testRegistry()
12+
13+
t.Run("should be same order as declared", func(t *testing.T) {
14+
labels := registry.AvailableLabels(reg)
15+
assert.Equal(t, labels, []string{
16+
"EKS + Go + React",
17+
"foo",
18+
"bar",
19+
"lorem",
20+
"ipsum",
21+
"Custom",
22+
})
23+
})
24+
}
25+
26+
func TestGetModulesByName(t *testing.T) {
27+
reg := testRegistry()
28+
t.Run("should return modules of specified stack", func(t *testing.T) {
29+
30+
assert.Equal(t, registry.GetModulesByName(reg, "EKS + Go + React"),
31+
[]string{"module-source 1", "module-source 2"})
32+
assert.Equal(t, registry.GetModulesByName(reg, "lorem"), []string{"module-source 5"})
33+
assert.Equal(t, registry.GetModulesByName(reg, "ipsum"), []string{"module-source 6"})
34+
assert.Equal(t, registry.GetModulesByName(reg, "Custom"), []string{"module-source 7"})
35+
})
36+
}
37+
38+
func testRegistry() registry.Registry {
39+
return registry.Registry{
40+
{"EKS + Go + React", []string{"module-source 1", "module-source 2"}},
41+
{"foo", []string{"module-source 3"}},
42+
{"bar", []string{"module-source 4"}},
43+
{"lorem", []string{"module-source 5"}},
44+
{"ipsum", []string{"module-source 6"}},
45+
{"Custom", []string{"module-source 7"}},
46+
}
47+
}

0 commit comments

Comments
 (0)