Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

COMMIT0-57 api for generating project #68

Merged
merged 10 commits into from
Nov 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ The best way then to use this is to add an alias, then you can use the CLI as if
## Usage

1) To create a project run `commit0 create [PROJECT_NAME]`
2) A folder will be created and within that update the `commit0.yml` and then run `commit0 generate -l=[LANGUAGE OF CHOICE] eg. go`
2) A folder will be created and within that update the `commit0.yml` and then run `commit0 generate`
3) You will see that there is now an idl folder created.
4) Within the idl folder modify the the protobuf services generated with your desired methods
5) Go up to the parent directory and re run `commit0 generate -l=[LANGUAGE OF CHOICE]`
Expand Down
18 changes: 18 additions & 0 deletions cmd/commit0_ui.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import (
"github.com/commitdev/commit0/internal/api"
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(commit0api)
}

var commit0api = &cobra.Command{
Use: "ui",
Short: "Run Commit0 Api",
Run: func(cmd *cobra.Command, args []string) {
api.Commit0Api()
},
}
23 changes: 22 additions & 1 deletion cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,34 @@ func Create(projectName string, outDir string, t *templator.Templator) string {
}
var wg sync.WaitGroup

util.TemplateFileIfDoesNotExist(rootDir, "commit0.yml", t.Commit0, &wg, projectName)
defaultProjConfig := defaultProjConfig(projectName)

util.TemplateFileIfDoesNotExist(rootDir, util.CommitYml, t.Commit0, &wg, defaultProjConfig)
util.TemplateFileIfDoesNotExist(rootDir, ".gitignore", t.GitIgnore, &wg, projectName)

wg.Wait()
return rootDir
}

func defaultProjConfig(projectName string) util.ProjectConfiguration {
return util.ProjectConfiguration{
ProjectName: projectName,
FrontendFramework: "react",
Organization: "mycompany",
Description: "",
Maintainers: []util.Maintainer{{
Name: "bob",
Email: "bob@test.com",
}},
Services: []util.Service{{
Name: "User",
Description: "User Service",
Language: "go",
GitRepo: "github.com/test/repo",
}},
}
}

var createCmd = &cobra.Command{
Use: "create",
Short: "Create new project with provided name.",
Expand Down
3 changes: 2 additions & 1 deletion cmd/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/commitdev/commit0/cmd"
"github.com/commitdev/commit0/internal/templator"
"github.com/commitdev/commit0/internal/util"
"github.com/gobuffalo/packr/v2"
)

Expand All @@ -25,7 +26,7 @@ func TestCreateWorks(t *testing.T) {
root := cmd.Create(projectName, tmpdir, templator)
defer os.RemoveAll(tmpdir)

st, err := os.Stat(path.Join(root, "commit0.yml"))
st, err := os.Stat(path.Join(root, util.CommitYml))
if err != nil {
t.Fatal(err)
}
Expand Down
73 changes: 3 additions & 70 deletions cmd/generate.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,19 @@
package cmd

import (
"log"
"sync"

"github.com/commitdev/commit0/internal/config"
"github.com/commitdev/commit0/internal/generate/golang"
"github.com/commitdev/commit0/internal/generate/kubernetes"
"github.com/commitdev/commit0/internal/generate/proto"
"github.com/commitdev/commit0/internal/generate/react"
"github.com/commitdev/commit0/internal/generate"
"github.com/commitdev/commit0/internal/templator"
"github.com/commitdev/commit0/internal/util"
"github.com/gobuffalo/packr/v2"
"github.com/kyokomi/emoji"
"github.com/logrusorgru/aurora"
"github.com/spf13/cobra"
)

var configPath string

const (
Go = "go"
React = "react"
Kubernetes = "kubernetes"
)

var supportedLanguages = [...]string{Go, React, Kubernetes}

func init() {

generateCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "commit0.yml", "config path")
generateCmd.PersistentFlags().StringVarP(&configPath, "config", "c", util.CommitYml, "config path")

rootCmd.AddCommand(generateCmd)
}
Expand All @@ -45,58 +29,7 @@ var generateCmd = &cobra.Command{
cfg := config.LoadConfig(configPath)
cfg.Print()

var wg sync.WaitGroup
if !ValidLanguage(cfg.Frontend.Framework) {
log.Fatalln(aurora.Red(emoji.Sprintf(":exclamation: '%s' is not a supported framework.", cfg.Frontend.Framework)))
}

for _, s := range cfg.Services {
if !ValidLanguage(cfg.Frontend.Framework) {
log.Fatalln(aurora.Red(emoji.Sprintf(":exclamation: '%s' in service '%s' is not a supported language.", s.Name, s.Language)))
}
}

for _, s := range cfg.Services {
switch s.Language {
case Go:
log.Println(aurora.Cyan(emoji.Sprintf("Creating Go service")))
proto.Generate(t, cfg, s, &wg)
golang.Generate(t, cfg, s, &wg)
}
}

if cfg.Infrastructure.AWS.EKS.ClusterName != "" {
log.Println(aurora.Cyan(emoji.Sprintf("Generating Terraform")))
kubernetes.Generate(t, cfg, &wg)
}

// @TODO : This strucuture probably needs to be adjusted. Probably too generic.
switch cfg.Frontend.Framework {
case React:
log.Println(aurora.Cyan(emoji.Sprintf("Creating React frontend")))
react.Generate(t, cfg, &wg)
}

util.TemplateFileIfDoesNotExist("", "README.md", t.Readme, &wg, templator.GenericTemplateData{*cfg})

// Wait for all the templates to be generated
wg.Wait()

log.Println("Executing commands")
// @TODO : Move this stuff to another command? Or genericize it a bit.
if cfg.Infrastructure.AWS.EKS.Deploy {
kubernetes.Execute(cfg)
}
generate.GenerateArtifactsHelper(t, cfg, "")

},
}

