From 68a1feea3ca26a8a8b64ec3c277a57c34d85c7b9 Mon Sep 17 00:00:00 2001 From: William Casarin Date: Thu, 10 Oct 2019 16:51:31 -0700 Subject: [PATCH 1/7] ocd: alignment Signed-off-by: William Casarin --- templator/templator.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From efec50fc980a0372abde657c5cdeaaf91d378fc6 Mon Sep 17 00:00:00 2001 From: William Casarin Date: Thu, 10 Oct 2019 16:53:41 -0700 Subject: [PATCH 2/7] cmd: create standalone Init function This will allow us to call this from within a test Signed-off-by: William Casarin --- cmd/commit0.go | 10 ++++++++-- main.go | 6 +----- 2 files changed, 9 insertions(+), 7 deletions(-) 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/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() } From 5f87969d4e42f8d5d02a387a774e73c1e8903163 Mon Sep 17 00:00:00 2001 From: William Casarin Date: Thu, 10 Oct 2019 16:56:28 -0700 Subject: [PATCH 3/7] moveonly: create standlone function for Create Signed-off-by: William Casarin --- cmd/create.go | 54 +++++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/cmd/create.go b/cmd/create.go index dd0fc334c..05a8123ee 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -13,6 +13,34 @@ func init() { rootCmd.AddCommand(createCmd) } +func Create(projectName string) { + 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) +} + var createCmd = &cobra.Command{ Use: "create", Short: "Create new project with provided name.", @@ -23,30 +51,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) }, } From aee97da3fd8c37a6f5611e35bb1e3b99191f49e7 Mon Sep 17 00:00:00 2001 From: William Casarin Date: Thu, 10 Oct 2019 16:57:46 -0700 Subject: [PATCH 4/7] cmd: add outDir to Create This will allow us to generate projects into any directory we would like. We also: - replace spome Sprintfs with path.Join - Use MkdirAll instead of MkDir - Return the project directory Signed-off-by: William Casarin --- cmd/create.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/cmd/create.go b/cmd/create.go index 05a8123ee..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,19 +13,19 @@ func init() { rootCmd.AddCommand(createCmd) } -func Create(projectName string) { - rootDir := fmt.Sprintf("./%v", projectName) - +func Create(projectName string, outDir string) string { + rootDir := path.Join(outDir, projectName) log.Printf("Creating project %s.", projectName) + err := os.MkdirAll(rootDir, os.ModePerm) - 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) + commit0ConfigPath := path.Join(rootDir, "commit0.yml") + log.Printf("%s", commit0ConfigPath) f, err := os.Create(commit0ConfigPath) if err != nil { @@ -33,12 +33,14 @@ func Create(projectName string) { } Templator.Commit0.Execute(f, projectName) - gitIgnorePath := fmt.Sprintf("%v/.gitignore", rootDir) + 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{ @@ -51,6 +53,6 @@ var createCmd = &cobra.Command{ projectName := args[0] - Create(projectName) + Create(projectName, "./") }, } From 4dc42197a413ae0cd3f204eca9fe94fc2a4032bd Mon Sep 17 00:00:00 2001 From: William Casarin Date: Thu, 10 Oct 2019 15:58:18 -0700 Subject: [PATCH 5/7] test: initial Create test Signed-off-by: William Casarin --- test/create_test.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 test/create_test.go 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") + } +} From bfed1604170f4fc53d91dc3aacabdca9a337b4f4 Mon Sep 17 00:00:00 2001 From: William Casarin Date: Thu, 10 Oct 2019 17:01:04 -0700 Subject: [PATCH 6/7] make: add check phase to Makefile Signed-off-by: William Casarin --- Makefile | 3 +++ 1 file changed, 3 insertions(+) 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 ./... From 437df202b1914739f0ce5177007521674e7a04ea Mon Sep 17 00:00:00 2001 From: William Casarin Date: Thu, 10 Oct 2019 17:09:08 -0700 Subject: [PATCH 7/7] add .travis.yml Signed-off-by: William Casarin --- .travis.yml | 9 +++++++++ README.md | 2 ++ 2 files changed, 11 insertions(+) create mode 100644 .travis.yml 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/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.