Skip to content

Commit c0e62fd

Browse files
authored
Merge pull request #72 from commitdev/react-templates
added new react templates, github CI and terraform generators
2 parents 1879690 + 0cdeb07 commit c0e62fd

File tree

141 files changed

+5936
-652
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

141 files changed

+5936
-652
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ main-packr.go
22
packrd
33
/commit0
44
.history/
5+
tmp/

README.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ The best way then to use this is to add an alias, then you can use the CLI as if
2222
## Usage
2323

2424
1) To create a project run `commit0 create [PROJECT_NAME]`
25-
2) A folder will be created and within that update the `commit0.yml` and then run `commit0 generate`
25+
2) A folder will be created and within that update the `commit0.yml` and then run `commit0 generate -c <commit0.yml>`
2626
3) You will see that there is now an idl folder created.
2727
4) Within the idl folder modify the the protobuf services generated with your desired methods
28-
5) Go up to the parent directory and re run `commit0 generate -l=[LANGUAGE OF CHOICE]`
28+
5) Go up to the parent directory and re run `commit0 generate -c <commit0.yml>`
2929
6) You will now see a `server` folder navigate to your service folder within that directory and implement the methods generated for it
3030
7) Once you have tested your implementation and are happy with it return to the idl repo push that directory up to git
3131
8) Return to the parent directory and check the depency file, for go it will be the go.mod file remove the lines that point it to your local directory, this will now point it to the version on git that was pushed up previously
@@ -66,13 +66,41 @@ this will create a commit0 executable in your working direcory. To install insta
6666
make install-go
6767
```
6868

69+
Compile a new `commit0` binary in the working directory
70+
```
71+
make build
72+
```
73+
74+
Now you can either add your project directory to your path or just execute it directly
75+
```
76+
mkdir tmp
77+
cd tmp
78+
../commit0 create test-app
79+
cd test-app
80+
../../commit0 generate -c commit0.yml
81+
```
82+
6983
### Architecture
7084
The project is built with GoLang and requires Docker
7185
- /cmd - the CLI command entry points
7286
- /internal/generate
7387
- /internal/config
7488
- /internal/templator - the templating service
7589

90+
Example Flow:
91+
The application starts at `cmd/generate.go`
92+
1. loads all the templates from packr
93+
- TODO: eventually this should be loaded remotely throug a dependency management system
94+
2. loads the config from the commit0.yml config file
95+
3. based on the configs, run the appropriate generators
96+
- templator is passed in to the Generate function for dependency injection
97+
4. each generator (`react/generate.go`, `ci/generate.go` etc) further delegates and actually executes the templating based on the configs passed in.
98+
- `internal/templator/templator.go` is the base class and includes generic templating handling logic
99+
- it CI is required, it'll also call a CI generator and pass in the service specific CI configs
100+
- TOOD: CI templates have to call separate templates based on the context
101+
- TODO: templator should be generic and not have any knowledge of the specific templating implementation (go, ci etc), move that logic upstream
102+
5. Depending on the config (`deploy == true` for certain) it'll also run the `Execute` function and actually deploy the infrastructure
103+
76104
### Building locally
77105

78106
As the templates are embeded into the binary you will need to ensure packr2 is installed.

internal/config/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ type aws struct {
7777
AccountId string `yaml:"accountId"`
7878
Region string
7979
EKS eks
80+
Cognito bool
81+
Terraform terraform
82+
}
83+
84+
type terraform struct {
85+
RemoteState bool
8086
}
8187

8288
type eks struct {

internal/config/react.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,6 @@ type reactApp struct {
44
Name string
55
}
66

7-
type reactHeader struct {
8-
Enabled bool
9-
}
10-
11-
type reactSidenavItem struct {
12-
Path string
13-
Label string
14-
Icon string
15-
}
16-
type reactSidenav struct {
17-
Enabled bool
18-
Items []reactSidenavItem
19-
}
20-
217
type reactAccount struct {
228
Enabled bool
239
Required bool
@@ -31,9 +17,5 @@ type reactView struct {
3117
type frontend struct {
3218
Framework string
3319
App reactApp
34-
Account reactAccount
35-
Header reactHeader
36-
Sidenav reactSidenav
37-
Views []reactView
3820
CI CI
3921
}

internal/generate/ci/generate.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func (e *CIGenerationError) Error() string {
2626
return fmt.Sprintf("Error: %s. Unable to Generate CI/CD Pipeline with config:\n%v\n", e.err, e.config)
2727
}
2828

29+
// TODO shouldn't have to pass in both ciConfig, and cfg, it's redundant.
2930
// Generate a CI configuration file based on your language and CI system
3031
func Generate(t *templator.CITemplator, cfg *config.Commit0Config, ciConfig config.CI, basePath string, wg *sync.WaitGroup) error {
3132

@@ -46,6 +47,10 @@ func Generate(t *templator.CITemplator, cfg *config.Commit0Config, ciConfig conf
4647
ciConfigPath = basePath
4748
ciFilename = ".travis.yml"
4849
ciTemp = t.TravisCI
50+
case "github":
51+
ciConfigPath = fmt.Sprintf("%s/%s", basePath, ".github/workflow/")
52+
ciFilename = "config.yml"
53+
ciTemp = t.Github
4954
default:
5055
return &CIGenerationError{"Unsupported CI System", ciConfig}
5156
}

internal/generate/generate_helper.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package generate
22

33
import (
4+
"log"
5+
"sync"
6+
47
"github.com/commitdev/commit0/internal/config"
58
"github.com/commitdev/commit0/internal/generate/golang"
69
"github.com/commitdev/commit0/internal/generate/kubernetes"
710
"github.com/commitdev/commit0/internal/generate/proto"
811
"github.com/commitdev/commit0/internal/generate/react"
12+
"github.com/commitdev/commit0/internal/generate/terraform"
913
"github.com/commitdev/commit0/internal/templator"
1014
"github.com/commitdev/commit0/internal/util"
1115
"github.com/kyokomi/emoji"
1216
"github.com/logrusorgru/aurora"
13-
"log"
14-
"sync"
1517
)
1618

1719
func GenerateArtifactsHelper(t *templator.Templator, cfg *config.Commit0Config, pathPrefix string) {
@@ -47,6 +49,9 @@ func GenerateArtifactsHelper(t *templator.Templator, cfg *config.Commit0Config,
4749
react.Generate(t, cfg, &wg, pathPrefix)
4850
}
4951

52+
log.Println(aurora.Cyan(emoji.Sprintf("Generating Terraform")))
53+
terraform.Generate(t, cfg, &wg, pathPrefix)
54+
5055
util.TemplateFileIfDoesNotExist(pathPrefix, "README.md", t.Readme, &wg, templator.GenericTemplateData{*cfg})
5156

5257
// Wait for all the templates to be generated
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package terraform
2+
3+
import (
4+
"sync"
5+
6+
"github.com/commitdev/commit0/internal/config"
7+
"github.com/commitdev/commit0/internal/templator"
8+
)
9+
10+
func Generate(t *templator.Templator, cfg *config.Commit0Config, wg *sync.WaitGroup, pathPrefix string) {
11+
data := templator.GenericTemplateData{*cfg}
12+
13+
t.Terraform.TemplateFiles(data, false, wg, pathPrefix)
14+
}

internal/templator/templator.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type CITemplator struct {
1616
CircleCI *template.Template
1717
TravisCI *template.Template
1818
Jenkins *template.Template
19+
Github *template.Template
1920
}
2021

2122
// DockerTemplator contains the templates relevent to docker
@@ -49,6 +50,7 @@ type Templator struct {
4950
React *DirectoryTemplator
5051
Kubernetes *DirectoryTemplator
5152
CI *CITemplator
53+
Terraform *DirectoryTemplator
5254
}
5355

5456
func NewTemplator(box *packr.Box) *Templator {
@@ -61,7 +63,8 @@ func NewTemplator(box *packr.Box) *Templator {
6163
GitIgnore: NewSingleFileTemplator(box, "util/gitignore.tmpl"),
6264
Readme: NewSingleFileTemplator(box, "util/README.tmpl"),
6365
Docker: NewDockerFileTemplator(box),
64-
React: NewDirectoryTemplator(box, "react"),
66+
React: NewEJSDirectoryTemplator(box, "react"),
67+
Terraform: NewDirectoryTemplator(box, "terraform"),
6568
Kubernetes: NewDirectoryTemplator(box, "kubernetes"),
6669
CI: NewCITemplator(box),
6770
}
@@ -115,10 +118,15 @@ func NewCITemplator(box *packr.Box) *CITemplator {
115118
jenkinsTemplateSource, _ := box.FindString("ci/Jenkinsfile.tmpl")
116119
jenkinsTemplate, _ := template.New("CIConfig").Parse(jenkinsTemplateSource)
117120

121+
githubTemplateSource, _ := box.FindString("ci/github.tmpl")
122+
// Github also uses double curly braces for their templates
123+
githubTemplate, _ := template.New("CIConfig").Delims("<%=", "%>").Parse(githubTemplateSource)
124+
118125
return &CITemplator{
119126
CircleCI: circleciTemplate,
120127
TravisCI: travisciTemplate,
121128
Jenkins: jenkinsTemplate,
129+
Github: githubTemplate,
122130
}
123131
}
124132

@@ -156,6 +164,23 @@ func NewDirectoryTemplator(box *packr.Box, dir string) *DirectoryTemplator {
156164
}
157165
}
158166

167+
// TODO standardize and consolidate the templating syntax, also allow for a config struct to change delimiters
168+
// NewEJSDirectoryTemplator
169+
func NewEJSDirectoryTemplator(box *packr.Box, dir string) *DirectoryTemplator {
170+
templates := []*template.Template{}
171+
for _, file := range getFileNames(box, dir) {
172+
templateSource, _ := box.FindString(file)
173+
template, err := template.New(file).Delims("<%=", "%>").Funcs(util.FuncMap).Parse(templateSource)
174+
if err != nil {
175+
panic(err)
176+
}
177+
templates = append(templates, template)
178+
}
179+
return &DirectoryTemplator{
180+
Templates: templates,
181+
}
182+
}
183+
159184
func getFileNames(box *packr.Box, dir string) []string {
160185
keys := []string{}
161186
box.WalkPrefix(dir, func(path string, info file.File) error {

templates/ci/github.tmpl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Build and Deploy
2+
3+
on: push
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- name: Checkout
11+
uses: actions/checkout@v1
12+
13+
# https://github.com/marketplace/actions/setup-node-js-for-use-with-actions
14+
- name: Setup Node
15+
uses: actions/setup-node@v1
16+
with:
17+
node-version: '12.x'
18+
19+
- run: npm install
20+
21+
- run: npm test
22+
23+
- run: npm run build
24+
25+
- name: upload build artifacts
26+
uses: actions/upload-artifact@v1
27+
with:
28+
name: build
29+
path: build

templates/commit0/commit0.tmpl

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,8 @@ frontend:
1919
framework: {{.FrontendFramework}}
2020
ci:
2121
system: circleci
22-
buildImage: react/react
23-
buildTag: 1234
24-
buildCommand: make build
25-
testCommand: make test
2622
app:
2723
name: {{.ProjectName}}
28-
header:
29-
enabled: true
30-
account:
31-
enabled: true
32-
required: false
33-
sidenav:
34-
enabled: true
35-
items:
36-
- path: /
37-
label: Home
38-
icon: home
39-
- path: /account
40-
label: Account
41-
icon: account_circle
42-
views:
43-
- path: /account
44-
component: account
45-
- path: /
46-
component: home
47-
4824

4925
services:
5026
{{range .Services}}

0 commit comments

Comments
 (0)