Skip to content

Commit

Permalink
Merge pull request #51 from commitdev/restructure-project
Browse files Browse the repository at this point in the history
Move some dirs from root into internal, shuffle some stuff around in cmd
  • Loading branch information
bmonkman committed Oct 24, 2019
2 parents 4fc10c3 + ff8e79f commit ba409d9
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 62 deletions.
15 changes: 3 additions & 12 deletions cmd/commit0.go
Expand Up @@ -2,29 +2,20 @@ package cmd

import (
"fmt"
"github.com/commitdev/commit0/templator"
"github.com/gobuffalo/packr/v2"
"github.com/spf13/cobra"
"os"
)

var Templator *templator.Templator
"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
Use: "commit0",
Short: "Commit0 is a moduler service generator.",
Short: "Commit0 is a modular service generator.",
Long: `TODO`,
Run: func(cmd *cobra.Command, args []string) {
},
}

func Init() {
templates := packr.New("templates", "../templates")
Templator = templator.NewTemplator(templates)
}

func Execute() {
Init()
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
14 changes: 9 additions & 5 deletions cmd/create.go
Expand Up @@ -5,15 +5,16 @@ import (
"os"
"path"

"github.com/commitdev/commit0/internal/templator"
"github.com/gobuffalo/packr/v2"
"github.com/spf13/cobra"
)

func init() {

rootCmd.AddCommand(createCmd)
}

func Create(projectName string, outDir string) string {
func Create(projectName string, outDir string, t *templator.Templator) string {
rootDir := path.Join(outDir, projectName)
log.Printf("Creating project %s.", projectName)
err := os.MkdirAll(rootDir, os.ModePerm)
Expand All @@ -31,14 +32,14 @@ func Create(projectName string, outDir string) string {
if err != nil {
log.Printf("Error creating commit0 config: %v", err)
}
Templator.Commit0.Execute(f, projectName)
t.Commit0.Execute(f, projectName)

gitIgnorePath := path.Join(rootDir, ".gitignore")
f, err = os.Create(gitIgnorePath)
if err != nil {
log.Printf("Error creating commit0 config: %v", err)
}
Templator.GitIgnore.Execute(f, projectName)
t.GitIgnore.Execute(f, projectName)

return rootDir
}
Expand All @@ -51,8 +52,11 @@ var createCmd = &cobra.Command{
log.Fatalf("Project name cannot be empty!")
}

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

projectName := args[0]

Create(projectName, "./")
Create(projectName, "./", t)
},
}
14 changes: 8 additions & 6 deletions cmd/create_test.go
@@ -1,16 +1,15 @@
package cmd_test

import (
"github.com/commitdev/commit0/cmd"
"io/ioutil"
"os"
"path"
"testing"
)

func TestInitWorks(t *testing.T) {
cmd.Init()
}
"github.com/commitdev/commit0/cmd"
"github.com/commitdev/commit0/internal/templator"
"github.com/gobuffalo/packr/v2"
)

func TestCreateWorks(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "commit0-")
Expand All @@ -20,7 +19,10 @@ func TestCreateWorks(t *testing.T) {

projectName := "test-project"

root := cmd.Create(projectName, tmpdir)
templates := packr.New("templates", "../templates")
templator := templator.NewTemplator(templates)

root := cmd.Create(projectName, tmpdir, templator)
defer os.RemoveAll(tmpdir)

st, err := os.Stat(path.Join(root, "commit0.yml"))
Expand Down
32 changes: 18 additions & 14 deletions cmd/generate.go
@@ -1,15 +1,16 @@
package cmd

import (
"github.com/commitdev/commit0/config"
"github.com/commitdev/commit0/generate/docker"
"github.com/commitdev/commit0/generate/golang"
"github.com/commitdev/commit0/generate/http"
"github.com/commitdev/commit0/generate/proto"
"github.com/commitdev/commit0/generate/react"

"log"

"github.com/commitdev/commit0/internal/config"
"github.com/commitdev/commit0/internal/generate/docker"
"github.com/commitdev/commit0/internal/generate/golang"
"github.com/commitdev/commit0/internal/generate/http"
"github.com/commitdev/commit0/internal/generate/proto"
"github.com/commitdev/commit0/internal/generate/react"
"github.com/commitdev/commit0/internal/templator"
"github.com/gobuffalo/packr/v2"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -39,23 +40,26 @@ var generateCmd = &cobra.Command{
log.Fatalf("'%s' is not a supported language.", language)
}

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

cfg := config.LoadConfig(configPath)
cfg.Language = language
cfg.Print()

switch language {
case Go:
proto.Generate(Templator, cfg)
golang.Generate(Templator, cfg)
docker.GenerateGoAppDockerFile(Templator, cfg)
docker.GenerateGoDockerCompose(Templator, cfg)
proto.Generate(t, cfg)
golang.Generate(t, cfg)
docker.GenerateGoAppDockerFile(t, cfg)
docker.GenerateGoDockerCompose(t, cfg)
case React:
react.Generate(Templator, cfg)
react.Generate(t, cfg)
}

if cfg.Network.Http.Enabled {
http.GenerateHttpGW(Templator, cfg)
docker.GenerateGoHttpGWDockerFile(Templator, cfg)
http.GenerateHttpGW(t, cfg)
docker.GenerateGoHttpGWDockerFile(t, cfg)
}
},
}
Expand Down
File renamed without changes.
File renamed without changes.
@@ -1,10 +1,9 @@
package docker

