Skip to content

Commit b38e72a

Browse files
authored
Zero root domain static site support (#231)
* copy bin files from src -> dest * update registry
1 parent b253a1e commit b38e72a

File tree

6 files changed

+83
-23
lines changed

6 files changed

+83
-23
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ $ zero init
8686
## Sample project initilization
8787
✔ Project Name: myapp-infra
8888
🎉 Initializing project
89-
✔ EKS + Go + React
89+
✔ EKS + Go + React + Gatsby
9090
✔ Should the created projects be checked into github automatically? (y/n): y
9191
✔ What's the root of the github org to create repositories in?: github.com/myapp-org
9292
✔ Existing AWS Profiles

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ require (
2828
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 // indirect
2929
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
3030
gopkg.in/yaml.v2 v2.2.2
31+
github.com/gabriel-vasile/mimetype v1.1.1
3132

3233
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7
100100
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
101101
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
102102
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
103+
github.com/gabriel-vasile/mimetype v1.1.1 h1:qbN9MPuRf3bstHu9zkI9jDWNfH//9+9kHxr9oRBBBOA=
104+
github.com/gabriel-vasile/mimetype v1.1.1/go.mod h1:6CDPel/o/3/s4+bp6kIbsWATq8pmgOisOPG40CJa6To=
103105
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
104106
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
105107
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=

internal/generate/generate_modules.go

Lines changed: 74 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package generate
22

33
import (
44
"fmt"
5+
"io"
6+
"log"
57
"os"
68
"path"
79
"path/filepath"
810
"regexp"
9-
"strings"
1011
"sync"
1112
"text/template"
1213

@@ -15,8 +16,9 @@ import (
1516
"github.com/commitdev/zero/internal/module"
1617
"github.com/commitdev/zero/internal/util"
1718
"github.com/commitdev/zero/pkg/util/flog"
18-
1919
"github.com/commitdev/zero/pkg/util/fs"
20+
21+
"github.com/gabriel-vasile/mimetype"
2022
)
2123

2224
// Generate accepts a projectconfig struct and renders the templates for all referenced modules
@@ -52,22 +54,23 @@ func Generate(projectConfig projectconfig.ZeroProjectConfig) error {
5254
mod.Parameters,
5355
}
5456

55-
fileTemplates := newTemplates(moduleDir, outputDir, false)
57+
txtTypeFiles, binTypeFiles := sortFileType(moduleDir, outputDir, false)
5658

57-
executeTemplates(fileTemplates, templateData, delimiters)
59+
executeTemplates(txtTypeFiles, templateData, delimiters)
60+
copyBinFiles(binTypeFiles)
5861
}
5962
return nil
6063
}
6164

62-
type TemplateConfig struct {
65+
type fileConfig struct {
6366
source string
6467
destination string
65-
isTemplate bool
6668
}
6769

68-
// newTemplates walks the module directory to find all to be templated
69-
func newTemplates(moduleDir string, outputDir string, overwrite bool) []*TemplateConfig {
70-
templates := []*TemplateConfig{}
70+
// sortFileType walks the module directory to find and classify all files into bin / text/plain (non-bin) types.
71+
func sortFileType(moduleDir string, outputDir string, overwrite bool) ([]*fileConfig, []*fileConfig) {
72+
binTypeFiles := []*fileConfig{}
73+
txtTypeFiles := []*fileConfig{}
7174

7275
paths, err := getAllFilePathsInDirectory(moduleDir)
7376
if err != nil {
@@ -80,11 +83,6 @@ func newTemplates(moduleDir string, outputDir string, overwrite bool) []*Templat
8083
continue
8184
}
8285

83-
_, file := filepath.Split(path)
84-
hasTmpltSuffix := strings.HasSuffix(file, constants.TemplateExtn)
85-
if hasTmpltSuffix {
86-
file = strings.Replace(file, constants.TemplateExtn, "", -1)
87-
}
8886
outputPath := fs.ReplacePath(path, moduleDir, outputDir)
8987

9088
if !overwrite {
@@ -94,13 +92,34 @@ func newTemplates(moduleDir string, outputDir string, overwrite bool) []*Templat
9492
}
9593
}
9694

97-
templates = append(templates, &TemplateConfig{
95+
// detect the file type
96+
detectedMIME, err := mimetype.DetectFile(path)
97+
if err != nil {
98+
panic(err)
99+
}
100+
101+
// detect root file type
102+
isBinary := true
103+
for mime := detectedMIME; mime != nil; mime = mime.Parent() {
104+
if mime.Is("text/plain") {
105+
isBinary = false
106+
}
107+
}
108+
109+
if isBinary {
110+
binTypeFiles = append(binTypeFiles, &fileConfig{
111+
source: path,
112+
destination: outputPath,
113+
})
114+
continue
115+
}
116+
117+
txtTypeFiles = append(txtTypeFiles, &fileConfig{
98118
source: path,
99119
destination: outputPath,
100-
isTemplate: hasTmpltSuffix,
101120
})
102121
}
103-
return templates
122+
return txtTypeFiles, binTypeFiles
104123
}
105124

106125
// getAllFilePathsInDirectory Recursively get all file paths in directory, including sub-directories.
@@ -122,7 +141,7 @@ func getAllFilePathsInDirectory(moduleDir string) ([]string, error) {
122141
return paths, nil
123142
}
124143

125-
func executeTemplates(templates []*TemplateConfig, data interface{}, delimiters []string) {
144+
func executeTemplates(templates []*fileConfig, data interface{}, delimiters []string) {
126145
var wg sync.WaitGroup
127146
leftDelim := delimiters[0]
128147
rightDelim := delimiters[1]
@@ -162,3 +181,40 @@ func executeTemplates(templates []*TemplateConfig, data interface{}, delimiters
162181

163182
wg.Wait()
164183
}
184+
185+
func copyBinFiles(binTypeFiles []*fileConfig) {
186+
187+
for _, binFile := range binTypeFiles {
188+
source := binFile.source
189+
dest := binFile.destination
190+
191+
// create dir
192+
outputDirPath, _ := path.Split(dest)
193+
err := fs.CreateDirs(outputDirPath)
194+
if err != nil {
195+
flog.Errorf("Error creating directory '%s': %v", source, err)
196+
}
197+
198+
// create refs to src and dest
199+
from, err := os.Open(source)
200+
if err != nil {
201+
flog.Errorf("Error opening file to read '%s' : %v", source, err)
202+
}
203+
defer from.Close()
204+
205+
to, err := os.OpenFile(dest, os.O_RDWR|os.O_CREATE, 0666)
206+
if err != nil {
207+
log.Fatal(err)
208+
flog.Errorf("Error creating file '%s': %v", dest, err)
209+
}
210+
defer to.Close()
211+
212+
// copy file
213+
_, err = io.Copy(to, from)
214+
if err != nil {
215+
flog.Errorf("Error copying file '%s' : %v", source, err)
216+
} else {
217+
flog.Successf("Finished copying file : %s", dest)
218+
}
219+
}
220+
}

internal/registry/registry.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ func GetRegistry() Registry {
1010
return Registry{
1111
// TODO: better place to store these options as configuration file or any source
1212
{
13-
"EKS + Go + React",
13+
"EKS + Go + React + Gatsby",
1414
[]string{
1515
"github.com/commitdev/zero-aws-eks-stack",
16+
"github.com/commitdev/zero-deployable-landing-page",
1617
"github.com/commitdev/zero-deployable-backend",
1718
"github.com/commitdev/zero-deployable-react-frontend",
1819
},

internal/registry/registry_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func TestAvailableLabels(t *testing.T) {
1313
t.Run("should be same order as declared", func(t *testing.T) {
1414
labels := registry.AvailableLabels(reg)
1515
assert.Equal(t, labels, []string{
16-
"EKS + Go + React",
16+
"EKS + Go + React + Gatsby",
1717
"foo",
1818
"bar",
1919
"lorem",
@@ -27,7 +27,7 @@ func TestGetModulesByName(t *testing.T) {
2727
reg := testRegistry()
2828
t.Run("should return modules of specified stack", func(t *testing.T) {
2929

30-
assert.Equal(t, registry.GetModulesByName(reg, "EKS + Go + React"),
30+
assert.Equal(t, registry.GetModulesByName(reg, "EKS + Go + React + Gatsby"),
3131
[]string{"module-source 1", "module-source 2"})
3232
assert.Equal(t, registry.GetModulesByName(reg, "lorem"), []string{"module-source 5"})
3333
assert.Equal(t, registry.GetModulesByName(reg, "ipsum"), []string{"module-source 6"})
@@ -37,7 +37,7 @@ func TestGetModulesByName(t *testing.T) {
3737

3838
func testRegistry() registry.Registry {
3939
return registry.Registry{
40-
{"EKS + Go + React", []string{"module-source 1", "module-source 2"}},
40+
{"EKS + Go + React + Gatsby", []string{"module-source 1", "module-source 2"}},
4141
{"foo", []string{"module-source 3"}},
4242
{"bar", []string{"module-source 4"}},
4343
{"lorem", []string{"module-source 5"}},

0 commit comments

Comments
 (0)