func ValidLanguage(language string) bool {
for _, l := range supportedLanguages {
if l == language {
return true
}
}

return false
}
1 change: 0 additions & 1 deletion example/hello-world/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ replace github.com/yourrepo/hello-world-idl => ./hello-world-idl
require (
github.com/grpc-ecosystem/grpc-gateway v1.11.3 // indirect
github.com/yourrepo/hello-world-idl v0.0.0
google.golang.org/appengine v1.4.0 // indirect
google.golang.org/genproto v0.0.0-20191009194640-548a555dbc03 // indirect
google.golang.org/grpc v1.24.0 // indirect
)
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.12
require (
github.com/gobuffalo/logger v1.0.1 // indirect
github.com/gobuffalo/packr/v2 v2.5.2
github.com/gorilla/mux v1.7.3
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
github.com/k0kubun/pp v3.0.1+incompatible
github.com/kyokomi/emoji v2.1.0+incompatible
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ github.com/gobuffalo/packd v0.3.0 h1:eMwymTkA1uXsqxS0Tpoop3Lc0u3kTfiMBE6nKtQU4g4
github.com/gobuffalo/packd v0.3.0/go.mod h1:zC7QkmNkYVGKPw4tHpBQ+ml7W/3tIebgeo1b36chA3Q=
github.com/gobuffalo/packr/v2 v2.5.2 h1:4EvjeIpQLZuRIljwnidYgbRXbr1yIzVRrESiLjqKj6s=
github.com/gobuffalo/packr/v2 v2.5.2/go.mod h1:sgEE1xNZ6G0FNN5xn9pevVu4nywaxHvgup67xisti08=
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/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
Expand Down
39 changes: 39 additions & 0 deletions internal/api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Commit0 Api

## Usage
- To run:
`commit0 api`
- Endpoint:
- POST `localhost:8080/{version}/generate`
- Post request body json example:
``` {
"projectName":"funApp",
"frontendFramework":"react",
"organization":"commit org",
"description":"this app will do amazing things",
"maintainers":[
{
"name":"Lill",
"email":"ll@gmail.com"
},
{
"name":"Pi",
"email":"pi@live.ca"
}
],
"services":[
{
"name":"user",
"description":"user service",
"language":"go",
"gitRepo":"github.com/user"
},
{
"name":"account",
"description":"bank account service",
"language":"go",
"gitRepo":"github.com/account"
}
]
}
```
53 changes: 53 additions & 0 deletions internal/api/create_project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package api

import (
"log"
"os"
"path"
"sync"

"github.com/commitdev/commit0/internal/config"
"github.com/commitdev/commit0/internal/generate"
"github.com/commitdev/commit0/internal/templator"
"github.com/commitdev/commit0/internal/util"
"github.com/gobuffalo/packr/v2"
)

func createProject(projectConfig util.ProjectConfiguration) string {
templates := packr.New("templates", "../../templates")
t := templator.NewTemplator(templates)
outDir := "./"
rootDir := path.Join(outDir, projectConfig.ProjectName)
log.Printf("Creating project %s.", projectConfig.ProjectName)
err := os.MkdirAll(rootDir, os.ModePerm)

if os.IsExist(err) {
log.Fatalf("Directory %v already exists! Error: %v", projectConfig.ProjectName, err)
} else if err != nil {
log.Fatalf("Error creating root: %v ", err)
}
var wg sync.WaitGroup

util.TemplateFileIfDoesNotExist(rootDir, util.CommitYml, t.Commit0, &wg, projectConfig)

util.TemplateFileIfDoesNotExist(rootDir, ".gitignore", t.GitIgnore, &wg, projectConfig.ProjectName)

wg.Wait()

GenerateArtifacts(projectConfig)

return rootDir
}

func GenerateArtifacts(projectConfig util.ProjectConfiguration) {

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

generatedYml := path.Join(projectConfig.ProjectName, util.CommitYml)

cfg := config.LoadConfig(generatedYml)
cfg.Print()

generate.GenerateArtifactsHelper(t, cfg, projectConfig.ProjectName)
}
40 changes: 40 additions & 0 deletions internal/api/generate_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package api

import (
"encoding/json"
"github.com/gorilla/mux"
"log"
"net/http"

"github.com/commitdev/commit0/internal/util"
)

func generateProject(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch req.Method {
case "POST":
decoder := json.NewDecoder(req.Body)
var projectConfig util.ProjectConfiguration
err := decoder.Decode(&projectConfig)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to add validation for post request

if err != nil {
panic(err)
}
log.Println(projectConfig.ProjectName)
createProject(projectConfig)
w.WriteHeader(http.StatusCreated)
w.Write([]byte(`{"message": "Post successful"}`))

default:
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(`{"message": "Not found"}`))
}

}

func Commit0Api() {
var router = mux.NewRouter()
var api = router.PathPrefix("/v1/generate").Subrouter()
api.NotFoundHandler = http.HandlerFunc(generateProject)

log.Fatal(http.ListenAndServe(":8080", router))
}
4 changes: 2 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"gopkg.in/yaml.v2"
)

type maintainers struct {
type maintainer struct {
Name string
Email string
}
Expand Down Expand Up @@ -63,7 +63,7 @@ type Commit0Config struct {
Organization string
Name string
Description string
Maintainers []maintainers
Maintainers []maintainer
Services []Service
Frontend frontend
Infrastructure infrastructure
Expand Down