diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 000000000..3a624ed3a --- /dev/null +++ b/.travis.yml @@ -0,0 +1,9 @@ +language: go +script: + - env GO111MODULE=on go build + - env GO111MODULE=on go test ./test + +go: + - 1.11.x + - 1.12.x + - master diff --git a/Makefile b/Makefile index 5c54453d7..6d3b456a5 100644 --- a/Makefile +++ b/Makefile @@ -30,6 +30,9 @@ deps-go: build-deps: go install github.com/gobuffalo/packr/v2/packr2 +check: + go test ./test + fmt: go fmt ./... diff --git a/README.md b/README.md index 6fc59a004..4b1827099 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # Commit0 [POC] +[![Build Status](https://travis-ci.org/commitdev/commit0.svg)](https://travis-ci.org/commitdev/commit0) + Status: currently poc Commit0 intends to be a multi-language service generator. The intention is to create a modular monolith, which is easy to seperate at a later stage when the boundries are completely understood. diff --git a/cmd/commit0.go b/cmd/commit0.go index f54cf238b..8d74067ef 100644 --- a/cmd/commit0.go +++ b/cmd/commit0.go @@ -3,6 +3,7 @@ package cmd import ( "fmt" "github.com/commitdev/commit0/templator" + "github.com/gobuffalo/packr/v2" "github.com/spf13/cobra" "os" ) @@ -17,8 +18,13 @@ var rootCmd = &cobra.Command{ }, } -func Execute(templates *templator.Templator) { - Templator = templates +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) diff --git a/cmd/create.go b/cmd/create.go index dd0fc334c..5a1bf528c 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -1,9 +1,9 @@ package cmd import ( - "fmt" "log" "os" + "path" "github.com/spf13/cobra" ) @@ -13,6 +13,36 @@ func init() { rootCmd.AddCommand(createCmd) } +func Create(projectName string, outDir string) string { + rootDir := path.Join(outDir, projectName) + log.Printf("Creating project %s.", projectName) + err := os.MkdirAll(rootDir, os.ModePerm) + + if os.IsExist(err) { + log.Fatalf("Directory %v already exists! Error: %v", projectName, err) + } else if err != nil { + log.Fatalf("Error creating root: %v ", err) + } + + commit0ConfigPath := path.Join(rootDir, "commit0.yml") + log.Printf("%s", commit0ConfigPath) + + f, err := os.Create(commit0ConfigPath) + if err != nil { + log.Printf("Error creating commit0 config: %v", err) + } + Templator.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) + + return rootDir +} + var createCmd = &cobra.Command{ Use: "create", Short: "Create new project with provided name.", @@ -23,30 +53,6 @@ var createCmd = &cobra.Command{ projectName := args[0] - rootDir := fmt.Sprintf("./%v", projectName) - - log.Printf("Creating project %s.", projectName) - - err := os.Mkdir(rootDir, os.ModePerm) - if os.IsExist(err) { - log.Fatalf("Directory %v already exists! Error: %v", projectName, err) - } else if err != nil { - log.Fatalf("Error creating root: %v ", err) - } - - commit0ConfigPath := fmt.Sprintf("%v/commit0.yml", rootDir) - - f, err := os.Create(commit0ConfigPath) - if err != nil { - log.Printf("Error creating commit0 config: %v", err) - } - Templator.Commit0.Execute(f, projectName) - - gitIgnorePath := fmt.Sprintf("%v/.gitignore", rootDir) - f, err = os.Create(gitIgnorePath) - if err != nil { - log.Printf("Error creating commit0 config: %v", err) - } - Templator.GitIgnore.Execute(f, projectName) + Create(projectName, "./") }, } diff --git a/main.go b/main.go index 825c5f7fb..7c249737b 100644 --- a/main.go +++ b/main.go @@ -2,12 +2,8 @@ package main import ( "github.com/commitdev/commit0/cmd" - "github.com/commitdev/commit0/templator" - "github.com/gobuffalo/packr/v2" ) func main() { - templates := packr.New("templates", "./templates") - templator := templator.NewTemplator(templates) - cmd.Execute(templator) + cmd.Execute() } diff --git a/templator/templator.go b/templator/templator.go index 855aefaad..8e8b78fff 100644 --- a/templator/templator.go +++ b/templator/templator.go @@ -22,7 +22,7 @@ type GoTemplator struct { } type Templator struct { - Commit0 *template.Template + Commit0 *template.Template GitIgnore *template.Template MakefileTemplate *template.Template ProtoHealthTemplate *template.Template diff --git a/test/create_test.go b/test/create_test.go new file mode 100644 index 000000000..dc3ab378f --- /dev/null +++ b/test/create_test.go @@ -0,0 +1,34 @@ +package main + +import ( + "github.com/commitdev/commit0/cmd" + "io/ioutil" + "os" + "path" + "testing" +) + +func TestInitWorks(t *testing.T) { + cmd.Init() +} + +func TestCreateWorks(t *testing.T) { + tmpdir, err := ioutil.TempDir("", "commit0-") + if err != nil { + t.Fatal(err) + } + + projectName := "test-project" + + root := cmd.Create(projectName, tmpdir) + defer os.RemoveAll(tmpdir) + + st, err := os.Stat(path.Join(root, "commit0.yml")) + if err != nil { + t.Fatal(err) + } + + if st.Size() == 0 { + t.Fatalf("commit0.yml is empty") + } +}