import (
"github.com/commitdev/commit0/util"

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

func GenerateGoAppDockerFile(templator *templator.Templator, config *config.Commit0Config) {
Expand All @@ -17,4 +16,4 @@ func GenerateGoHttpGWDockerFile(templator *templator.Templator, config *config.C

func GenerateGoDockerCompose(templator *templator.Templator, config *config.Commit0Config) {
util.TemplateFileIfDoesNotExist("", "docker-compose.yml", templator.Docker.DockerCompose, config)
}
}
Expand Up @@ -2,12 +2,12 @@ package golang

import (
"fmt"
"github.com/commitdev/commit0/util"

"github.com/commitdev/commit0/config"
"github.com/commitdev/commit0/templator"
"log"
"os"

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

func Generate(templator *templator.Templator, config *config.Commit0Config) {
Expand Down
@@ -1,10 +1,9 @@
package http

import (
"github.com/commitdev/commit0/util"

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

func GenerateHttpGW(templator *templator.Templator, config *config.Commit0Config) {
Expand Down
Expand Up @@ -3,13 +3,13 @@ package proto
import (
"bytes"
"fmt"

"github.com/commitdev/commit0/config"
"github.com/commitdev/commit0/templator"
"github.com/commitdev/commit0/util"
"log"
"os"
"os/exec"

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

func Generate(templator *templator.Templator, config *config.Commit0Config) {
Expand Down
@@ -1,8 +1,8 @@
package react

import (
"github.com/commitdev/commit0/config"
"github.com/commitdev/commit0/templator"
"github.com/commitdev/commit0/internal/config"
"github.com/commitdev/commit0/internal/templator"
)

func Generate(templator *templator.Templator, config *config.Commit0Config) {
Expand Down
4 changes: 2 additions & 2 deletions templator/templator.go → internal/templator/templator.go
Expand Up @@ -5,8 +5,8 @@ import (
"strings"
"text/template"

"github.com/commitdev/commit0/config"
"github.com/commitdev/commit0/util"
"github.com/commitdev/commit0/internal/config"
"github.com/commitdev/commit0/internal/util"
"github.com/gobuffalo/packr/v2"
"github.com/gobuffalo/packr/v2/file"
)
Expand Down
2 changes: 1 addition & 1 deletion util/util.go → internal/util/util.go
Expand Up @@ -46,7 +46,7 @@ func TemplateFileIfDoesNotExist(fileDir string, fileName string, template *templ
fullFilePath := path.Join(fileDir, fileName)

if _, err := os.Stat(fullFilePath); os.IsNotExist(err) {
if (fileDir != "") {
if fileDir != "" {
err := CreateDirIfDoesNotExist(fileDir)
if err != nil {
log.Printf("Error creating directory %v: %v", fullFilePath, err)
Expand Down
4 changes: 1 addition & 3 deletions main.go
@@ -1,8 +1,6 @@
package main

import (
"github.com/commitdev/commit0/cmd"
)
import "github.com/commitdev/commit0/cmd"

func main() {
cmd.Execute()
Expand Down

0 comments on commit ba409d9

Please sign in to comment.