Skip to content

Commit 92b5b74

Browse files
committed
#64 : Remove -l flag, move all configuration to yaml, a lot of refactoring
1 parent 3adee73 commit 92b5b74

File tree

39 files changed

+324
-363
lines changed

39 files changed

+324
-363
lines changed

cmd/generate.go

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ import (
55
"sync"
66

77
"github.com/commitdev/commit0/internal/config"
8-
"github.com/commitdev/commit0/internal/generate/ci"
9-
"github.com/commitdev/commit0/internal/generate/docker"
108
"github.com/commitdev/commit0/internal/generate/golang"
11-
"github.com/commitdev/commit0/internal/generate/http"
129
"github.com/commitdev/commit0/internal/generate/kubernetes"
1310
"github.com/commitdev/commit0/internal/generate/proto"
1411
"github.com/commitdev/commit0/internal/generate/react"
@@ -19,7 +16,6 @@ import (
1916
)
2017

2118
var configPath string
22-
var language string
2319

2420
const (
2521
Go = "go"
@@ -32,7 +28,6 @@ var supportedLanguages = [...]string{Go, React, Kubernetes}
3228
func init() {
3329

3430
generateCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "commit0.yml", "config path")
35-
generateCmd.PersistentFlags().StringVarP(&language, "language", "l", "", "language to generate project in")
3631

3732
rootCmd.AddCommand(generateCmd)
3833
}
@@ -41,54 +36,58 @@ var generateCmd = &cobra.Command{
4136
Use: "generate",
4237
Short: "Generate idl & application folders",
4338
Run: func(cmd *cobra.Command, args []string) {
44-
if !ValidLanguage() {
45-
log.Fatalf("'%s' is not a supported language.", language)
46-
}
4739

4840
templates := packr.New("templates", "../templates")
4941
t := templator.NewTemplator(templates)
5042

5143
cfg := config.LoadConfig(configPath)
52-
cfg.Language = language
5344
cfg.Print()
5445

5546
var wg sync.WaitGroup
56-
switch language {
57-
case Go:
58-
proto.Generate(t, cfg, &wg)
59-
golang.Generate(t, cfg, &wg)
47+
if !ValidLanguage(cfg.Frontend.Framework) {
48+
log.Fatalf("'%s' is not a supported framework.", cfg.Frontend.Framework)
49+
}
6050

61-
docker.GenerateGoAppDockerFile(t, cfg, &wg)
62-
docker.GenerateGoDockerCompose(t, cfg, &wg)
63-
case React:
64-
react.Generate(t, cfg, &wg)
65-
case Kubernetes:
66-
kubernetes.Generate(t, cfg, &wg)
51+
for _, s := range cfg.Services {
52+
if !ValidLanguage(cfg.Frontend.Framework) {
53+
log.Fatalf("'%s' in service '%s' is not a supported language.", s.Name, s.Language)
54+
}
6755
}
6856

69-
util.TemplateFileIfDoesNotExist("", "README.md", t.Readme, &wg, cfg)
57+
for _, s := range cfg.Services {
58+
switch s.Language {
59+
case Go:
60+
log.Printf("Creating Go service")
61+
proto.Generate(t, cfg, s, &wg)
62+
golang.Generate(t, cfg, s, &wg)
63+
}
64+
}
7065

71-
if cfg.CI.System != "" {
72-
ci.Generate(t.CI, cfg, ".", &wg)
66+
if cfg.Infrastructure.AWS.EKS.ClusterName != "" {
67+
kubernetes.Generate(t, cfg, &wg)
7368
}
7469

75-
if cfg.Network.Http.Enabled {
76-
http.GenerateHTTPGW(t, cfg, &wg)
77-
docker.GenerateGoHTTPGWDockerFile(t, cfg, &wg)
70+
// @TODO : This strucuture probably needs to be adjusted. Probably too generic.
71+
switch cfg.Frontend.Framework {
72+
case React:
73+
react.Generate(t, cfg, &wg)
7874
}
7975

76+
util.TemplateFileIfDoesNotExist("", "README.md", t.Readme, &wg, cfg)
77+
8078
// Wait for all the templates to be generated
8179
wg.Wait()
8280

83-
switch language {
84-
case Kubernetes:
81+
log.Println("Executing commands")
82+
// @TODO : Move this stuff to another command? Or genericize it a bit.
83+
if cfg.Infrastructure.AWS.EKS.Deploy {
8584
kubernetes.Execute(cfg)
8685
}
8786

8887
},
8988
}
9089

91-
func ValidLanguage() bool {
90+
func ValidLanguage(language string) bool {
9291
for _, l := range supportedLanguages {
9392
if l == language {
9493
return true

internal/config/config.go

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,71 +8,80 @@ import (
88
"gopkg.in/yaml.v2"
99
)
1010

11-
type Maintainers struct {
11+
type maintainers struct {
1212
Name string
1313
Email string
1414
}
1515

16-
type Grpc struct {
16+
type grpc struct {
1717
Host string
1818
Port int
1919
}
2020

21-
type Graphql struct {
21+
type graphql struct {
2222
Enabled bool
2323
Port int
2424
}
2525

26-
type Http struct {
26+
type http struct {
2727
Enabled bool
2828
Port int
2929
}
3030

31-
type Web struct {
31+
type web struct {
3232
Enabled bool
3333
Port int
3434
}
3535

36-
type Network struct {
37-
Grpc Grpc
38-
Http Http
39-
Web Web
40-
Graphql Graphql
36+
type network struct {
37+
Grpc grpc
38+
Http http
39+
Web web
40+
Graphql graphql
4141
}
4242

4343
type Service struct {
4444
Name string
4545
Description string
46+
Language string
47+
GitRepo string `yaml:"gitRepo"`
48+
DockerRepo string `yaml:"dockerRepo"`
49+
Network network
50+
CI CI
4651
}
4752

4853
type CI struct {
49-
System string `yaml:"system"`
50-
BuildImage string `yaml:"build-image"`
51-
BuildCommand string `yaml:"build-command"`
52-
TestCommand string `yaml:"test-command"`
53-
LanguageVersion string `yaml:"language-version"`
54+
System string
55+
Language string
56+
BuildImage string `yaml:"buildImage"`
57+
BuildTag string `yaml:"buildTag"`
58+
BuildCommand string `yaml:"buildCommand"`
59+
TestCommand string `yaml:"testCommand"`
5460
}
5561

5662
type Commit0Config struct {
57-
Language string `yaml:"string"`
58-
Organization string `yaml:"organization"`
59-
Name string `yaml:"name"`
60-
Description string `yaml:"description"`
61-
GitRepo string `yaml:"git-repo"`
62-
DockerRepo string `yaml:"docker-repo"`
63-
Maintainers []Maintainers `yaml:"maintainers"`
64-
Network Network `yaml:"network"`
65-
Services []Service `yaml:"services"`
66-
React React `yaml:"react"`
67-
Kubernetes Kubernetes `yaml:"kubernetes"`
68-
CI CI `yaml:"ci"`
63+
Organization string
64+
Name string
65+
Description string
66+
Maintainers []maintainers
67+
Services []Service
68+
Frontend frontend
69+
Infrastructure infrastructure
6970
}
7071

71-
type Kubernetes struct {
72-
ClusterName string `yaml:"clusterName"`
73-
Deploy bool `yaml:"deploy"`
74-
AWSAccountId string `yaml:"awsAccountId"`
75-
AWSRegion string `yaml:"awsRegion"`
72+
type infrastructure struct {
73+
AWS aws
74+
}
75+
76+
type aws struct {
77+
AccountId string `yaml:"accountId"`
78+
Region string
79+
EKS eks
80+
}
81+
82+
type eks struct {
83+
ClusterName string `yaml:"clusterName"`
84+
Deploy bool
7685
}
7786

7887
func LoadConfig(filePath string) *Commit0Config {

internal/config/react.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ type reactView struct {
2828
Component string
2929
}
3030

31-
type React struct {
32-
App reactApp
33-
Account reactAccount
34-
Header reactHeader
35-
Sidenav reactSidenav
36-
Views []reactView
31+
type frontend struct {
32+
Framework string
33+
App reactApp
34+
Account reactAccount
35+
Header reactHeader
36+
Sidenav reactSidenav
37+
Views []reactView
38+
CI CI
3739
}

internal/generate/ci/generate.go

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,54 +19,42 @@ const (
1919

2020
type CIGenerationError struct {
2121
err string
22-
config *config.Commit0Config
22+
config config.CI
2323
}
2424

2525
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

2929
// Generate a CI configuration file based on your language and CI system
30-
func Generate(templator *templator.CITemplator, config *config.Commit0Config, basePath string, wg *sync.WaitGroup) error {
31-
switch config.Language {
32-
case "go":
33-
if config.CI.LanguageVersion == "" {
34-
config.CI.LanguageVersion = defaultGoVersion
35-
}
36-
if config.CI.BuildImage == "" {
37-
config.CI.BuildImage = fmt.Sprintf("%s:%s", defaultGoDockerImage, config.CI.LanguageVersion)
38-
}
39-
if config.CI.BuildCommand == "" {
40-
config.CI.BuildCommand = defaultBuildCommand
41-
}
42-
if config.CI.TestCommand == "" {
43-
config.CI.TestCommand = defaultTestCommand
44-
}
45-
default:
46-
return &CIGenerationError{"Unsupported Language", config}
47-
}
30+
func Generate(t *templator.CITemplator, cfg *config.Commit0Config, ciConfig config.CI, basePath string, wg *sync.WaitGroup) error {
4831

4932
var ciConfigPath string
5033
var ciFilename string
5134
var ciTemp *template.Template
5235

53-
switch config.CI.System {
36+
switch ciConfig.System {
5437
case "jenkins":
5538
ciConfigPath = basePath
5639
ciFilename = "Jenkinsfile"
57-
ciTemp = templator.Jenkins
40+
ciTemp = t.Jenkins
5841
case "circleci":
5942
ciConfigPath = fmt.Sprintf("%s/%s", basePath, ".circleci/")
6043
ciFilename = "config.yml"
61-
ciTemp = templator.CircleCI
44+
ciTemp = t.CircleCI
6245
case "travisci":
6346
ciConfigPath = basePath
6447
ciFilename = ".travis.yml"
65-
ciTemp = templator.TravisCI
48+
ciTemp = t.TravisCI
6649
default:
67-
return &CIGenerationError{"Unsupported CI System", config}
50+
return &CIGenerationError{"Unsupported CI System", ciConfig}
51+
}
52+
53+
data := templator.CITemplateData{
54+
*cfg,
55+
ciConfig,
6856
}
69-
util.TemplateFileIfDoesNotExist(ciConfigPath, ciFilename, ciTemp, wg, config)
57+
util.TemplateFileIfDoesNotExist(ciConfigPath, ciFilename, ciTemp, wg, data)
7058

7159
return nil
7260
}

internal/generate/ci/generate_test.go

Lines changed: 5 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -10,73 +10,20 @@ import (
1010
"github.com/commitdev/commit0/internal/templator"
1111
)
1212

13-
func TestGenerateJenkins(t *testing.T) {
14-
testConf := &config.Commit0Config{
15-
Language: "go",
16-
CI: config.CI{
17-
System: "jenkins",
18-
},
19-
}
20-
testTemp := &templator.CITemplator{
21-
Jenkins: &template.Template{},
22-
CircleCI: &template.Template{},
23-
TravisCI: &template.Template{},
24-
}
25-
var wg sync.WaitGroup
26-
err := ci.Generate(testTemp, testConf, "/dev/null", &wg)
27-
if err != nil {
28-
t.Errorf("Error when executing test. %s", err)
29-
}
30-
31-
expectedBuildImage := "golang/golang:1.12"
32-
actualBuildImage := testConf.CI.BuildImage
33-
if actualBuildImage != expectedBuildImage {
34-
t.Errorf("want: %s, got: %s", expectedBuildImage, actualBuildImage)
35-
}
36-
37-
expectedBuildCommand := "make build"
38-
actualBuildCommand := testConf.CI.BuildCommand
39-
if actualBuildCommand != expectedBuildCommand {
40-
t.Errorf("want: %s, got: %s", expectedBuildCommand, actualBuildCommand)
13+
func TestGenerateInvalidCISystem(t *testing.T) {
14+
testConf := &config.Commit0Config{}
15+
testCI := config.CI{
16+
System: "invalidCISystem",
4117
}
4218

43-
expectedTestCommand := "make test"
44-
actualTestCommand := testConf.CI.TestCommand
45-
if actualTestCommand != expectedTestCommand {
46-
t.Errorf("want: %s, got: %s", expectedTestCommand, actualTestCommand)
47-
}
48-
}
49-
50-
func TestGenerateInvalidLanguage(t *testing.T) {
51-
testConf := &config.Commit0Config{
52-
Language: "invalidLanguage",
53-
}
5419
testTemp := &templator.CITemplator{
5520
Jenkins: &template.Template{},
5621
CircleCI: &template.Template{},
5722
TravisCI: &template.Template{},
5823
}
59-
var wg sync.WaitGroup
60-
err := ci.Generate(testTemp, testConf, "/dev/null", &wg)
61-
if err == nil {
62-
t.Errorf("Error should be thrown with invalid language specified. %s", err.Error())
63-
}
64-
}
6524

66-
func TestGenerateInvalidCISystem(t *testing.T) {
67-
testConf := &config.Commit0Config{
68-
Language: "go",
69-
CI: config.CI{
70-
System: "invalidCISystem",
71-
},
72-
}
73-
testTemp := &templator.CITemplator{
74-
Jenkins: &template.Template{},
75-
CircleCI: &template.Template{},
76-
TravisCI: &template.Template{},
77-
}
7825
var wg sync.WaitGroup
79-
err := ci.Generate(testTemp, testConf, "/dev/null", &wg)
26+
err := ci.Generate(testTemp, testConf, testCI, "/dev/null", &wg)
8027
if err == nil {
8128
t.Errorf("Error should be thrown with invalid ci system specified. %s", err.Error())
8229
}

0 commit comments

Comments
 (0)