From f40c775eb06a1fba87da99e0dbe2258836c7cfc4 Mon Sep 17 00:00:00 2001 From: Victor Schumacher Date: Mon, 18 May 2020 18:47:09 -0300 Subject: [PATCH 01/26] create mainfile and makefile --- pkg/formula/creator.go | 107 +++++++++++-------------------- pkg/formula/tpl/tpl_go/tpl_go.go | 6 +- 2 files changed, 41 insertions(+), 72 deletions(-) diff --git a/pkg/formula/creator.go b/pkg/formula/creator.go index 6028eca8e..76f7ec3a5 100644 --- a/pkg/formula/creator.go +++ b/pkg/formula/creator.go @@ -208,7 +208,7 @@ func createSrcFiles(dir, pkg, lang string) error { } switch lang { case "Go": - err = createMainFile(srcDir, pkg, lang) + err = createMainFile(srcDir, pkg, tpl_go.TemplateMain, "go", "main", false) if err != nil { return err } @@ -216,7 +216,7 @@ func createSrcFiles(dir, pkg, lang string) error { if err != nil { return err } - err = createMakefileForm(srcDir, pkg, dir, lang) + err = createMakefileForm(srcDir, pkg, dir, tpl_go.TemplateMakefile, true) if err != nil { return err } @@ -234,11 +234,11 @@ func createSrcFiles(dir, pkg, lang string) error { return err } case "Java": - err = createMainFile(srcDir, pkg, lang) + err = createMainFile(srcDir, pkg, tpl_java.TemplateMain, "java", "Main", true) if err != nil { return err } - err = createMakefileForm(srcDir, pkg, dir, lang) + err = createMakefileForm(srcDir, pkg, dir, tpl_java.TemplateMakefile, false) if err != nil { return err } @@ -260,11 +260,15 @@ func createSrcFiles(dir, pkg, lang string) error { return err } case "Node": - err = createMainFile(srcDir, pkg, lang) + err = createMainFile(srcDir, pkg, tpl_node.TemplateIndex, "js", "index", false) if err != nil { return err } - err = createMakefileForm(srcDir, pkg, dir, lang) + err = createMakefileForm(srcDir, pkg, dir, tpl_node.TemplateMakefile, false) + if err != nil { + return err + } + err = createPackageJson(dir, tpl_node.TemplatePackageJson) if err != nil { return err } @@ -286,11 +290,11 @@ func createSrcFiles(dir, pkg, lang string) error { return err } case "Python": - err = createMainFile(srcDir, pkg, lang) + err = createMainFile(srcDir, pkg, tpl_python.TemplateMain, "py", "main", false) if err != nil { return err } - err = createMakefileForm(srcDir, pkg, dir, lang) + err = createMakefileForm(srcDir, pkg, dir, tpl_python.TemplateMakefile, false) if err != nil { return err } @@ -308,11 +312,11 @@ func createSrcFiles(dir, pkg, lang string) error { return err } default: - err = createMainFile(srcDir, pkg, lang) + err = createMainFile(srcDir, pkg, tpl_shell.TemplateMain, "sh", "main", false) if err != nil { return err } - err = createMakefileForm(srcDir, pkg, dir, lang) + err = createMakefileForm(srcDir, pkg, dir, tpl_shell.TemplateMakefile, false) if err != nil { return err } @@ -330,7 +334,6 @@ func createSrcFiles(dir, pkg, lang string) error { return err } } - return nil } @@ -377,43 +380,17 @@ func createRunTemplate(dir, lang string) error { } return nil } - -func createMakefileForm(dir string, name, pathName, lang string) error { - switch lang { - case "Go": - tplFile := tpl_go.TemplateMakefile - tplFile = strings.ReplaceAll(tplFile, "{{name}}", name) - tplFile = strings.ReplaceAll(tplFile, "{{form-path}}", pathName) - return fileutil.WriteFile(fmt.Sprintf("%s/Makefile", dir), []byte(tplFile)) - case "Java": - tfj := tpl_java.TemplateMakefile - tfj = strings.ReplaceAll(tfj, nameBin, name) - fu := strings.Title(strings.ToLower(name)) - tfj = strings.ReplaceAll(tfj, nameBinFirstUpper, fu) - return fileutil.WriteFile(fmt.Sprintf("%s/Makefile", dir), []byte(tfj)) - case "Node": - tfn := tpl_node.TemplateMakefile - tfn = strings.ReplaceAll(tfn, nameBin, name) - err := fileutil.WriteFile(fmt.Sprintf("%s/Makefile", dir), []byte(tfn)) - if err != nil { - return err - } - tfpj := tpl_node.TemplatePackageJson - return fileutil.WriteFile(fmt.Sprintf("%s/package.json", dir), []byte(tfpj)) - case "Python": - tfp := tpl_python.TemplateMakefile - tfp = strings.ReplaceAll(tfp, nameBin, name) - fu := strings.Title(strings.ToLower(name)) - tfp = strings.ReplaceAll(tfp, nameBinFirstUpper, fu) - return fileutil.WriteFile(fmt.Sprintf("%s/Makefile", dir), []byte(tfp)) - default: - tfs := tpl_shell.TemplateMakefile - tfs = strings.ReplaceAll(tfs, nameBin, name) - return fileutil.WriteFile(fmt.Sprintf("%s/Makefile", dir), []byte(tfs)) +func createMakefileForm(dir, name, pathName, tpl string, compiled bool) error { + if compiled { + tpl = strings.ReplaceAll(tpl, "{{name}}", name) + tpl = strings.ReplaceAll(tpl, "{{form-path}}", pathName) + return fileutil.WriteFile(fmt.Sprintf("%s/Makefile", dir), []byte(tpl)) } + tpl = strings.ReplaceAll(tpl, nameBin, name) + return fileutil.WriteFile(fmt.Sprintf("%s/Makefile", dir), []byte(tpl)) } -func createDockerfile(dir string, tpl string) error { +func createDockerfile(dir, tpl string) error { return fileutil.WriteFile(fmt.Sprintf("%s/Dockerfile", dir), []byte(tpl)) } @@ -423,31 +400,15 @@ func createGoModFile(dir, pkg string) error { return fileutil.WriteFile(fmt.Sprintf("%s/go.mod", dir), []byte(tplFile)) } -func createMainFile(dir, pkg, lang string) error { - switch lang { - case "Go": - tfgo := tpl_go.TemplateMain - tfgo = strings.ReplaceAll(tfgo, nameModule, pkg) - return fileutil.WriteFile(fmt.Sprintf("%s/main.go", dir), []byte(tfgo)) - case "Java": - tfj := tpl_java.TemplateMain - tfj = strings.ReplaceAll(tfj, nameBin, pkg) - fu := strings.Title(strings.ToLower(pkg)) - tfj = strings.ReplaceAll(tfj, nameBinFirstUpper, fu) - return fileutil.WriteFile(fmt.Sprintf("%s/Main.java", dir), []byte(tfj)) - case "Node": - tfn := tpl_node.TemplateIndex - tfn = strings.ReplaceAll(tfn, nameBin, pkg) - return fileutil.WriteFile(fmt.Sprintf("%s/index.js", dir), []byte(tfn)) - case "Python": - tfp := tpl_python.TemplateMain - tfp = strings.ReplaceAll(tfp, nameBin, pkg) - return fileutil.WriteFile(fmt.Sprintf("%s/main.py", dir), []byte(tfp)) - default: - tfs := tpl_shell.TemplateMain - tfs = strings.ReplaceAll(tfs, nameBin, pkg) - return fileutil.WriteFile(fmt.Sprintf("%s/main.sh", dir), []byte(tfs)) +func createMainFile(dir, pkg, tpl, fileFormat, startFile string, uc bool, ) error { + if uc { + tpl = strings.ReplaceAll(tpl, nameBin, pkg) + tpl = strings.ReplaceAll(tpl, nameBinFirstUpper, strings.Title(strings.ToLower(pkg))) + return fileutil.WriteFile(fmt.Sprintf("%s/%s.%s", dir, startFile, fileFormat), []byte(tpl)) } + + tpl = strings.ReplaceAll(tpl, nameBin, pkg) + return fileutil.WriteFile(fmt.Sprintf("%s/%s.%s", dir, startFile, fileFormat), []byte(tpl)) } func createConfigFile(dir string) error { @@ -557,3 +518,11 @@ func generateParent(fc []string, index int) string { return "root" } } + +func createPackageJson(dir, tpl string) error { + err := fileutil.WriteFile(fmt.Sprintf("%s/package.json", dir), []byte(tpl)) + if err != nil { + return err + } + return nil +} diff --git a/pkg/formula/tpl/tpl_go/tpl_go.go b/pkg/formula/tpl/tpl_go/tpl_go.go index 4a00fa9cd..4791f502b 100644 --- a/pkg/formula/tpl/tpl_go/tpl_go.go +++ b/pkg/formula/tpl/tpl_go/tpl_go.go @@ -130,11 +130,11 @@ build: mkdir -p $(DIST_MAC_DIR) $(DIST_LINUX_DIR) $(DIST_WIN_DIR) export MODULE=$(GO111MODULE=on go list -m) #LINUX - CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) -tags release -ldflags '-X $(MODULE)/cmd.Version=$(VERSION) -X $(MODULE)/cmd.BuildDate=$(DATE)' -o '$(DIST_LINUX_DIR)/$(BIN_LINUX)' -v $(CMD_PATH) + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) -tags release -ldflags '-X $(MODULE)/cmd.Version=$(VERSION) -X $(MODULE)/cmd.BuildDate=$(DATE)' -o '$(DIST_LINUX_DIR)/$(BIN_LINUX)' -v $(CMD_PATH) && cp -r . $(DIST_LINUX_DIR) #MAC - GOOS=darwin GOARCH=amd64 $(GOBUILD) -tags release -ldflags '-X $(MODULE)/cmd.Version=$(VERSION) -X $(MODULE)/cmd.BuildDate=$(DATE)' -o '$(DIST_MAC_DIR)/$(BIN_MAC)' -v $(CMD_PATH) + GOOS=darwin GOARCH=amd64 $(GOBUILD) -tags release -ldflags '-X $(MODULE)/cmd.Version=$(VERSION) -X $(MODULE)/cmd.BuildDate=$(DATE)' -o '$(DIST_MAC_DIR)/$(BIN_MAC)' -v $(CMD_PATH) && cp Dockerfile $(DIST_MAC_DIR) && cp -r . $(DIST_LINUX_DIR) #WINDOWS 64 - GOOS=windows GOARCH=amd64 $(GOBUILD) -tags release -ldflags '-X $(MODULE)/cmd.Version=$(VERSION) -X $(MODULE)/cmd.BuildDate=$(DATE)' -o '$(DIST_WIN_DIR)/$(BIN_WIN)' -v $(CMD_PATH) + GOOS=windows GOARCH=amd64 $(GOBUILD) -tags release -ldflags '-X $(MODULE)/cmd.Version=$(VERSION) -X $(MODULE)/cmd.BuildDate=$(DATE)' -o '$(DIST_WIN_DIR)/$(BIN_WIN)' -v $(CMD_PATH) && cp Dockerfile $(DIST_WIN_DIR) && cp -r . $(DIST_LINUX_DIR) test: $(GOTEST) -short ` + "`go list ./... | grep -v vendor/`" From e0b14d5c8ec1b4cf4b31ca09001264a8e255dcaf Mon Sep 17 00:00:00 2001 From: Victor Schumacher Date: Tue, 19 May 2020 09:09:38 -0300 Subject: [PATCH 02/26] refactor run template and template names --- pkg/formula/creator.go | 88 +++++++++--------------- pkg/formula/tpl/tpl_go/tpl_go.go | 18 ++--- pkg/formula/tpl/tpl_java/tpl_java.go | 10 +-- pkg/formula/tpl/tpl_node/tpl_node.go | 12 ++-- pkg/formula/tpl/tpl_python/tpl_python.go | 8 +-- pkg/formula/tpl/tpl_shell/tpl_shell.go | 8 +-- 6 files changed, 61 insertions(+), 83 deletions(-) diff --git a/pkg/formula/creator.go b/pkg/formula/creator.go index 76f7ec3a5..2642f3144 100644 --- a/pkg/formula/creator.go +++ b/pkg/formula/creator.go @@ -149,7 +149,7 @@ func verifyCommand(fCmd string, trees map[string]Tree) error { return nil } -func changeMakefileMain(formPath string, fCmd, fName string) error { +func changeMakefileMain(formPath, fCmd, fName string) error { d := strings.Split(fCmd, " ") dir := fmt.Sprintf("%s/%s", formPath, Makefile) tplFile, err := fileutil.ReadFile(dir) @@ -175,7 +175,7 @@ func formulaValue(file []byte) string { } func createMakefileMain(dir, dirForm, name string) error { - tplFile := tpl_go.TemplateMakefileMain + tplFile := tpl_go.Makefilemain tplFile = strings.ReplaceAll(tplFile, "{{formName}}", strings.ToUpper(name)) tplFile = strings.ReplaceAll(string(tplFile), "{{formPath}}", dirForm) @@ -188,14 +188,14 @@ func createMakefileMain(dir, dirForm, name string) error { } func createScripts(dir string) error { - tplFile := tpl_go.TemplateCopyBinConfig + tplFile := tpl_go.CopyBinConfig err := fileutil.WriteFilePerm(fmt.Sprintf("%s/copy-bin-configs.sh", dir), []byte(tplFile), 0755) if err != nil { return err } - tplFile = tpl_go.TemplateUnzipBinConfigs + tplFile = tpl_go.UnzipBinConfigs return fileutil.WriteFilePerm(fmt.Sprintf("%s/unzip-bin-configs.sh", dir), []byte(tplFile), 0755) } @@ -208,7 +208,7 @@ func createSrcFiles(dir, pkg, lang string) error { } switch lang { case "Go": - err = createMainFile(srcDir, pkg, tpl_go.TemplateMain, "go", "main", false) + err = createMainFile(srcDir, pkg, tpl_go.Main, "go", "main", false) if err != nil { return err } @@ -216,11 +216,11 @@ func createSrcFiles(dir, pkg, lang string) error { if err != nil { return err } - err = createMakefileForm(srcDir, pkg, dir, tpl_go.TemplateMakefile, true) + err = createMakefileForm(srcDir, pkg, dir, tpl_go.Makefile, true) if err != nil { return err } - err = createDockerfile(srcDir, tpl_go.TemplateDockerfile) + err = createDockerfile(srcDir, tpl_go.Dockerfile) if err != nil { return err } @@ -234,19 +234,19 @@ func createSrcFiles(dir, pkg, lang string) error { return err } case "Java": - err = createMainFile(srcDir, pkg, tpl_java.TemplateMain, "java", "Main", true) + err = createMainFile(srcDir, pkg, tpl_java.Main, "java", "Main", true) if err != nil { return err } - err = createMakefileForm(srcDir, pkg, dir, tpl_java.TemplateMakefile, false) + err = createMakefileForm(srcDir, pkg, dir, tpl_java.Makefile, false) if err != nil { return err } - err = createDockerfile(srcDir, tpl_java.TemplateDockerfile) + err = createDockerfile(srcDir, tpl_java.Dockerfile) if err != nil { return err } - err = createRunTemplate(srcDir, lang) + err = createRunTemplate(srcDir, tpl_java.RunTemplate) if err != nil { return err } @@ -260,23 +260,23 @@ func createSrcFiles(dir, pkg, lang string) error { return err } case "Node": - err = createMainFile(srcDir, pkg, tpl_node.TemplateIndex, "js", "index", false) + err = createMainFile(srcDir, pkg, tpl_node.Index, "js", "index", false) if err != nil { return err } - err = createMakefileForm(srcDir, pkg, dir, tpl_node.TemplateMakefile, false) + err = createMakefileForm(srcDir, pkg, dir, tpl_node.Makefile, false) if err != nil { return err } - err = createPackageJson(dir, tpl_node.TemplatePackageJson) + err = createPackageJson(srcDir, tpl_node.PackageJson) if err != nil { return err } - err = createDockerfile(srcDir, tpl_node.TemplateDockerfile) + err = createDockerfile(srcDir, tpl_node.Dockerfile) if err != nil { return err } - err = createRunTemplate(srcDir, lang) + err = createRunTemplate(srcDir, tpl_node.RunTemplate) if err != nil { return err } @@ -290,15 +290,15 @@ func createSrcFiles(dir, pkg, lang string) error { return err } case "Python": - err = createMainFile(srcDir, pkg, tpl_python.TemplateMain, "py", "main", false) + err = createMainFile(srcDir, pkg, tpl_python.Main, "py", "main", false) if err != nil { return err } - err = createMakefileForm(srcDir, pkg, dir, tpl_python.TemplateMakefile, false) + err = createMakefileForm(srcDir, pkg, dir, tpl_python.Makefile, false) if err != nil { return err } - err = createDockerfile(srcDir, tpl_python.TemplateDockerfile) + err = createDockerfile(srcDir, tpl_python.Dockerfile) if err != nil { return err } @@ -312,15 +312,15 @@ func createSrcFiles(dir, pkg, lang string) error { return err } default: - err = createMainFile(srcDir, pkg, tpl_shell.TemplateMain, "sh", "main", false) + err = createMainFile(srcDir, pkg, tpl_shell.Main, "sh", "main", false) if err != nil { return err } - err = createMakefileForm(srcDir, pkg, dir, tpl_shell.TemplateMakefile, false) + err = createMakefileForm(srcDir, pkg, dir, tpl_shell.Makefile, false) if err != nil { return err } - err = createDockerfile(srcDir, tpl_shell.TemplateDockerfile) + err = createDockerfile(srcDir, tpl_shell.Dockerfile) if err != nil { return err } @@ -340,46 +340,28 @@ func createSrcFiles(dir, pkg, lang string) error { func createPkgFile(dir, pkg, lang string) error { switch lang { case "Go": - tfgo := tpl_go.TemplatePkg - tfgo = strings.ReplaceAll(tfgo, nameModule, pkg) - + tfgo := strings.ReplaceAll(tpl_go.Pkg, nameModule, pkg) return fileutil.WriteFile(fmt.Sprintf("%s/%s.go", dir, pkg), []byte(tfgo)) case "Java": - tfj := tpl_java.TemplateFileJava - tfj = strings.ReplaceAll(tfj, nameBin, pkg) + tfj := strings.ReplaceAll(tpl_java.File, nameBin, pkg) fu := strings.Title(strings.ToLower(pkg)) tfj = strings.ReplaceAll(tfj, nameBinFirstUpper, fu) return fileutil.WriteFile(fmt.Sprintf("%s/%s.java", dir, fu), []byte(tfj)) case "Node": - tfn := tpl_node.TemplateFileNode + tfn := tpl_node.File tfn = strings.ReplaceAll(tfn, nameBin, pkg) return fileutil.WriteFile(fmt.Sprintf("%s/%s.js", dir, pkg), []byte(tfn)) case "Python": - tfp := tpl_python.TemplateFilePython - tfp = strings.ReplaceAll(tfp, nameBinFirstUpper, pkg) - return fileutil.WriteFile(fmt.Sprintf("%s/%s.py", dir, pkg), []byte(tfp)) + return fileutil.WriteFile(fmt.Sprintf("%s/%s.py", dir, pkg), []byte(tpl_python.File)) default: - tfs := tpl_shell.TemplateFileShell - return fileutil.WriteFile(fmt.Sprintf("%s/%s.sh", dir, pkg), []byte(tfs)) + return fileutil.WriteFile(fmt.Sprintf("%s/%s.sh", dir, pkg), []byte(tpl_shell.File)) } } -func createRunTemplate(dir, lang string) error { - switch lang { - case "Go": - return nil - case "Java": - tj := tpl_java.TemplateRunTemplate - return fileutil.WriteFilePerm(fmt.Sprintf("%s/run_template", dir), []byte(tj), 0777) - case "Node": - tn := tpl_node.TemplateRunTemplate - return fileutil.WriteFilePerm(fmt.Sprintf("%s/run_template", dir), []byte(tn), 0777) - case "Python": - default: - - } - return nil +func createRunTemplate(dir, tpl string) error { + return fileutil.WriteFilePerm(fmt.Sprintf("%s/run_template", dir), []byte(tpl), 0777) } + func createMakefileForm(dir, name, pathName, tpl string, compiled bool) error { if compiled { tpl = strings.ReplaceAll(tpl, "{{name}}", name) @@ -395,7 +377,7 @@ func createDockerfile(dir, tpl string) error { } func createGoModFile(dir, pkg string) error { - tplFile := tpl_go.TemplateGoMod + tplFile := tpl_go.GoMod tplFile = strings.ReplaceAll(tplFile, nameModule, pkg) return fileutil.WriteFile(fmt.Sprintf("%s/go.mod", dir), []byte(tplFile)) } @@ -412,7 +394,7 @@ func createMainFile(dir, pkg, tpl, fileFormat, startFile string, uc bool, ) erro } func createConfigFile(dir string) error { - tplFile := tpl_go.TemplateConfig + tplFile := tpl_go.Config return fileutil.WriteFile(fmt.Sprintf("%s/config.json", dir), []byte(tplFile)) } @@ -520,9 +502,5 @@ func generateParent(fc []string, index int) string { } func createPackageJson(dir, tpl string) error { - err := fileutil.WriteFile(fmt.Sprintf("%s/package.json", dir), []byte(tpl)) - if err != nil { - return err - } - return nil + return fileutil.WriteFile(fmt.Sprintf("%s/package.json", dir), []byte(tpl)) } diff --git a/pkg/formula/tpl/tpl_go/tpl_go.go b/pkg/formula/tpl/tpl_go/tpl_go.go index 4791f502b..1171dbc4a 100644 --- a/pkg/formula/tpl/tpl_go/tpl_go.go +++ b/pkg/formula/tpl/tpl_go/tpl_go.go @@ -1,7 +1,7 @@ package tpl_go const ( - TemplateConfig = `{ + Config = `{ "description": "Sample inputs in Ritchie.", "inputs" : [ { @@ -30,7 +30,7 @@ const ( } ] }` - TemplateCopyBinConfig = `#!/bin/sh + CopyBinConfig = `#!/bin/sh FORMULAS="$1" @@ -87,13 +87,13 @@ init() { init ` - TemplateGoMod = `module {{nameModule}} + GoMod = `module {{nameModule}} go 1.14 require github.com/fatih/color v1.9.0` - TemplateMain = `package main + Main = `package main import ( "os" @@ -112,7 +112,7 @@ func main() { }.Run() }` - TemplateMakefile = `# Go parameters + Makefile = `# Go parameters BINARY_NAME={{name}} GOCMD=go GOBUILD=$(GOCMD) build @@ -139,7 +139,7 @@ build: test: $(GOTEST) -short ` + "`go list ./... | grep -v vendor/`" - TemplateDockerfile = ` + Dockerfile = ` FROM golang:alpine AS builder WORKDIR /app/ COPY . . @@ -150,7 +150,7 @@ WORKDIR /app/ COPY --from=builder app/main . ENTRYPOINT ["./main"]` - TemplateMakefileMain = `#Makefiles + Makefilemain = `#Makefiles {{formName}}={{formPath}} FORMULAS=$({{formName}}) @@ -188,7 +188,7 @@ endif rm -rf $(HOME)/.rit/repo/local/tree.json cp tree/tree.json $(HOME)/.rit/repo/local/tree.json ` - TemplatePkg = `package {{nameModule}} + Pkg = `package {{nameModule}} import ( "fmt" @@ -208,6 +208,6 @@ func(in Input)Run() { color.Yellow(fmt.Sprintf("You receive %s in boolean.", in.Boolean )) }` - TemplateUnzipBinConfigs = `#!/bin/sh + UnzipBinConfigs = `#!/bin/sh find formulas -name "*.zip" | while read filename; do unzip -o -d "` + "`dirname \"$filename\"`\" \"$filename\"; rm -f \"$filename\"; done;" ) diff --git a/pkg/formula/tpl/tpl_java/tpl_java.go b/pkg/formula/tpl/tpl_java/tpl_java.go index d776b58c6..94fcb7315 100644 --- a/pkg/formula/tpl/tpl_java/tpl_java.go +++ b/pkg/formula/tpl/tpl_java/tpl_java.go @@ -1,7 +1,7 @@ package tpl_java const ( - TemplateMain = `import {{bin-name}}.{{bin-name-first-upper}}; + Main = `import {{bin-name}}.{{bin-name-first-upper}}; public class Main { @@ -14,7 +14,7 @@ public class Main { } }` - TemplateMakefile = `# Java Parameters + Makefile = `# Java Parameters BINARY_NAME_UNIX={{bin-name}}.sh BINARY_NAME_WINDOWS={{bin-name}}.bat DIST=../dist @@ -31,7 +31,7 @@ build: #Clean files rm Main.jar manifest.txt *.class {{bin-name}}/*.class $(BINARY_NAME_WINDOWS) $(BINARY_NAME_UNIX)` - TemplateDockerfile = ` + Dockerfile = ` FROM alpine:latest USER root @@ -48,10 +48,10 @@ ENV PATH="$JAVA_HOME/bin:${PATH}" ENTRYPOINT java -jar Main.jar` - TemplateRunTemplate = `#!/bin/sh + RunTemplate = `#!/bin/sh java -jar Main.jar` - TemplateFileJava = `package {{bin-name}}; + File = `package {{bin-name}}; public class {{bin-name-first-upper}} { diff --git a/pkg/formula/tpl/tpl_node/tpl_node.go b/pkg/formula/tpl/tpl_node/tpl_node.go index 2936afe92..8be2b9993 100644 --- a/pkg/formula/tpl/tpl_node/tpl_node.go +++ b/pkg/formula/tpl/tpl_node/tpl_node.go @@ -1,7 +1,7 @@ package tpl_node const ( - TemplateIndex = `const run = require("./{{bin-name}}/{{bin-name}}") + Index = `const run = require("./{{bin-name}}/{{bin-name}}") const INPUT1 = process.env.SAMPLE_TEXT const INPUT2 = process.env.SAMPLE_LIST @@ -9,7 +9,7 @@ const INPUT3 = process.env.SAMPLE_BOOL run(INPUT1, INPUT2, INPUT3)` - TemplateMakefile = `# Make Run Node + Makefile = `# Make Run Node BINARY_NAME_UNIX={{bin-name}}.sh BINARY_NAME_WINDOWS={{bin-name}}.bat DIST=../dist @@ -24,7 +24,7 @@ build: #Clean files rm $(BINARY_NAME_UNIX)` - TemplateDockerfile = ` + Dockerfile = ` FROM node:10 WORKDIR /app @@ -33,10 +33,10 @@ COPY . . ENTRYPOINT node index.js` - TemplateRunTemplate = `#!/bin/sh + RunTemplate = `#!/bin/sh node index.js` - TemplatePackageJson = `{ + PackageJson = `{ "name": "src", "version": "1.0.0", "description": "Sample formula in node", @@ -48,7 +48,7 @@ node index.js` "license": "ISC" }` - TemplateFileNode = `function Run(input1, input2, input3) { + File = `function Run(input1, input2, input3) { console.log("Hello World!") console.log("You receive "+ input1 +" in text."); console.log("You receive "+ input2 +" in list."); diff --git a/pkg/formula/tpl/tpl_python/tpl_python.go b/pkg/formula/tpl/tpl_python/tpl_python.go index c74bec77f..51f25c8ec 100644 --- a/pkg/formula/tpl/tpl_python/tpl_python.go +++ b/pkg/formula/tpl/tpl_python/tpl_python.go @@ -1,7 +1,7 @@ package tpl_python const ( - TemplateMain = `#!/usr/bin/python3 + Main = `#!/usr/bin/python3 import os from {{bin-name}} import {{bin-name}} @@ -12,7 +12,7 @@ input3 = os.environ.get('SAMPLE_BOOL') {{bin-name}}.Run(input1, input2, input3) ` - TemplateMakefile = `# Make Run Python + Makefile = `# Make Run Python BINARY_NAME={{bin-name}}.py BINARY_NAME_WINDOWS={{bin-name}}.bat DIST=../dist @@ -23,7 +23,7 @@ build: chmod +x $(DIST_DIR)/main.py echo 'python main.py' >> $(DIST_DIR)/$(BINARY_NAME_WINDOWS)` - TemplateDockerfile = ` + Dockerfile = ` FROM python:3 WORKDIR /app @@ -32,7 +32,7 @@ COPY . . ENTRYPOINT python3 main.py` - TemplateFilePython = `#!/usr/bin/python3 + File = `#!/usr/bin/python3 import time def Run(input1, input2, input3): diff --git a/pkg/formula/tpl/tpl_shell/tpl_shell.go b/pkg/formula/tpl/tpl_shell/tpl_shell.go index 7584ea157..6349a0a5f 100644 --- a/pkg/formula/tpl/tpl_shell/tpl_shell.go +++ b/pkg/formula/tpl/tpl_shell/tpl_shell.go @@ -1,13 +1,13 @@ package tpl_shell const ( - TemplateMain = `#!/bin/sh + Main = `#!/bin/sh . ./{{bin-name}}/{{bin-name}}.sh --source-only run $SAMPLE_TEXT $SAMPLE_LIST $SAMPLE_BOOL` - TemplateMakefile = `# SH + Makefile = `# SH BINARY_NAME={{bin-name}}.sh DIST=../dist DIST_DIR=$(DIST)/commons/bin @@ -16,7 +16,7 @@ build: cp main.sh $(DIST_DIR)/$(BINARY_NAME) && cp -r {{bin-name}} $(DIST_DIR) && cp Dockerfile $(DIST_DIR) chmod +x $(DIST_DIR)/$(BINARY_NAME)` - TemplateDockerfile = ` + Dockerfile = ` FROM alpine:3.7 WORKDIR /app @@ -27,7 +27,7 @@ RUN chmod +x main.sh ENTRYPOINT /app/main.sh` - TemplateFileShell = `#!/bin/sh + File = `#!/bin/sh run() { echo "Hello World! " echo "You receive $SAMPLE_TEXT in text. " From 0da78660321921d9954ee34cb1ddfe4f04a70de6 Mon Sep 17 00:00:00 2001 From: Victor Schumacher Date: Tue, 19 May 2020 16:59:58 -0300 Subject: [PATCH 03/26] extrator on pkg dir --- pkg/formula/creator.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/pkg/formula/creator.go b/pkg/formula/creator.go index 2642f3144..31ed20ce3 100644 --- a/pkg/formula/creator.go +++ b/pkg/formula/creator.go @@ -156,6 +156,7 @@ func changeMakefileMain(formPath, fCmd, fName string) error { if err != nil { return err } + fmt.Println(d) variable := strings.ToUpper(d[len(d)-1]) + "=" + strings.Join(d[1:], "/") tplFile = []byte(strings.ReplaceAll(string(tplFile), "\nFORMULAS=", "\n"+variable+"\nFORMULAS=")) formulas := formulaValue(tplFile) @@ -202,6 +203,7 @@ func createScripts(dir string) error { func createSrcFiles(dir, pkg, lang string) error { srcDir := fmt.Sprintf("%s/src", dir) + pkgDir := fmt.Sprintf("%s/%s", srcDir, pkg) err := fileutil.CreateDirIfNotExists(srcDir, os.ModePerm) if err != nil { return err @@ -250,8 +252,7 @@ func createSrcFiles(dir, pkg, lang string) error { if err != nil { return err } - pkgDir := fmt.Sprintf("%s/%s", srcDir, pkg) - err = fileutil.CreateDirIfNotExists(pkgDir, os.ModePerm) + err = createPkgDir(pkgDir) if err != nil { return err } @@ -280,8 +281,7 @@ func createSrcFiles(dir, pkg, lang string) error { if err != nil { return err } - pkgDir := fmt.Sprintf("%s/%s", srcDir, pkg) - err = fileutil.CreateDirIfNotExists(pkgDir, os.ModePerm) + err = createPkgDir(pkgDir) if err != nil { return err } @@ -302,8 +302,7 @@ func createSrcFiles(dir, pkg, lang string) error { if err != nil { return err } - pkgDir := fmt.Sprintf("%s/%s", srcDir, pkg) - err = fileutil.CreateDirIfNotExists(pkgDir, os.ModePerm) + err = createPkgDir(pkgDir) if err != nil { return err } @@ -324,8 +323,7 @@ func createSrcFiles(dir, pkg, lang string) error { if err != nil { return err } - pkgDir := fmt.Sprintf("%s/%s", srcDir, pkg) - err = fileutil.CreateDirIfNotExists(pkgDir, os.ModePerm) + err = createPkgDir(pkgDir) if err != nil { return err } @@ -337,6 +335,10 @@ func createSrcFiles(dir, pkg, lang string) error { return nil } +func createPkgDir(pkgDir string) error { + return fileutil.CreateDirIfNotExists(pkgDir, os.ModePerm) +} + func createPkgFile(dir, pkg, lang string) error { switch lang { case "Go": @@ -359,7 +361,7 @@ func createPkgFile(dir, pkg, lang string) error { } func createRunTemplate(dir, tpl string) error { - return fileutil.WriteFilePerm(fmt.Sprintf("%s/run_template", dir), []byte(tpl), 0777) + return fileutil.WriteFilePerm(fmt.Sprintf("%s/run_template", dir), []byte(tpl), 0777) } func createMakefileForm(dir, name, pathName, tpl string, compiled bool) error { @@ -382,7 +384,7 @@ func createGoModFile(dir, pkg string) error { return fileutil.WriteFile(fmt.Sprintf("%s/go.mod", dir), []byte(tplFile)) } -func createMainFile(dir, pkg, tpl, fileFormat, startFile string, uc bool, ) error { +func createMainFile(dir, pkg, tpl, fileFormat, startFile string, uc bool) error { if uc { tpl = strings.ReplaceAll(tpl, nameBin, pkg) tpl = strings.ReplaceAll(tpl, nameBinFirstUpper, strings.Title(strings.ToLower(pkg))) From ec5b0f8ff32f4f6f5c3b0edc15388ca5a6bcf4a0 Mon Sep 17 00:00:00 2001 From: Victor Schumacher Date: Wed, 20 May 2020 08:41:33 -0300 Subject: [PATCH 04/26] change make to use the two last words --- pkg/formula/creator.go | 19 ++++++++++--------- pkg/formula/tpl/tpl_go/tpl_go.go | 2 +- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/pkg/formula/creator.go b/pkg/formula/creator.go index 31ed20ce3..15de498dc 100644 --- a/pkg/formula/creator.go +++ b/pkg/formula/creator.go @@ -64,6 +64,7 @@ func (c CreateManager) Create(fCmd, lang string) (CreateManager, error) { func generateFormulaFiles(formPath, fCmd, lang string, new bool) error { d := strings.Split(fCmd, " ") dirForm := strings.Join(d[1:], "/") + formulaName := fmt.Sprintf("%s_%s", d[len(d)-2], d[len(d)-1]) var dir string if new { @@ -72,7 +73,8 @@ func generateFormulaFiles(formPath, fCmd, lang string, new bool) error { if err != nil && !os.IsExist(err) { return err } - err = createMakefileMain(formPath, dirForm, d[len(d)-1]) + + err = createMakefileMain(formPath, dirForm, formulaName) if err != nil { return err } @@ -83,7 +85,8 @@ func generateFormulaFiles(formPath, fCmd, lang string, new bool) error { if err != nil && !os.IsExist(err) { return err } - err = changeMakefileMain(formPath, fCmd, d[len(d)-1]) + fmt.Println(d) + err = changeMakefileMain(formPath, fCmd, formulaName) if err != nil { return err } @@ -92,7 +95,7 @@ func generateFormulaFiles(formPath, fCmd, lang string, new bool) error { if err != nil { return err } - err = createSrcFiles(dir, d[len(d)-1], lang) + err = createSrcFiles(dir, formulaName, lang) if err != nil { return err } @@ -133,7 +136,7 @@ func verifyCommand(fCmd string, trees map[string]Tree) error { return errors.New("the formula's command needs to start with \"rit\" [ex.: rit group verb ]") } - if len(s) == 1 || len(s) == 2 { + if len(s) <= 2 { return errors.New("the formula's command needs at least 2 words following \"rit\" [ex.: rit group verb ]") } cp := fmt.Sprintf("root_%s", strings.Join(s[1:len(s)-1], "_")) @@ -156,12 +159,10 @@ func changeMakefileMain(formPath, fCmd, fName string) error { if err != nil { return err } - fmt.Println(d) - variable := strings.ToUpper(d[len(d)-1]) + "=" + strings.Join(d[1:], "/") + variable := strings.ToUpper(fName) + "=" + strings.Join(d[1:], "/") tplFile = []byte(strings.ReplaceAll(string(tplFile), "\nFORMULAS=", "\n"+variable+"\nFORMULAS=")) formulas := formulaValue(tplFile) tplFile = []byte(strings.ReplaceAll(string(tplFile), formulas, formulas+" $("+strings.ToUpper(fName)+")")) - err = fileutil.WriteFile(dir, tplFile) if err != nil { return err @@ -176,10 +177,10 @@ func formulaValue(file []byte) string { } func createMakefileMain(dir, dirForm, name string) error { - tplFile := tpl_go.Makefilemain + tplFile := tpl_go.MakefileMain tplFile = strings.ReplaceAll(tplFile, "{{formName}}", strings.ToUpper(name)) - tplFile = strings.ReplaceAll(string(tplFile), "{{formPath}}", dirForm) + tplFile = strings.ReplaceAll(tplFile, "{{formPath}}", dirForm) err := createScripts(dir) if err != nil { diff --git a/pkg/formula/tpl/tpl_go/tpl_go.go b/pkg/formula/tpl/tpl_go/tpl_go.go index 1171dbc4a..2ec48b42e 100644 --- a/pkg/formula/tpl/tpl_go/tpl_go.go +++ b/pkg/formula/tpl/tpl_go/tpl_go.go @@ -150,7 +150,7 @@ WORKDIR /app/ COPY --from=builder app/main . ENTRYPOINT ["./main"]` - Makefilemain = `#Makefiles + MakefileMain = `#Makefiles {{formName}}={{formPath}} FORMULAS=$({{formName}}) From 1d9c1ee0615b5ce4c3a2f08208e113b8c31c4c8c Mon Sep 17 00:00:00 2001 From: Victor Schumacher Date: Thu, 21 May 2020 09:35:40 -0300 Subject: [PATCH 05/26] enable to save formula in another folder --- cmd/single/main.go | 2 +- cmd/team/main.go | 2 +- pkg/cmd/create_formula.go | 23 ++++++++++++++++++----- pkg/cmd/create_formula_test.go | 2 +- pkg/formula/creator.go | 20 ++++++++++++++++++-- pkg/formula/creator_test.go | 2 +- pkg/formula/formula.go | 2 +- 7 files changed, 41 insertions(+), 12 deletions(-) diff --git a/cmd/single/main.go b/cmd/single/main.go index 0cb7b26f6..36f290908 100644 --- a/cmd/single/main.go +++ b/cmd/single/main.go @@ -110,7 +110,7 @@ func buildCommands() *cobra.Command { updateRepoCmd := cmd.NewUpdateRepoCmd(repoManager) autocompleteZsh := cmd.NewAutocompleteZsh(autocompleteGen) autocompleteBash := cmd.NewAutocompleteBash(autocompleteGen) - createFormulaCmd := cmd.NewCreateFormulaCmd(formulaCreator, inputText, inputList) + createFormulaCmd := cmd.NewCreateFormulaCmd(formulaCreator, inputText, inputList, inputBool) autocompleteCmd.AddCommand(autocompleteZsh, autocompleteBash) addCmd.AddCommand(addRepoCmd) diff --git a/cmd/team/main.go b/cmd/team/main.go index 46f58649c..9f8fcde9d 100644 --- a/cmd/team/main.go +++ b/cmd/team/main.go @@ -137,7 +137,7 @@ func buildCommands() *cobra.Command { updateRepoCmd := cmd.NewUpdateRepoCmd(repoManager) autocompleteZsh := cmd.NewAutocompleteZsh(autocompleteGen) autocompleteBash := cmd.NewAutocompleteBash(autocompleteGen) - createFormulaCmd := cmd.NewCreateFormulaCmd(formulaCreator, inputText, inputList) + createFormulaCmd := cmd.NewCreateFormulaCmd(formulaCreator, inputText, inputList, inputBool) autocompleteCmd.AddCommand(autocompleteZsh, autocompleteBash) addCmd.AddCommand(addRepoCmd) diff --git a/pkg/cmd/create_formula.go b/pkg/cmd/create_formula.go index 0eff10cf1..86a54b6b8 100644 --- a/pkg/cmd/create_formula.go +++ b/pkg/cmd/create_formula.go @@ -15,11 +15,12 @@ type createFormulaCmd struct { formula.Creator prompt.InputText prompt.InputList + prompt.InputBool } // CreateFormulaCmd creates a new cmd instance -func NewCreateFormulaCmd(cf formula.Creator, it prompt.InputText, il prompt.InputList) *cobra.Command { - c := createFormulaCmd{cf, it, il} +func NewCreateFormulaCmd(cf formula.Creator, it prompt.InputText, il prompt.InputList, ib prompt.InputBool) *cobra.Command { + c := createFormulaCmd{cf, it, il, ib} return &cobra.Command{ Use: "formula", Short: "Create a new formula", @@ -30,8 +31,9 @@ func NewCreateFormulaCmd(cf formula.Creator, it prompt.InputText, il prompt.Inpu func (c createFormulaCmd) runFunc() CommandRunnerFunc { return func(cmd *cobra.Command, args []string) error { - fmt.Println("Creating Formula ...") - fCmd, err := c.Text("New formula's command ? [ex.: rit group verb ]", true) + var localRepoDir = "" + + fCmd, err := c.Text("Enter the new formula command [ex.: rit group verb noun]", true) if err != nil { return err } @@ -39,10 +41,21 @@ func (c createFormulaCmd) runFunc() CommandRunnerFunc { if err != nil { return err } - f, err := c.Create(fCmd, lang) + choice, err := c.Bool("Use default repo?", []string{"yes", "no"}) + + if !choice { + localRepoDir, err = c.Text("Enter your path [ex.:/home/user/my-ritchie-formulas ]", true) + fmt.Println("Make sure you have Makefile and tree.json") + if err != nil { + return err + } + + } + f, err := c.Create(fCmd, lang,localRepoDir) if err != nil { return err } + log.Printf("Formula in %s successfully created!\n", lang) log.Printf("Your formula is in %s", f.FormPath) return nil diff --git a/pkg/cmd/create_formula_test.go b/pkg/cmd/create_formula_test.go index 559181c75..8e03943b1 100644 --- a/pkg/cmd/create_formula_test.go +++ b/pkg/cmd/create_formula_test.go @@ -5,7 +5,7 @@ import ( ) func TestNewCreateFormulaCmd(t *testing.T) { - cmd := NewCreateFormulaCmd(formCreator{}, inputTextMock{}, inputListMock{}) + cmd := NewCreateFormulaCmd(formCreator{}, inputTextMock{}, inputListMock{}, inputTrueMock{}) if cmd == nil { t.Errorf("NewCreateFormulaCmd got %v", cmd) diff --git a/pkg/formula/creator.go b/pkg/formula/creator.go index 15de498dc..31e085175 100644 --- a/pkg/formula/creator.go +++ b/pkg/formula/creator.go @@ -29,8 +29,16 @@ func NewCreator(homePath string, tm TreeManager) CreateManager { return CreateManager{FormPath: fmt.Sprintf(FormCreatePathPattern, homePath), treeManager: tm} } -func (c CreateManager) Create(fCmd, lang string) (CreateManager, error) { +func (c CreateManager) Create(fCmd, lang, localRepoDir string) (CreateManager, error) { _ = fileutil.CreateDirIfNotExists(c.FormPath, os.ModePerm) + + if localRepoDir != "" { + if !filesVerify(localRepoDir){ + return CreateManager{}, errors.New("Makefile or tree.json not found") + } + c.FormPath = localRepoDir + } + trees, err := c.treeManager.Tree() if err != nil { return CreateManager{}, err @@ -46,7 +54,7 @@ func (c CreateManager) Create(fCmd, lang string) (CreateManager, error) { return CreateManager{}, err } - if fileutil.Exists(fmt.Sprintf(TreeCreatePathPattern, c.FormPath)) && (fileutil.Exists(fmt.Sprintf("%s/%s", c.FormPath, Makefile))) { + if filesVerify(c.FormPath) { err = generateFormulaFiles(c.FormPath, fCmd, lang, false) if err != nil { return CreateManager{}, err @@ -61,6 +69,14 @@ func (c CreateManager) Create(fCmd, lang string) (CreateManager, error) { return c, nil } +func filesVerify(formPath string) bool { + if fileutil.Exists(fmt.Sprintf(TreeCreatePathPattern, formPath)) && + (fileutil.Exists(fmt.Sprintf("%s/%s", formPath, Makefile))) { + return true + } + return false +} + func generateFormulaFiles(formPath, fCmd, lang string, new bool) error { d := strings.Split(fCmd, " ") dirForm := strings.Join(d[1:], "/") diff --git a/pkg/formula/creator_test.go b/pkg/formula/creator_test.go index 56e921993..98f56ddbd 100644 --- a/pkg/formula/creator_test.go +++ b/pkg/formula/creator_test.go @@ -132,7 +132,7 @@ func TestCreator(t *testing.T) { in := tt.in out := tt.out - _, got := creator.Create(in.fCmd, in.lang) + _, got := creator.Create(in.fCmd, in.lang, "") if got != nil && got.Error() != out.err.Error() { t.Errorf("Create(%s) got %v, want %v", tt.name, got, out.err) } diff --git a/pkg/formula/formula.go b/pkg/formula/formula.go index 7abf7dd2a..774c8425c 100644 --- a/pkg/formula/formula.go +++ b/pkg/formula/formula.go @@ -160,5 +160,5 @@ type Runner interface { } type Creator interface { - Create(formulaCmd, lang string) (CreateManager, error) + Create(formulaCmd, lang, customRepoDir string) (CreateManager, error) } From 193d129349db6035500b2a6f63d4f91ee09695f1 Mon Sep 17 00:00:00 2001 From: Victor Schumacher Date: Thu, 21 May 2020 12:08:02 -0300 Subject: [PATCH 06/26] extractor files checker --- pkg/cmd/create_formula.go | 18 ++++++++++-------- pkg/cmd/mocks_test.go | 2 +- pkg/formula/creator.go | 27 +++++++++++++++++++++------ pkg/formula/formula.go | 1 + 4 files changed, 33 insertions(+), 15 deletions(-) diff --git a/pkg/cmd/create_formula.go b/pkg/cmd/create_formula.go index 05ba9096a..6f432ee85 100644 --- a/pkg/cmd/create_formula.go +++ b/pkg/cmd/create_formula.go @@ -22,8 +22,9 @@ type createFormulaCmd struct { // createFormula type for stdin json decoder type createFormula struct { - FormulaCmd string `json:"formulaCmd"` - Lang string `json:"lang"` + FormulaCmd string `json:"formulaCmd"` + Lang string `json:"lang"` + LocalRepoDir string `json:"localRepoDir"` } // CreateFormulaCmd creates a new cmd instance @@ -41,7 +42,7 @@ func NewCreateFormulaCmd(cf formula.Creator, it prompt.InputText, il prompt.Inpu Use: "formula", Short: "Create a new formula", Example: "rit create formula", - RunE: RunFuncE(c.runStdin(), c.runPrompt()), + RunE: RunFuncE(c.runStdin(), c.runPrompt()), } cmd.LocalFlags() @@ -52,7 +53,7 @@ func NewCreateFormulaCmd(cf formula.Creator, it prompt.InputText, il prompt.Inpu func (c createFormulaCmd) runPrompt() CommandRunnerFunc { return func(cmd *cobra.Command, args []string) error { - var localRepoDir = "" + var localRepoDir string fCmd, err := c.Text("Enter the new formula command [ex.: rit group verb noun]", true) @@ -67,17 +68,18 @@ func (c createFormulaCmd) runPrompt() CommandRunnerFunc { return err } - choice, err := c.Bool("Use default repo?", []string{"yes", "no"}) + choice, err := c.Bool("Use default repo (ritchie-formulas-local)? ", []string{"yes", "no"}) if !choice { - localRepoDir, err = c.Text("Enter your path [ex.:/home/user/my-ritchie-formulas ]", true) + // verificar user.current(pegar home dir) + my rit + localRepoDir, err = c.Text("Enter your path [ex.:/home/user/my-ritchie-formulas]", true) fmt.Println("Make sure you have Makefile and tree.json") if err != nil { return err } } - f, err := c.Create(fCmd, lang,localRepoDir) + f, err := c.Create(fCmd, lang, localRepoDir) if err != nil { return err } @@ -105,7 +107,7 @@ func (c createFormulaCmd) runStdin() CommandRunnerFunc { cf.FormulaCmd, cf.Lang, "", - ) + ) if err != nil { return err } diff --git a/pkg/cmd/mocks_test.go b/pkg/cmd/mocks_test.go index edfee95d5..4c4e4b494 100644 --- a/pkg/cmd/mocks_test.go +++ b/pkg/cmd/mocks_test.go @@ -92,7 +92,7 @@ func (repoCleaner) Clean(name string) error { type formCreator struct{} -func (formCreator) Create(formulaCmd, lang string) (formula.CreateManager, error) { +func (formCreator) Create(formulaCmd, lang, customRepoDir string) (formula.CreateManager, error) { return formula.CreateManager{}, nil } diff --git a/pkg/formula/creator.go b/pkg/formula/creator.go index 31e085175..700a219ec 100644 --- a/pkg/formula/creator.go +++ b/pkg/formula/creator.go @@ -20,6 +20,9 @@ import ( "github.com/ZupIT/ritchie-cli/pkg/formula/tpl/tpl_shell" ) +var ErrMakefileNotFound = errors.New("makefile not found") +var ErrTreeJsonNotFound = errors.New("tree.json not found") + type CreateManager struct { FormPath string treeManager TreeManager @@ -33,9 +36,13 @@ func (c CreateManager) Create(fCmd, lang, localRepoDir string) (CreateManager, e _ = fileutil.CreateDirIfNotExists(c.FormPath, os.ModePerm) if localRepoDir != "" { - if !filesVerify(localRepoDir){ - return CreateManager{}, errors.New("Makefile or tree.json not found") + if !existsMakefile(c.FormPath) { + return CreateManager{}, ErrMakefileNotFound + } + if !existsTreeJson(c.FormPath) { + return CreateManager{}, ErrTreeJsonNotFound } + c.FormPath = localRepoDir } @@ -54,7 +61,7 @@ func (c CreateManager) Create(fCmd, lang, localRepoDir string) (CreateManager, e return CreateManager{}, err } - if filesVerify(c.FormPath) { + if existsMakefile(c.FormPath) && existsTreeJson(c.FormPath) { err = generateFormulaFiles(c.FormPath, fCmd, lang, false) if err != nil { return CreateManager{}, err @@ -69,14 +76,22 @@ func (c CreateManager) Create(fCmd, lang, localRepoDir string) (CreateManager, e return c, nil } -func filesVerify(formPath string) bool { - if fileutil.Exists(fmt.Sprintf(TreeCreatePathPattern, formPath)) && - (fileutil.Exists(fmt.Sprintf("%s/%s", formPath, Makefile))) { +func existsTreeJson(formPath string) bool { + treePath := fmt.Sprintf(TreeCreatePathPattern, formPath) + if fileutil.Exists(treePath) { return true } return false } +func existsMakefile(formPath string) bool { + makefilePath := fmt.Sprintf(MakefileCreatePathPattern, formPath, Makefile) + if fileutil.Exists(makefilePath) { + return true + } + return false +} +// TODO: formula must not be static func generateFormulaFiles(formPath, fCmd, lang string, new bool) error { d := strings.Split(fCmd, " ") dirForm := strings.Join(d[1:], "/") diff --git a/pkg/formula/formula.go b/pkg/formula/formula.go index 8fe66ff6e..85b0a0324 100644 --- a/pkg/formula/formula.go +++ b/pkg/formula/formula.go @@ -27,6 +27,7 @@ const ( DefaultCacheQtd = 5 FormCreatePathPattern = "%s/ritchie-formulas-local" TreeCreatePathPattern = "%s/tree/tree.json" + MakefileCreatePathPattern = "%s/%s" Makefile = "Makefile" ) From ebd630897ecc9c551dd82044b39059aed9028926 Mon Sep 17 00:00:00 2001 From: Victor Schumacher Date: Thu, 21 May 2020 20:29:32 -0300 Subject: [PATCH 07/26] added unit tests --- pkg/file/fileutil/file_util.go | 6 +-- pkg/formula/creator.go | 7 +++- pkg/formula/creator_test.go | 76 +++++++++++++++++++++++++++++++--- pkg/formula/formula.go | 43 +++++++++---------- 4 files changed, 101 insertions(+), 31 deletions(-) diff --git a/pkg/file/fileutil/file_util.go b/pkg/file/fileutil/file_util.go index 6a631180b..2d6422e95 100644 --- a/pkg/file/fileutil/file_util.go +++ b/pkg/file/fileutil/file_util.go @@ -119,13 +119,13 @@ func CreateDirIfNotExists(dir string, perm os.FileMode) error { } // CreateFileIfNotExist creates file if not exists -func CreateFileIfNotExist(file string, content []byte) error { +func CreateFileIfNotExist(file string, content []byte) (error, error) { if _, err := os.Stat(file); os.IsNotExist(err) { if err := WriteFile(file, content); err != nil { - return err + return err, nil } } - return nil + return nil, nil } // RemoveDir removes path and any children it contains. diff --git a/pkg/formula/creator.go b/pkg/formula/creator.go index 700a219ec..324578638 100644 --- a/pkg/formula/creator.go +++ b/pkg/formula/creator.go @@ -36,14 +36,17 @@ func (c CreateManager) Create(fCmd, lang, localRepoDir string) (CreateManager, e _ = fileutil.CreateDirIfNotExists(c.FormPath, os.ModePerm) if localRepoDir != "" { - if !existsMakefile(c.FormPath) { + + if !existsMakefile(localRepoDir) { return CreateManager{}, ErrMakefileNotFound } - if !existsTreeJson(c.FormPath) { + if !existsTreeJson(localRepoDir) { + return CreateManager{}, ErrTreeJsonNotFound } c.FormPath = localRepoDir + fmt.Println(c.FormPath) } trees, err := c.treeManager.Tree() diff --git a/pkg/formula/creator_test.go b/pkg/formula/creator_test.go index 98f56ddbd..ce74f90b4 100644 --- a/pkg/formula/creator_test.go +++ b/pkg/formula/creator_test.go @@ -3,6 +3,7 @@ package formula import ( "errors" "fmt" + "log" "os" "testing" @@ -33,15 +34,60 @@ func (repoListerMock) List() ([]Repository, error) { func cleanForm() { _ = fileutil.RemoveDir(fmt.Sprintf(FormCreatePathPattern, os.TempDir())) + _ = fileutil.RemoveDir(os.TempDir() + "/customRepo") +} + +func createDirWithMakefile() (string, error) { + dir := os.TempDir() + "/customRepo" + err := fileutil.CreateDirIfNotExists(dir, os.ModePerm) + makefilePath := fmt.Sprintf("%s/%s", dir, Makefile) + _, err = fileutil.CreateFileIfNotExist(makefilePath, []byte("")) + return dir, err +} + +func createDirWithTree() (string, error) { + dir := os.TempDir() + "/customRepo" + err := fileutil.CreateDirIfNotExists(dir, os.ModePerm) + treeJsonPath := fmt.Sprintf("%s/%s", dir, TreeJson) + _, err = fileutil.CreateFileIfNotExist(treeJsonPath, []byte("")) + return dir, err +} + +func createDir(dirname string, filename string, both bool) (string, error) { + dir := os.TempDir() + dirname + treeJsonDir := fmt.Sprintf("%s/%s", dir, "tree") + treeJsonFile := fmt.Sprintf(TreeCreatePathPattern, dir) + + err := fileutil.CreateDirIfNotExists(dir, os.ModePerm) + if both { + err := fileutil.CreateDirIfNotExists(treeJsonDir, os.ModePerm) + + _, err = fileutil.CreateFileIfNotExist(treeJsonFile, []byte("")) + makefilePath := fmt.Sprintf("%s/%s", dir, Makefile) + _, err = fileutil.CreateFileIfNotExist(makefilePath, []byte("")) + return dir, err + } + path := fmt.Sprintf("%s/%s", dir, filename) + _, err = fileutil.CreateFileIfNotExist(path, []byte("")) + return dir, err } func TestCreator(t *testing.T) { cleanForm() + dir, err := createDirWithMakefile() + + fullDir,err := createDir("/customFullRepo","", true) + + fmt.Println(fullDir) + if err != nil { + log.Fatalf("Erro") + } treeMan := NewTreeManager("../../testdata", repoListerMock{}, api.SingleCoreCmds) type in struct { - fCmd string - lang string + fCmd string + lang string + customRepoDir string } type out struct { @@ -49,7 +95,6 @@ func TestCreator(t *testing.T) { } creator := NewCreator(fmt.Sprintf(FormCreatePathPattern, os.TempDir()), treeMan) - tests := []struct { name string in *in @@ -125,14 +170,35 @@ func TestCreator(t *testing.T) { err: errors.New("the formula's command needs to start with \"rit\" [ex.: rit group verb ]"), }, }, + { + name: "command to custom repo with missing packge.json", + in: &in{ + fCmd: fCmdCorrectGo, + lang: langGo, + customRepoDir: dir, + }, + out: &out{ + err: ErrTreeJsonNotFound, + }, + }, + { + name: "command to custom repo correct", + in: &in{ + fCmd: fCmdCorrectGo, + lang: langGo, + customRepoDir: fullDir, + }, + out: &out{ + err: nil, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { in := tt.in out := tt.out - - _, got := creator.Create(in.fCmd, in.lang, "") + _, got := creator.Create(in.fCmd, in.lang, in.customRepoDir) if got != nil && got.Error() != out.err.Error() { t.Errorf("Create(%s) got %v, want %v", tt.name, got, out.err) } diff --git a/pkg/formula/formula.go b/pkg/formula/formula.go index 85b0a0324..f3bb70864 100644 --- a/pkg/formula/formula.go +++ b/pkg/formula/formula.go @@ -9,26 +9,27 @@ import ( ) const ( - PathPattern = "%s/formulas/%s" - TmpDirPattern = "%s/tmp/%s" - TmpBinDirPattern = "%s/tmp/%s/%s" - DefaultConfig = "config.json" - ConfigPattern = "%s/%s" - CommandEnv = "COMMAND" - PwdEnv = "PWD" - BinPattern = "%s%s" - BinPathPattern = "%s/bin" - windows = "windows" - darwin = "darwin" - linux = "linux" - EnvPattern = "%s=%s" - CachePattern = "%s/.%s.cache" - DefaultCacheNewLabel = "Type new value?" - DefaultCacheQtd = 5 - FormCreatePathPattern = "%s/ritchie-formulas-local" - TreeCreatePathPattern = "%s/tree/tree.json" + PathPattern = "%s/formulas/%s" + TmpDirPattern = "%s/tmp/%s" + TmpBinDirPattern = "%s/tmp/%s/%s" + DefaultConfig = "config.json" + ConfigPattern = "%s/%s" + CommandEnv = "COMMAND" + PwdEnv = "PWD" + BinPattern = "%s%s" + BinPathPattern = "%s/bin" + windows = "windows" + darwin = "darwin" + linux = "linux" + EnvPattern = "%s=%s" + CachePattern = "%s/.%s.cache" + DefaultCacheNewLabel = "Type new value?" + DefaultCacheQtd = 5 + FormCreatePathPattern = "%s/ritchie-formulas-local" + TreeCreatePathPattern = "%s/tree/tree.json" MakefileCreatePathPattern = "%s/%s" - Makefile = "Makefile" + Makefile = "Makefile" + TreeJson = "tree.json" ) // Config type that represents formula config @@ -159,12 +160,12 @@ func (d *Definition) ConfigURL(configName string) string { return fmt.Sprintf("%s/%s/%s", d.RepoURL, d.Path, configName) } -//Runner defines the formula runner process +// Runner defines the formula runner process type Runner interface { Run(def Definition, inputType api.TermInputType) error } -//Creator defines the formula creator process +// Creator defines the formula creator process type Creator interface { Create(formulaCmd, lang, customRepoDir string) (CreateManager, error) } From eaa7bc0e8321bbd14580e240dedaca7c383cfa92 Mon Sep 17 00:00:00 2001 From: Victor Schumacher Date: Fri, 22 May 2020 14:45:36 -0300 Subject: [PATCH 08/26] refactor --- pkg/cmd/create_formula.go | 7 ++-- pkg/cmd/create_formula_test.go | 1 - pkg/file/fileutil/file_util.go | 6 ++-- pkg/formula/creator.go | 12 +++---- pkg/formula/creator_test.go | 54 ++++++++++++++++++------------- testdata/Makefile | 58 ++++++++++++++++++++++++++++++++++ 6 files changed, 100 insertions(+), 38 deletions(-) create mode 100644 testdata/Makefile diff --git a/pkg/cmd/create_formula.go b/pkg/cmd/create_formula.go index 6f432ee85..a709918a8 100644 --- a/pkg/cmd/create_formula.go +++ b/pkg/cmd/create_formula.go @@ -28,7 +28,6 @@ type createFormula struct { } // CreateFormulaCmd creates a new cmd instance - func NewCreateFormulaCmd(cf formula.Creator, it prompt.InputText, il prompt.InputList, ib prompt.InputBool) *cobra.Command { c := createFormulaCmd{ cf, @@ -38,7 +37,6 @@ func NewCreateFormulaCmd(cf formula.Creator, it prompt.InputText, il prompt.Inpu } cmd := &cobra.Command{ - Use: "formula", Short: "Create a new formula", Example: "rit create formula", @@ -58,7 +56,6 @@ func (c createFormulaCmd) runPrompt() CommandRunnerFunc { fCmd, err := c.Text("Enter the new formula command [ex.: rit group verb noun]", true) fmt.Println("Creating Formula ...") - if err != nil { return err } @@ -69,9 +66,8 @@ func (c createFormulaCmd) runPrompt() CommandRunnerFunc { } choice, err := c.Bool("Use default repo (ritchie-formulas-local)? ", []string{"yes", "no"}) - if !choice { - // verificar user.current(pegar home dir) + my rit + // TODO verify user.current(pegar home dir) + my rit localRepoDir, err = c.Text("Enter your path [ex.:/home/user/my-ritchie-formulas]", true) fmt.Println("Make sure you have Makefile and tree.json") if err != nil { @@ -79,6 +75,7 @@ func (c createFormulaCmd) runPrompt() CommandRunnerFunc { } } + f, err := c.Create(fCmd, lang, localRepoDir) if err != nil { return err diff --git a/pkg/cmd/create_formula_test.go b/pkg/cmd/create_formula_test.go index 1c5fddc1a..6f0d10cc9 100644 --- a/pkg/cmd/create_formula_test.go +++ b/pkg/cmd/create_formula_test.go @@ -5,7 +5,6 @@ import ( ) func TestNewCreateFormulaCmd(t *testing.T) { - cmd := NewCreateFormulaCmd(formCreator{}, inputTextMock{}, inputListMock{}, inputTrueMock{}) cmd.PersistentFlags().Bool("stdin", false, "input by stdin") if cmd == nil { diff --git a/pkg/file/fileutil/file_util.go b/pkg/file/fileutil/file_util.go index 2d6422e95..6a631180b 100644 --- a/pkg/file/fileutil/file_util.go +++ b/pkg/file/fileutil/file_util.go @@ -119,13 +119,13 @@ func CreateDirIfNotExists(dir string, perm os.FileMode) error { } // CreateFileIfNotExist creates file if not exists -func CreateFileIfNotExist(file string, content []byte) (error, error) { +func CreateFileIfNotExist(file string, content []byte) error { if _, err := os.Stat(file); os.IsNotExist(err) { if err := WriteFile(file, content); err != nil { - return err, nil + return err } } - return nil, nil + return nil } // RemoveDir removes path and any children it contains. diff --git a/pkg/formula/creator.go b/pkg/formula/creator.go index 324578638..59c221128 100644 --- a/pkg/formula/creator.go +++ b/pkg/formula/creator.go @@ -37,16 +37,15 @@ func (c CreateManager) Create(fCmd, lang, localRepoDir string) (CreateManager, e if localRepoDir != "" { - if !existsMakefile(localRepoDir) { - return CreateManager{}, ErrMakefileNotFound - } if !existsTreeJson(localRepoDir) { return CreateManager{}, ErrTreeJsonNotFound } + if !existsMakefile(localRepoDir) { + return CreateManager{}, ErrMakefileNotFound + } c.FormPath = localRepoDir - fmt.Println(c.FormPath) } trees, err := c.treeManager.Tree() @@ -89,12 +88,13 @@ func existsTreeJson(formPath string) bool { func existsMakefile(formPath string) bool { makefilePath := fmt.Sprintf(MakefileCreatePathPattern, formPath, Makefile) + if fileutil.Exists(makefilePath) { return true } return false } -// TODO: formula must not be static + func generateFormulaFiles(formPath, fCmd, lang string, new bool) error { d := strings.Split(fCmd, " ") dirForm := strings.Join(d[1:], "/") @@ -107,7 +107,6 @@ func generateFormulaFiles(formPath, fCmd, lang string, new bool) error { if err != nil && !os.IsExist(err) { return err } - err = createMakefileMain(formPath, dirForm, formulaName) if err != nil { return err @@ -119,7 +118,6 @@ func generateFormulaFiles(formPath, fCmd, lang string, new bool) error { if err != nil && !os.IsExist(err) { return err } - fmt.Println(d) err = changeMakefileMain(formPath, fCmd, formulaName) if err != nil { return err diff --git a/pkg/formula/creator_test.go b/pkg/formula/creator_test.go index ce74f90b4..61b5dd4f1 100644 --- a/pkg/formula/creator_test.go +++ b/pkg/formula/creator_test.go @@ -35,53 +35,52 @@ func (repoListerMock) List() ([]Repository, error) { func cleanForm() { _ = fileutil.RemoveDir(fmt.Sprintf(FormCreatePathPattern, os.TempDir())) _ = fileutil.RemoveDir(os.TempDir() + "/customRepo") + _ = fileutil.RemoveDir(os.TempDir() + "/customRepoMakefile") + _ = fileutil.RemoveDir(os.TempDir() + "/customRepoTreejson") } func createDirWithMakefile() (string, error) { - dir := os.TempDir() + "/customRepo" + dir := os.TempDir() + "/customRepoMakefile" err := fileutil.CreateDirIfNotExists(dir, os.ModePerm) makefilePath := fmt.Sprintf("%s/%s", dir, Makefile) - _, err = fileutil.CreateFileIfNotExist(makefilePath, []byte("")) + err = fileutil.CreateFileIfNotExist(makefilePath, []byte("")) return dir, err } func createDirWithTree() (string, error) { - dir := os.TempDir() + "/customRepo" + dir := os.TempDir() + "/customRepoTreejson" + treeJsonDir := fmt.Sprintf("%s/%s", dir, "tree") + treeJsonFile := fmt.Sprintf(TreeCreatePathPattern, dir) err := fileutil.CreateDirIfNotExists(dir, os.ModePerm) - treeJsonPath := fmt.Sprintf("%s/%s", dir, TreeJson) - _, err = fileutil.CreateFileIfNotExist(treeJsonPath, []byte("")) + err = fileutil.CreateDirIfNotExists(treeJsonDir, os.ModePerm) + err = fileutil.CreateFileIfNotExist(treeJsonFile, []byte("")) return dir, err } -func createDir(dirname string, filename string, both bool) (string, error) { - dir := os.TempDir() + dirname +func createFullDir() (string, error){ + dir := os.TempDir() + "/customRepo" treeJsonDir := fmt.Sprintf("%s/%s", dir, "tree") treeJsonFile := fmt.Sprintf(TreeCreatePathPattern, dir) - + makefilePath := fmt.Sprintf("%s/%s", dir, Makefile) err := fileutil.CreateDirIfNotExists(dir, os.ModePerm) - if both { - err := fileutil.CreateDirIfNotExists(treeJsonDir, os.ModePerm) + err = fileutil.CreateDirIfNotExists(treeJsonDir, os.ModePerm) + makefile, _ := fileutil.ReadFile("../../testdata/Makefile") + err = fileutil.CreateFileIfNotExist(makefilePath, makefile) + err = fileutil.CreateFileIfNotExist(treeJsonFile, []byte("{}")) - _, err = fileutil.CreateFileIfNotExist(treeJsonFile, []byte("")) - makefilePath := fmt.Sprintf("%s/%s", dir, Makefile) - _, err = fileutil.CreateFileIfNotExist(makefilePath, []byte("")) - return dir, err - } - path := fmt.Sprintf("%s/%s", dir, filename) - _, err = fileutil.CreateFileIfNotExist(path, []byte("")) return dir, err } func TestCreator(t *testing.T) { cleanForm() - dir, err := createDirWithMakefile() - - fullDir,err := createDir("/customFullRepo","", true) - fmt.Println(fullDir) + makefileDir, err := createDirWithMakefile() + jsonDir, err := createDirWithTree() + fullDir , err := createFullDir() if err != nil { log.Fatalf("Erro") } + treeMan := NewTreeManager("../../testdata", repoListerMock{}, api.SingleCoreCmds) type in struct { @@ -175,12 +174,23 @@ func TestCreator(t *testing.T) { in: &in{ fCmd: fCmdCorrectGo, lang: langGo, - customRepoDir: dir, + customRepoDir: makefileDir, }, out: &out{ err: ErrTreeJsonNotFound, }, }, + { + name: "command to custom repo with missing Makefile", + in: &in{ + fCmd: fCmdCorrectGo, + lang: langGo, + customRepoDir: jsonDir, + }, + out: &out{ + err: ErrMakefileNotFound, + }, + }, { name: "command to custom repo correct", in: &in{ diff --git a/testdata/Makefile b/testdata/Makefile new file mode 100644 index 000000000..78e51d624 --- /dev/null +++ b/testdata/Makefile @@ -0,0 +1,58 @@ +#Makefiles +TERRAFORM=aws/terraform +NAVIGATE_HANDBOOK=github/navigate-handbook +SEARCH_HANDBOOK=github/search-handbook +SC_COFFEE_GO=scaffold/coffee-go +SC_COFFEE_JAVA=scaffold/coffee-java +SC_COFFEE_SHELL=scaffold/coffee-shell +SC_COFFEE_PYTHON=scaffold/coffee-python +SC_COFFEE_NODE=scaffold/coffee-node +SC_SPRING_STARTER=scaffold/spring-starter +KAFKA=kafka +DOCKER=docker/compose +KUBERNETES=kubernetes/core +FAST_MERGE=github/fast-merge + +FORMULAS=$(TERRAFORM) $(SC_COFFEE_GO) $(SC_COFFEE_JAVA) $(SC_COFFEE_SHELL) $(SC_COFFEE_PYTHON) $(SC_COFFEE_NODE) $(SC_SPRING_STARTER) $(KAFKA) $(DOCKER) $(NAVIGATE_HANDBOOK) $(SEARCH_HANDBOOK) $(KUBERNETES) $(FAST_MERGE) + +PWD_INITIAL=$(shell pwd) + +FORM_TO_UPPER = $(shell echo $(form) | tr '[:lower:]' '[:upper:]') +FORM = $($(FORM_TO_UPPER)) + +push-s3: + echo $(RITCHIE_AWS_BUCKET) + echo "Init pwd: $(PWD_INITIAL)" + for formula in $(FORMULAS); do cd $$formula/src && make build && cd $(PWD_INITIAL) || exit; done + ./copy-bin-configs.sh "$(FORMULAS)" + aws s3 cp . s3://$(RITCHIE_AWS_BUCKET)/ --exclude "*" --include "formulas/*" --recursive + aws s3 cp . s3://$(RITCHIE_AWS_BUCKET)/ --exclude "*" --include "tree/tree.json" --recursive + rm -rf formulas + +bin: + echo "Init pwd: $(PWD_INITIAL)" + echo "Formulas bin: $(FORMULAS)" + for formula in $(FORMULAS); do cd $$formula/src && make build && cd $(PWD_INITIAL); done + ./copy-bin-configs.sh "$(FORMULAS)" + +test-local: +ifneq ("$(FORM)", "") + @echo "Using form true: " $(FORM_TO_UPPER) + $(MAKE) bin FORMULAS=$(FORM) + mkdir -p $(HOME)/.rit/formulas + rm -rf $(HOME)/.rit/formulas/$(FORM) + ./unzip-bin-configs.sh + cp -r formulas/* $(HOME)/.rit/formulas + rm -rf formulas +else + @echo "Use make test-local form=NAME_FORMULA for specific formula." + @echo "form false: ALL FORMULAS" + $(MAKE) bin + rm -rf $(HOME)/.rit/formulas + ./unzip-bin-configs.sh + mv formulas $(HOME)/.rit +endif + mkdir -p $(HOME)/.rit/repo/local + rm -rf $(HOME)/.rit/repo/local/tree.json + cp tree/tree.json $(HOME)/.rit/repo/local/tree.json + From 72b9cd468f735bac33b3db82e644044123124cfd Mon Sep 17 00:00:00 2001 From: Victor Schumacher Date: Fri, 22 May 2020 15:29:31 -0300 Subject: [PATCH 09/26] fix lint --- pkg/formula/creator.go | 11 ++-------- pkg/formula/creator_test.go | 40 +++++++++++++++++-------------------- 2 files changed, 20 insertions(+), 31 deletions(-) diff --git a/pkg/formula/creator.go b/pkg/formula/creator.go index 59c221128..2bfe11790 100644 --- a/pkg/formula/creator.go +++ b/pkg/formula/creator.go @@ -80,19 +80,12 @@ func (c CreateManager) Create(fCmd, lang, localRepoDir string) (CreateManager, e func existsTreeJson(formPath string) bool { treePath := fmt.Sprintf(TreeCreatePathPattern, formPath) - if fileutil.Exists(treePath) { - return true - } - return false + return fileutil.Exists(treePath) } func existsMakefile(formPath string) bool { makefilePath := fmt.Sprintf(MakefileCreatePathPattern, formPath, Makefile) - - if fileutil.Exists(makefilePath) { - return true - } - return false + return fileutil.Exists(makefilePath) } func generateFormulaFiles(formPath, fCmd, lang string, new bool) error { diff --git a/pkg/formula/creator_test.go b/pkg/formula/creator_test.go index 61b5dd4f1..fd3d70109 100644 --- a/pkg/formula/creator_test.go +++ b/pkg/formula/creator_test.go @@ -3,7 +3,6 @@ package formula import ( "errors" "fmt" - "log" "os" "testing" @@ -39,47 +38,44 @@ func cleanForm() { _ = fileutil.RemoveDir(os.TempDir() + "/customRepoTreejson") } -func createDirWithMakefile() (string, error) { +func createDirWithMakefile() string { dir := os.TempDir() + "/customRepoMakefile" - err := fileutil.CreateDirIfNotExists(dir, os.ModePerm) + _ = fileutil.CreateDirIfNotExists(dir, os.ModePerm) makefilePath := fmt.Sprintf("%s/%s", dir, Makefile) - err = fileutil.CreateFileIfNotExist(makefilePath, []byte("")) - return dir, err + _ = fileutil.CreateFileIfNotExist(makefilePath, []byte("")) + return dir } -func createDirWithTree() (string, error) { +func createDirWithTree() string { dir := os.TempDir() + "/customRepoTreejson" treeJsonDir := fmt.Sprintf("%s/%s", dir, "tree") treeJsonFile := fmt.Sprintf(TreeCreatePathPattern, dir) - err := fileutil.CreateDirIfNotExists(dir, os.ModePerm) - err = fileutil.CreateDirIfNotExists(treeJsonDir, os.ModePerm) - err = fileutil.CreateFileIfNotExist(treeJsonFile, []byte("")) - return dir, err + _ = fileutil.CreateDirIfNotExists(dir, os.ModePerm) + _ = fileutil.CreateDirIfNotExists(treeJsonDir, os.ModePerm) + _ = fileutil.CreateFileIfNotExist(treeJsonFile, []byte("")) + return dir } -func createFullDir() (string, error){ +func createFullDir() string { dir := os.TempDir() + "/customRepo" treeJsonDir := fmt.Sprintf("%s/%s", dir, "tree") treeJsonFile := fmt.Sprintf(TreeCreatePathPattern, dir) makefilePath := fmt.Sprintf("%s/%s", dir, Makefile) - err := fileutil.CreateDirIfNotExists(dir, os.ModePerm) - err = fileutil.CreateDirIfNotExists(treeJsonDir, os.ModePerm) + _ = fileutil.CreateDirIfNotExists(dir, os.ModePerm) + _ = fileutil.CreateDirIfNotExists(treeJsonDir, os.ModePerm) makefile, _ := fileutil.ReadFile("../../testdata/Makefile") - err = fileutil.CreateFileIfNotExist(makefilePath, makefile) - err = fileutil.CreateFileIfNotExist(treeJsonFile, []byte("{}")) + _ = fileutil.CreateFileIfNotExist(makefilePath, makefile) + _ = fileutil.CreateFileIfNotExist(treeJsonFile, []byte("{}")) - return dir, err + return dir } func TestCreator(t *testing.T) { cleanForm() - makefileDir, err := createDirWithMakefile() - jsonDir, err := createDirWithTree() - fullDir , err := createFullDir() - if err != nil { - log.Fatalf("Erro") - } + makefileDir := createDirWithMakefile() + jsonDir := createDirWithTree() + fullDir := createFullDir() treeMan := NewTreeManager("../../testdata", repoListerMock{}, api.SingleCoreCmds) From ae35145334d19e50d1fa5b28afeffb88e072f347 Mon Sep 17 00:00:00 2001 From: Victor Schumacher Date: Fri, 22 May 2020 15:32:15 -0300 Subject: [PATCH 10/26] fix lint --- pkg/cmd/create_formula.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cmd/create_formula.go b/pkg/cmd/create_formula.go index a709918a8..2186290f7 100644 --- a/pkg/cmd/create_formula.go +++ b/pkg/cmd/create_formula.go @@ -65,7 +65,7 @@ func (c createFormulaCmd) runPrompt() CommandRunnerFunc { return err } - choice, err := c.Bool("Use default repo (ritchie-formulas-local)? ", []string{"yes", "no"}) + choice, _ := c.Bool("Use default repo (ritchie-formulas-local)? ", []string{"yes", "no"}) if !choice { // TODO verify user.current(pegar home dir) + my rit localRepoDir, err = c.Text("Enter your path [ex.:/home/user/my-ritchie-formulas]", true) From cb299ef0ebfdd7613a281710ad175dbf6a3274d5 Mon Sep 17 00:00:00 2001 From: Victor Schumacher Date: Fri, 22 May 2020 16:29:45 -0300 Subject: [PATCH 11/26] remove useless todo and add the correct create on stdin --- pkg/cmd/create_formula.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/cmd/create_formula.go b/pkg/cmd/create_formula.go index 2186290f7..669ff767f 100644 --- a/pkg/cmd/create_formula.go +++ b/pkg/cmd/create_formula.go @@ -67,7 +67,6 @@ func (c createFormulaCmd) runPrompt() CommandRunnerFunc { choice, _ := c.Bool("Use default repo (ritchie-formulas-local)? ", []string{"yes", "no"}) if !choice { - // TODO verify user.current(pegar home dir) + my rit localRepoDir, err = c.Text("Enter your path [ex.:/home/user/my-ritchie-formulas]", true) fmt.Println("Make sure you have Makefile and tree.json") if err != nil { @@ -103,7 +102,7 @@ func (c createFormulaCmd) runStdin() CommandRunnerFunc { f, err := c.Create( cf.FormulaCmd, cf.Lang, - "", + cf.LocalRepoDir, ) if err != nil { return err From 02d635a0ffa7f9fe7335e01a75db97b501f01cf5 Mon Sep 17 00:00:00 2001 From: Victor Schumacher Date: Mon, 25 May 2020 15:04:30 -0300 Subject: [PATCH 12/26] create files when custom repo is empty --- pkg/cmd/create_formula.go | 2 +- pkg/formula/creator.go | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/pkg/cmd/create_formula.go b/pkg/cmd/create_formula.go index 669ff767f..55f26320e 100644 --- a/pkg/cmd/create_formula.go +++ b/pkg/cmd/create_formula.go @@ -68,7 +68,7 @@ func (c createFormulaCmd) runPrompt() CommandRunnerFunc { choice, _ := c.Bool("Use default repo (ritchie-formulas-local)? ", []string{"yes", "no"}) if !choice { localRepoDir, err = c.Text("Enter your path [ex.:/home/user/my-ritchie-formulas]", true) - fmt.Println("Make sure you have Makefile and tree.json") + // fmt.Println("Make sure you have Makefile and tree.json") if err != nil { return err } diff --git a/pkg/formula/creator.go b/pkg/formula/creator.go index 2bfe11790..f5e739aa3 100644 --- a/pkg/formula/creator.go +++ b/pkg/formula/creator.go @@ -37,11 +37,10 @@ func (c CreateManager) Create(fCmd, lang, localRepoDir string) (CreateManager, e if localRepoDir != "" { - if !existsTreeJson(localRepoDir) { - + if !existsTreeJson(localRepoDir) && existsMakefile(localRepoDir){ return CreateManager{}, ErrTreeJsonNotFound } - if !existsMakefile(localRepoDir) { + if !existsMakefile(localRepoDir) && existsTreeJson(localRepoDir) { return CreateManager{}, ErrMakefileNotFound } From 1a7cf5a48cd97653f2cdb779ec4feeffbead7159 Mon Sep 17 00:00:00 2001 From: Victor Schumacher Date: Tue, 26 May 2020 09:40:40 -0300 Subject: [PATCH 13/26] refactor --- pkg/cmd/create_formula.go | 9 +++++++++ pkg/formula/creator.go | 5 ++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/pkg/cmd/create_formula.go b/pkg/cmd/create_formula.go index 55f26320e..db08fede0 100644 --- a/pkg/cmd/create_formula.go +++ b/pkg/cmd/create_formula.go @@ -1,9 +1,11 @@ package cmd import ( + "errors" "fmt" "log" "os" + "strings" "github.com/spf13/cobra" @@ -12,6 +14,9 @@ import ( "github.com/ZupIT/ritchie-cli/pkg/stdin" ) +var ErrNotAllowedCharacter = errors.New(`not allowed character on formula name \/,><`) + + // createFormulaCmd type for add formula command type createFormulaCmd struct { formula.Creator @@ -54,6 +59,10 @@ func (c createFormulaCmd) runPrompt() CommandRunnerFunc { var localRepoDir string fCmd, err := c.Text("Enter the new formula command [ex.: rit group verb noun]", true) + notAllowed := `\/><,` + if strings.ContainsAny(fCmd, notAllowed){ + return ErrNotAllowedCharacter + } fmt.Println("Creating Formula ...") if err != nil { diff --git a/pkg/formula/creator.go b/pkg/formula/creator.go index f5e739aa3..840e578f3 100644 --- a/pkg/formula/creator.go +++ b/pkg/formula/creator.go @@ -88,9 +88,12 @@ func existsMakefile(formPath string) bool { } func generateFormulaFiles(formPath, fCmd, lang string, new bool) error { + + d := strings.Split(fCmd, " ") + dirForm := strings.Join(d[1:], "/") - formulaName := fmt.Sprintf("%s_%s", d[len(d)-2], d[len(d)-1]) + formulaName := strings.Join(d[1:], "_") var dir string if new { From c13679eaaa2b0fa76d3cc8304604405462b2d1d1 Mon Sep 17 00:00:00 2001 From: Victor Schumacher Date: Tue, 26 May 2020 13:29:42 -0300 Subject: [PATCH 14/26] homedir and remove commentaries --- pkg/cmd/create_formula.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/pkg/cmd/create_formula.go b/pkg/cmd/create_formula.go index db08fede0..8c3440533 100644 --- a/pkg/cmd/create_formula.go +++ b/pkg/cmd/create_formula.go @@ -14,7 +14,7 @@ import ( "github.com/ZupIT/ritchie-cli/pkg/stdin" ) -var ErrNotAllowedCharacter = errors.New(`not allowed character on formula name \/,><`) +var ErrNotAllowedCharacter = errors.New(`not allowed character on formula name \/,><@`) // createFormulaCmd type for add formula command @@ -59,7 +59,7 @@ func (c createFormulaCmd) runPrompt() CommandRunnerFunc { var localRepoDir string fCmd, err := c.Text("Enter the new formula command [ex.: rit group verb noun]", true) - notAllowed := `\/><,` + notAllowed := `\/><,@` if strings.ContainsAny(fCmd, notAllowed){ return ErrNotAllowedCharacter } @@ -73,11 +73,13 @@ func (c createFormulaCmd) runPrompt() CommandRunnerFunc { if err != nil { return err } - - choice, _ := c.Bool("Use default repo (ritchie-formulas-local)? ", []string{"yes", "no"}) + homeDir, _ := os.UserHomeDir() + ritFormulasPath := fmt.Sprintf("%s/my-ritchie-formulas", homeDir) + repoQuestion := fmt.Sprintf("Use default repo (%s)?", ritFormulasPath) + choice, _ := c.Bool(repoQuestion, []string{"yes", "no"}) if !choice { - localRepoDir, err = c.Text("Enter your path [ex.:/home/user/my-ritchie-formulas]", true) - // fmt.Println("Make sure you have Makefile and tree.json") + pathQuestion := fmt.Sprintf("Enter your path [ex.:%s]",ritFormulasPath) + localRepoDir, err = c.Text(pathQuestion, true) if err != nil { return err } From bf6f6cb433be221a5c9a34ff8b546a921a3a188a Mon Sep 17 00:00:00 2001 From: Victor Schumacher Date: Wed, 27 May 2020 10:39:54 -0300 Subject: [PATCH 15/26] reafactor switchie case --- pkg/formula/creator.go | 178 +++++++++++++++++++-------- pkg/formula/lang.go | 77 ++++++++++++ pkg/formula/tpl/tpl_java/tpl_java.go | 2 +- pkg/formula/tpl/tpl_node/tpl_node.go | 2 +- 4 files changed, 206 insertions(+), 53 deletions(-) create mode 100644 pkg/formula/lang.go diff --git a/pkg/formula/creator.go b/pkg/formula/creator.go index 840e578f3..e000e740a 100644 --- a/pkg/formula/creator.go +++ b/pkg/formula/creator.go @@ -37,7 +37,7 @@ func (c CreateManager) Create(fCmd, lang, localRepoDir string) (CreateManager, e if localRepoDir != "" { - if !existsTreeJson(localRepoDir) && existsMakefile(localRepoDir){ + if !existsTreeJson(localRepoDir) && existsMakefile(localRepoDir) { return CreateManager{}, ErrTreeJsonNotFound } if !existsMakefile(localRepoDir) && existsTreeJson(localRepoDir) { @@ -79,17 +79,16 @@ func (c CreateManager) Create(fCmd, lang, localRepoDir string) (CreateManager, e func existsTreeJson(formPath string) bool { treePath := fmt.Sprintf(TreeCreatePathPattern, formPath) - return fileutil.Exists(treePath) + return fileutil.Exists(treePath) } func existsMakefile(formPath string) bool { makefilePath := fmt.Sprintf(MakefileCreatePathPattern, formPath, Makefile) - return fileutil.Exists(makefilePath) + return fileutil.Exists(makefilePath) } func generateFormulaFiles(formPath, fCmd, lang string, new bool) error { - d := strings.Split(fCmd, " ") dirForm := strings.Join(d[1:], "/") @@ -246,14 +245,7 @@ func createSrcFiles(dir, pkg, lang string) error { if err != nil { return err } - err = createMakefileForm(srcDir, pkg, dir, tpl_go.Makefile, true) - if err != nil { - return err - } - err = createDockerfile(srcDir, tpl_go.Dockerfile) - if err != nil { - return err - } + pkgDir := fmt.Sprintf("%s/pkg/%s", srcDir, pkg) err = fileutil.CreateDirIfNotExists(pkgDir, os.ModePerm) if err != nil { @@ -264,27 +256,7 @@ func createSrcFiles(dir, pkg, lang string) error { return err } case "Java": - err = createMainFile(srcDir, pkg, tpl_java.Main, "java", "Main", true) - if err != nil { - return err - } - err = createMakefileForm(srcDir, pkg, dir, tpl_java.Makefile, false) - if err != nil { - return err - } - err = createDockerfile(srcDir, tpl_java.Dockerfile) - if err != nil { - return err - } - err = createRunTemplate(srcDir, tpl_java.RunTemplate) - if err != nil { - return err - } - err = createPkgDir(pkgDir) - if err != nil { - return err - } - err = createPkgFile(pkgDir, pkg, lang) + err = createJavaFiles(srcDir, pkg, pkgDir, dir) if err != nil { return err } @@ -305,7 +277,7 @@ func createSrcFiles(dir, pkg, lang string) error { if err != nil { return err } - err = createRunTemplate(srcDir, tpl_node.RunTemplate) + err = createRunTemplate(srcDir, tpl_node.Run) if err != nil { return err } @@ -318,23 +290,7 @@ func createSrcFiles(dir, pkg, lang string) error { return err } case "Python": - err = createMainFile(srcDir, pkg, tpl_python.Main, "py", "main", false) - if err != nil { - return err - } - err = createMakefileForm(srcDir, pkg, dir, tpl_python.Makefile, false) - if err != nil { - return err - } - err = createDockerfile(srcDir, tpl_python.Dockerfile) - if err != nil { - return err - } - err = createPkgDir(pkgDir) - if err != nil { - return err - } - err = createPkgFile(pkgDir, pkg, lang) + err = createPythonFiles(srcDir, pkg, pkgDir, dir) if err != nil { return err } @@ -363,6 +319,126 @@ func createSrcFiles(dir, pkg, lang string) error { return nil } +func createPythonFiles(srcDir, pkg, pkgDir, dir string) error { + err := createGenericFiles(srcDir, pkg, dir, Python) + if err != nil { + return err + } + err = createPkgDir(pkgDir) + if err != nil { + return err + } + pkgFile := fmt.Sprintf("%s/%s.%s", pkgDir, pkg, Python.FileFormat) + err = fileutil.WriteFile(pkgFile, []byte(Python.File)) + if err != nil { + return err + } + + return nil +} + +func createJavaFiles(srcDir, pkg, pkgDir, dir string) error { + err := createGenericFiles(srcDir, pkg, dir, Java) + if err != nil { + return err + } + err = createRunTemplate(srcDir, Java.Run) + if err != nil { + return err + } + err = createPkgDir(pkgDir) + if err != nil { + return err + } + tfj := strings.ReplaceAll(Java.File, nameBin, pkg) + fu := strings.Title(strings.ToLower(pkg)) + tfj = strings.ReplaceAll(tfj, nameBinFirstUpper, fu) + err = fileutil.WriteFile(fmt.Sprintf("%s/%s.%s", pkgDir, fu, Java.FileFormat), []byte(tfj)) + if err != nil { + return err + } + return nil +} + +func createNodeFiles(srcDir, pkg, pkgDir, dir string) error { + err := createGenericFiles(srcDir, pkg, dir, Node) + if err != nil { + return err + } + err = createRunTemplate(srcDir, Node.Run) + if err != nil { + return err + } + err = createPkgDir(pkgDir) + if err != nil { + return err + } + tfn := Node.File + tfn = strings.ReplaceAll(tfn, nameBin, pkg) + err = fileutil.WriteFile(fmt.Sprintf("%s/%s.%s", dir, pkgDir, Node.FileFormat), []byte(tfn)) + if err != nil { + return err + } + return nil +} + +func createGoFiles(srcDir, pkg, pkgDir, dir string) error { + err := createGenericFiles(srcDir, pkg, dir, Go) + if err != nil { + return err + } + err = createGoModFile(srcDir, pkg) + if err != nil { + return err + } + pkgDir := fmt.Sprintf("%s/pkg/%s", srcDir, pkg) + err = fileutil.CreateDirIfNotExists(pkgDir, os.ModePerm) + if err != nil { + return err + } + + tfgo := strings.ReplaceAll(tpl_go.Pkg, nameModule, pkg) + err = fileutil.WriteFile(fmt.Sprintf("%s/%s.%s", dir, pkgDir, Go.FileFormat), []byte(tfgo)) + if err != nil { + return err + } + return nil +} + +func createShellFiles(srcDir, pkg, pkgDir, dir string) error { + err :=createGenericFiles(srcDir,pkg,dir, Shell) + if err != nil { + return err + } + err = createPkgDir(pkgDir) + if err != nil { + return err + } + pkgFile := fmt.Sprintf("%s/%s.%s", dir, pkgDir, Shell.FileFormat) + err = fileutil.WriteFile(pkgFile, []byte(Shell.File)) + if err != nil { + return err + } + return nil +} + +func createGenericFiles(srcDir, pkg, dir string, l Lang) error { + err := createMainFile(srcDir, pkg, l.Main, l.FileFormat, l.StartFile, l.UpperCase) + if err != nil { + return err + } + err = createMakefileForm(srcDir, pkg, dir, l.Makefile, l.Compiled) + if err != nil { + return err + } + err = createDockerfile(srcDir, l.Dockerfile) + if err != nil { + return err + } + + return nil +} + func createPkgDir(pkgDir string) error { return fileutil.CreateDirIfNotExists(pkgDir, os.ModePerm) } diff --git a/pkg/formula/lang.go b/pkg/formula/lang.go new file mode 100644 index 000000000..32ed23a75 --- /dev/null +++ b/pkg/formula/lang.go @@ -0,0 +1,77 @@ +package formula + +import ( + "github.com/ZupIT/ritchie-cli/pkg/formula/tpl/tpl_java" + "github.com/ZupIT/ritchie-cli/pkg/formula/tpl/tpl_python" +) + +type Lang struct { + FileFormat string + StartFile string + Main string + Makefile string + Run string + Dockerfile string + File string + Compiled bool + UpperCase bool +} + +var Python = Lang{ + FileFormat: "py", + StartFile: "main", + Main: tpl_python.Main, + Makefile: tpl_python.Makefile, + Dockerfile: tpl_python.Dockerfile, + File: tpl_python.File, + Compiled: false, + UpperCase: false, +} + +var Java = Lang{ + FileFormat: "java", + StartFile: "Main", + Main: tpl_java.Main, + Makefile: tpl_java.Makefile, + Run: tpl_java.Run, + Dockerfile: tpl_java.Dockerfile, + File: tpl_java.File, + Compiled: false, + UpperCase: true, +} + +var Go = Lang{ + FileFormat: "java", + StartFile: "Main", + Main: tpl_java.Main, + Makefile: tpl_java.Makefile, + Run: tpl_java.Run, + Dockerfile: tpl_java.Dockerfile, + File: tpl_java.File, + Compiled: false, + UpperCase: true, +} + +var Node = Lang{ + FileFormat: "java", + StartFile: "Main", + Main: tpl_java.Main, + Makefile: tpl_java.Makefile, + Run: tpl_java.Run, + Dockerfile: tpl_java.Dockerfile, + File: tpl_java.File, + Compiled: false, + UpperCase: true, +} + +var Shell = Lang{ + FileFormat: "java", + StartFile: "Main", + Main: tpl_java.Main, + Makefile: tpl_java.Makefile, + Run: tpl_java.Run, + Dockerfile: tpl_java.Dockerfile, + File: tpl_java.File, + Compiled: false, + UpperCase: true, +} \ No newline at end of file diff --git a/pkg/formula/tpl/tpl_java/tpl_java.go b/pkg/formula/tpl/tpl_java/tpl_java.go index 94fcb7315..525160df3 100644 --- a/pkg/formula/tpl/tpl_java/tpl_java.go +++ b/pkg/formula/tpl/tpl_java/tpl_java.go @@ -48,7 +48,7 @@ ENV PATH="$JAVA_HOME/bin:${PATH}" ENTRYPOINT java -jar Main.jar` - RunTemplate = `#!/bin/sh + Run = `#!/bin/sh java -jar Main.jar` File = `package {{bin-name}}; diff --git a/pkg/formula/tpl/tpl_node/tpl_node.go b/pkg/formula/tpl/tpl_node/tpl_node.go index 8be2b9993..4b86c31f1 100644 --- a/pkg/formula/tpl/tpl_node/tpl_node.go +++ b/pkg/formula/tpl/tpl_node/tpl_node.go @@ -33,7 +33,7 @@ COPY . . ENTRYPOINT node index.js` - RunTemplate = `#!/bin/sh + Run = `#!/bin/sh node index.js` PackageJson = `{ From 039cc7a19151603e5301b21f104aff7489a307d8 Mon Sep 17 00:00:00 2001 From: Victor Schumacher Date: Wed, 27 May 2020 11:09:20 -0300 Subject: [PATCH 16/26] move function to create src --- pkg/formula/creator.go | 96 +++++------------------------------------- pkg/formula/lang.go | 40 +++++++++--------- 2 files changed, 31 insertions(+), 105 deletions(-) diff --git a/pkg/formula/creator.go b/pkg/formula/creator.go index e000e740a..0f45fbfbf 100644 --- a/pkg/formula/creator.go +++ b/pkg/formula/creator.go @@ -14,10 +14,6 @@ import ( "github.com/ZupIT/ritchie-cli/pkg/api" "github.com/ZupIT/ritchie-cli/pkg/file/fileutil" "github.com/ZupIT/ritchie-cli/pkg/formula/tpl/tpl_go" - "github.com/ZupIT/ritchie-cli/pkg/formula/tpl/tpl_java" - "github.com/ZupIT/ritchie-cli/pkg/formula/tpl/tpl_node" - "github.com/ZupIT/ritchie-cli/pkg/formula/tpl/tpl_python" - "github.com/ZupIT/ritchie-cli/pkg/formula/tpl/tpl_shell" ) var ErrMakefileNotFound = errors.New("makefile not found") @@ -237,23 +233,9 @@ func createSrcFiles(dir, pkg, lang string) error { } switch lang { case "Go": - err = createMainFile(srcDir, pkg, tpl_go.Main, "go", "main", false) + err := createGoFiles(srcDir, pkg, dir) if err != nil { - return err - } - err = createGoModFile(srcDir, pkg) - if err != nil { - return err - } - - pkgDir := fmt.Sprintf("%s/pkg/%s", srcDir, pkg) - err = fileutil.CreateDirIfNotExists(pkgDir, os.ModePerm) - if err != nil { - return err - } - err = createPkgFile(pkgDir, pkg, lang) - if err != nil { - return err + return nil } case "Java": err = createJavaFiles(srcDir, pkg, pkgDir, dir) @@ -261,31 +243,7 @@ func createSrcFiles(dir, pkg, lang string) error { return err } case "Node": - err = createMainFile(srcDir, pkg, tpl_node.Index, "js", "index", false) - if err != nil { - return err - } - err = createMakefileForm(srcDir, pkg, dir, tpl_node.Makefile, false) - if err != nil { - return err - } - err = createPackageJson(srcDir, tpl_node.PackageJson) - if err != nil { - return err - } - err = createDockerfile(srcDir, tpl_node.Dockerfile) - if err != nil { - return err - } - err = createRunTemplate(srcDir, tpl_node.Run) - if err != nil { - return err - } - err = createPkgDir(pkgDir) - if err != nil { - return err - } - err = createPkgFile(pkgDir, pkg, lang) + err =createNodeFiles(srcDir,pkg,pkgDir,dir) if err != nil { return err } @@ -295,25 +253,9 @@ func createSrcFiles(dir, pkg, lang string) error { return err } default: - err = createMainFile(srcDir, pkg, tpl_shell.Main, "sh", "main", false) - if err != nil { - return err - } - err = createMakefileForm(srcDir, pkg, dir, tpl_shell.Makefile, false) - if err != nil { - return err - } - err = createDockerfile(srcDir, tpl_shell.Dockerfile) + err = createShellFiles(srcDir, pkg, pkgDir, dir) if err != nil { - return err - } - err = createPkgDir(pkgDir) - if err != nil { - return err - } - err = createPkgFile(pkgDir, pkg, lang) - if err != nil { - return err + return nil } } return nil @@ -333,7 +275,6 @@ func createPythonFiles(srcDir, pkg, pkgDir, dir string) error { if err != nil { return err } - return nil } @@ -373,6 +314,10 @@ func createNodeFiles(srcDir, pkg, pkgDir, dir string) error { if err != nil { return err } + err = createPackageJson(srcDir, Node.PackageJson) + if err != nil { + return err + } tfn := Node.File tfn = strings.ReplaceAll(tfn, nameBin, pkg) err = fileutil.WriteFile(fmt.Sprintf("%s/%s.%s", dir, pkgDir, Node.FileFormat), []byte(tfn)) @@ -382,7 +327,7 @@ func createNodeFiles(srcDir, pkg, pkgDir, dir string) error { return nil } -func createGoFiles(srcDir, pkg, pkgDir, dir string) error { +func createGoFiles(srcDir, pkg, dir string) error { err := createGenericFiles(srcDir, pkg, dir, Go) if err != nil { return err @@ -443,27 +388,6 @@ func createPkgDir(pkgDir string) error { return fileutil.CreateDirIfNotExists(pkgDir, os.ModePerm) } -func createPkgFile(dir, pkg, lang string) error { - switch lang { - case "Go": - tfgo := strings.ReplaceAll(tpl_go.Pkg, nameModule, pkg) - return fileutil.WriteFile(fmt.Sprintf("%s/%s.go", dir, pkg), []byte(tfgo)) - case "Java": - tfj := strings.ReplaceAll(tpl_java.File, nameBin, pkg) - fu := strings.Title(strings.ToLower(pkg)) - tfj = strings.ReplaceAll(tfj, nameBinFirstUpper, fu) - return fileutil.WriteFile(fmt.Sprintf("%s/%s.java", dir, fu), []byte(tfj)) - case "Node": - tfn := tpl_node.File - tfn = strings.ReplaceAll(tfn, nameBin, pkg) - return fileutil.WriteFile(fmt.Sprintf("%s/%s.js", dir, pkg), []byte(tfn)) - case "Python": - return fileutil.WriteFile(fmt.Sprintf("%s/%s.py", dir, pkg), []byte(tpl_python.File)) - default: - return fileutil.WriteFile(fmt.Sprintf("%s/%s.sh", dir, pkg), []byte(tpl_shell.File)) - } -} - func createRunTemplate(dir, tpl string) error { return fileutil.WriteFilePerm(fmt.Sprintf("%s/run_template", dir), []byte(tpl), 0777) } diff --git a/pkg/formula/lang.go b/pkg/formula/lang.go index 32ed23a75..4d66fa865 100644 --- a/pkg/formula/lang.go +++ b/pkg/formula/lang.go @@ -1,8 +1,11 @@ package formula import ( + "github.com/ZupIT/ritchie-cli/pkg/formula/tpl/tpl_go" "github.com/ZupIT/ritchie-cli/pkg/formula/tpl/tpl_java" + "github.com/ZupIT/ritchie-cli/pkg/formula/tpl/tpl_node" "github.com/ZupIT/ritchie-cli/pkg/formula/tpl/tpl_python" + "github.com/ZupIT/ritchie-cli/pkg/formula/tpl/tpl_shell" ) type Lang struct { @@ -12,6 +15,7 @@ type Lang struct { Makefile string Run string Dockerfile string + PackageJson string File string Compiled bool UpperCase bool @@ -41,25 +45,24 @@ var Java = Lang{ } var Go = Lang{ - FileFormat: "java", - StartFile: "Main", - Main: tpl_java.Main, - Makefile: tpl_java.Makefile, - Run: tpl_java.Run, - Dockerfile: tpl_java.Dockerfile, - File: tpl_java.File, + FileFormat: "go", + StartFile: "main", + Main: tpl_go.Main, + Makefile: tpl_go.Makefile, + Dockerfile: tpl_go.Dockerfile, Compiled: false, UpperCase: true, } var Node = Lang{ - FileFormat: "java", - StartFile: "Main", - Main: tpl_java.Main, - Makefile: tpl_java.Makefile, - Run: tpl_java.Run, - Dockerfile: tpl_java.Dockerfile, - File: tpl_java.File, + FileFormat: "js", + StartFile: "index", + Main: tpl_node.Index, + Makefile: tpl_node.Makefile, + Run: tpl_node.Run, + Dockerfile: tpl_node.Dockerfile, + PackageJson: tpl_node.PackageJson, + File: tpl_node.File, Compiled: false, UpperCase: true, } @@ -67,11 +70,10 @@ var Node = Lang{ var Shell = Lang{ FileFormat: "java", StartFile: "Main", - Main: tpl_java.Main, - Makefile: tpl_java.Makefile, - Run: tpl_java.Run, - Dockerfile: tpl_java.Dockerfile, - File: tpl_java.File, + Main: tpl_shell.Main, + Makefile: tpl_shell.Makefile, + Dockerfile: tpl_shell.Dockerfile, + File: tpl_shell.File, Compiled: false, UpperCase: true, } \ No newline at end of file From db35f1565055a7adf0ad245515e412def8721004 Mon Sep 17 00:00:00 2001 From: Kadu Artur Prussek Date: Wed, 27 May 2020 11:54:52 -0300 Subject: [PATCH 17/26] Fix create pkgDir file node --- pkg/cmd/create_formula.go | 4 ++-- pkg/formula/creator.go | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/pkg/cmd/create_formula.go b/pkg/cmd/create_formula.go index 8c3440533..6842e794b 100644 --- a/pkg/cmd/create_formula.go +++ b/pkg/cmd/create_formula.go @@ -119,8 +119,8 @@ func (c createFormulaCmd) runStdin() CommandRunnerFunc { return err } - log.Printf("Formula in %s successfully created!\n", cf.Lang) - log.Printf("Your formula is in %s", f.FormPath) + fmt.Printf("Formula in %s successfully created!\n", cf.Lang) + fmt.Printf("Your formula is in %s", f.FormPath) return nil } diff --git a/pkg/formula/creator.go b/pkg/formula/creator.go index 0f45fbfbf..cf00c660f 100644 --- a/pkg/formula/creator.go +++ b/pkg/formula/creator.go @@ -243,7 +243,7 @@ func createSrcFiles(dir, pkg, lang string) error { return err } case "Node": - err =createNodeFiles(srcDir,pkg,pkgDir,dir) + err = createNodeFiles(srcDir, pkg, pkgDir, dir) if err != nil { return err } @@ -320,7 +320,8 @@ func createNodeFiles(srcDir, pkg, pkgDir, dir string) error { } tfn := Node.File tfn = strings.ReplaceAll(tfn, nameBin, pkg) - err = fileutil.WriteFile(fmt.Sprintf("%s/%s.%s", dir, pkgDir, Node.FileFormat), []byte(tfn)) + file := fmt.Sprintf("%s.%s", pkgDir, Node.FileFormat) + err = fileutil.WriteFile(file, []byte(tfn)) if err != nil { return err } @@ -351,7 +352,7 @@ func createGoFiles(srcDir, pkg, dir string) error { } func createShellFiles(srcDir, pkg, pkgDir, dir string) error { - err :=createGenericFiles(srcDir,pkg,dir, Shell) + err := createGenericFiles(srcDir, pkg, dir, Shell) if err != nil { return err } From ed87cc2cbc2ec44bc873e7f8dcdde9ae99fcc919 Mon Sep 17 00:00:00 2001 From: Kadu Artur Prussek Date: Wed, 27 May 2020 12:53:25 -0300 Subject: [PATCH 18/26] Create interface LangCreator --- pkg/formula/creator.go | 128 ++----------------- pkg/formula/lang.go | 278 +++++++++++++++++++++++++++++++---------- 2 files changed, 222 insertions(+), 184 deletions(-) diff --git a/pkg/formula/creator.go b/pkg/formula/creator.go index cf00c660f..940401975 100644 --- a/pkg/formula/creator.go +++ b/pkg/formula/creator.go @@ -233,141 +233,35 @@ func createSrcFiles(dir, pkg, lang string) error { } switch lang { case "Go": - err := createGoFiles(srcDir, pkg, dir) - if err != nil { + pkgDir := fmt.Sprintf("%s/pkg/%s", srcDir, pkg) + golang := NewGo() + if err := golang.Create(srcDir, pkgDir, pkg, dir); err != nil { return nil } case "Java": - err = createJavaFiles(srcDir, pkg, pkgDir, dir) - if err != nil { + java := NewJava() + if err := java.Create(srcDir, pkg, pkgDir, dir); err != nil { return err } case "Node": - err = createNodeFiles(srcDir, pkg, pkgDir, dir) - if err != nil { + node := NewNode() + if err := node.Create(srcDir, pkg, pkgDir, dir); err != nil { return err } case "Python": - err = createPythonFiles(srcDir, pkg, pkgDir, dir) - if err != nil { + python := NewPython() + if err := python.Create(srcDir, pkg, pkgDir, dir); err != nil { return err } default: - err = createShellFiles(srcDir, pkg, pkgDir, dir) - if err != nil { + shell := NewShell() + if err = shell.Create(srcDir, pkg, pkgDir, dir); err != nil { return nil } } return nil } -func createPythonFiles(srcDir, pkg, pkgDir, dir string) error { - err := createGenericFiles(srcDir, pkg, dir, Python) - if err != nil { - return err - } - err = createPkgDir(pkgDir) - if err != nil { - return err - } - pkgFile := fmt.Sprintf("%s/%s.%s", pkgDir, pkg, Python.FileFormat) - err = fileutil.WriteFile(pkgFile, []byte(Python.File)) - if err != nil { - return err - } - return nil -} - -func createJavaFiles(srcDir, pkg, pkgDir, dir string) error { - err := createGenericFiles(srcDir, pkg, dir, Java) - if err != nil { - return err - } - err = createRunTemplate(srcDir, Java.Run) - if err != nil { - return err - } - err = createPkgDir(pkgDir) - if err != nil { - return err - } - tfj := strings.ReplaceAll(Java.File, nameBin, pkg) - fu := strings.Title(strings.ToLower(pkg)) - tfj = strings.ReplaceAll(tfj, nameBinFirstUpper, fu) - err = fileutil.WriteFile(fmt.Sprintf("%s/%s.%s", pkgDir, fu, Java.FileFormat), []byte(tfj)) - if err != nil { - return err - } - return nil -} - -func createNodeFiles(srcDir, pkg, pkgDir, dir string) error { - err := createGenericFiles(srcDir, pkg, dir, Node) - if err != nil { - return err - } - err = createRunTemplate(srcDir, Node.Run) - if err != nil { - return err - } - err = createPkgDir(pkgDir) - if err != nil { - return err - } - err = createPackageJson(srcDir, Node.PackageJson) - if err != nil { - return err - } - tfn := Node.File - tfn = strings.ReplaceAll(tfn, nameBin, pkg) - file := fmt.Sprintf("%s.%s", pkgDir, Node.FileFormat) - err = fileutil.WriteFile(file, []byte(tfn)) - if err != nil { - return err - } - return nil -} - -func createGoFiles(srcDir, pkg, dir string) error { - err := createGenericFiles(srcDir, pkg, dir, Go) - if err != nil { - return err - } - err = createGoModFile(srcDir, pkg) - if err != nil { - return err - } - pkgDir := fmt.Sprintf("%s/pkg/%s", srcDir, pkg) - err = fileutil.CreateDirIfNotExists(pkgDir, os.ModePerm) - if err != nil { - return err - } - - tfgo := strings.ReplaceAll(tpl_go.Pkg, nameModule, pkg) - err = fileutil.WriteFile(fmt.Sprintf("%s/%s.%s", dir, pkgDir, Go.FileFormat), []byte(tfgo)) - if err != nil { - return err - } - return nil -} - -func createShellFiles(srcDir, pkg, pkgDir, dir string) error { - err := createGenericFiles(srcDir, pkg, dir, Shell) - if err != nil { - return err - } - err = createPkgDir(pkgDir) - if err != nil { - return err - } - pkgFile := fmt.Sprintf("%s/%s.%s", dir, pkgDir, Shell.FileFormat) - err = fileutil.WriteFile(pkgFile, []byte(Shell.File)) - if err != nil { - return err - } - return nil -} - func createGenericFiles(srcDir, pkg, dir string, l Lang) error { err := createMainFile(srcDir, pkg, l.Main, l.FileFormat, l.StartFile, l.UpperCase) if err != nil { diff --git a/pkg/formula/lang.go b/pkg/formula/lang.go index 4d66fa865..34ffa7e63 100644 --- a/pkg/formula/lang.go +++ b/pkg/formula/lang.go @@ -1,6 +1,11 @@ package formula import ( + "fmt" + "os" + "strings" + + "github.com/ZupIT/ritchie-cli/pkg/file/fileutil" "github.com/ZupIT/ritchie-cli/pkg/formula/tpl/tpl_go" "github.com/ZupIT/ritchie-cli/pkg/formula/tpl/tpl_java" "github.com/ZupIT/ritchie-cli/pkg/formula/tpl/tpl_node" @@ -8,72 +13,211 @@ import ( "github.com/ZupIT/ritchie-cli/pkg/formula/tpl/tpl_shell" ) +type LangCreator interface { + Create(srcDir, pkg, pkgDir, dir string) error +} + type Lang struct { - FileFormat string - StartFile string - Main string - Makefile string - Run string - Dockerfile string + FileFormat string + StartFile string + Main string + Makefile string + Run string + Dockerfile string PackageJson string - File string - Compiled bool - UpperCase bool -} - -var Python = Lang{ - FileFormat: "py", - StartFile: "main", - Main: tpl_python.Main, - Makefile: tpl_python.Makefile, - Dockerfile: tpl_python.Dockerfile, - File: tpl_python.File, - Compiled: false, - UpperCase: false, -} - -var Java = Lang{ - FileFormat: "java", - StartFile: "Main", - Main: tpl_java.Main, - Makefile: tpl_java.Makefile, - Run: tpl_java.Run, - Dockerfile: tpl_java.Dockerfile, - File: tpl_java.File, - Compiled: false, - UpperCase: true, -} - -var Go = Lang{ - FileFormat: "go", - StartFile: "main", - Main: tpl_go.Main, - Makefile: tpl_go.Makefile, - Dockerfile: tpl_go.Dockerfile, - Compiled: false, - UpperCase: true, -} - -var Node = Lang{ - FileFormat: "js", - StartFile: "index", - Main: tpl_node.Index, - Makefile: tpl_node.Makefile, - Run: tpl_node.Run, - Dockerfile: tpl_node.Dockerfile, - PackageJson: tpl_node.PackageJson, - File: tpl_node.File, - Compiled: false, - UpperCase: true, -} - -var Shell = Lang{ - FileFormat: "java", - StartFile: "Main", - Main: tpl_shell.Main, - Makefile: tpl_shell.Makefile, - Dockerfile: tpl_shell.Dockerfile, - File: tpl_shell.File, - Compiled: false, - UpperCase: true, -} \ No newline at end of file + File string + Compiled bool + UpperCase bool +} + +type Python struct { + Lang +} + +func NewPython() Python { + return Python{Lang{ + FileFormat: "py", + StartFile: "main", + Main: tpl_python.Main, + Makefile: tpl_python.Makefile, + Dockerfile: tpl_python.Dockerfile, + File: tpl_python.File, + Compiled: false, + UpperCase: false, + }} +} + +func (p Python) Create(srcDir, pkg, pkgDir, dir string) error { + if err := createGenericFiles(srcDir, pkg, dir, p.Lang); err != nil { + return err + } + + if err := createPkgDir(pkgDir); err != nil { + return err + } + + pkgFile := fmt.Sprintf("%s/%s.%s", pkgDir, pkg, p.FileFormat) + if err := fileutil.WriteFile(pkgFile, []byte(p.File)); err != nil { + return err + } + + return nil +} + +type Java struct { + Lang +} + +func NewJava() Java { + return Java{Lang{ + FileFormat: "java", + StartFile: "Main", + Main: tpl_java.Main, + Makefile: tpl_java.Makefile, + Run: tpl_java.Run, + Dockerfile: tpl_java.Dockerfile, + File: tpl_java.File, + Compiled: false, + UpperCase: true, + }} +} + +func (j Java) Create(srcDir, pkg, pkgDir, dir string) error { + if err := createGenericFiles(srcDir, pkg, dir, j.Lang); err != nil { + return err + } + + if err := createRunTemplate(srcDir, j.Run); err != nil { + return err + } + + if err := createPkgDir(pkgDir); err != nil { + return err + } + + templateFileJava := strings.ReplaceAll(j.File, nameBin, pkg) + firstUpper := strings.Title(strings.ToLower(pkg)) + templateFileJava = strings.ReplaceAll(templateFileJava, nameBinFirstUpper, firstUpper) + file := fmt.Sprintf("%s/%s.%s", pkgDir, firstUpper, j.FileFormat) + if err := fileutil.WriteFile(file, []byte(templateFileJava)); err != nil { + return err + } + + return nil +} + +type Go struct { + Lang +} + +func NewGo() Go { + return Go{Lang{ + FileFormat: "go", + StartFile: "main", + Main: tpl_go.Main, + Makefile: tpl_go.Makefile, + Dockerfile: tpl_go.Dockerfile, + Compiled: false, + UpperCase: true, + }} +} + +func (g Go) Create(srcDir, pkg, pkgDir, dir string) error { + if err := createGenericFiles(srcDir, pkg, dir, g.Lang); err != nil { + return err + } + + if err := createGoModFile(srcDir, pkg); err != nil { + return err + } + + if err := fileutil.CreateDirIfNotExists(pkgDir, os.ModePerm); err != nil { + return err + } + + templateGo := strings.ReplaceAll(tpl_go.Pkg, nameModule, pkg) + file := fmt.Sprintf("%s/%s.%s", dir, pkgDir, g.FileFormat) + if err := fileutil.WriteFile(file, []byte(templateGo)); err != nil { + return err + } + return nil +} + +type Node struct { + Lang +} + +func NewNode() Node { + return Node{Lang{ + FileFormat: "js", + StartFile: "index", + Main: tpl_node.Index, + Makefile: tpl_node.Makefile, + Run: tpl_node.Run, + Dockerfile: tpl_node.Dockerfile, + PackageJson: tpl_node.PackageJson, + File: tpl_node.File, + Compiled: false, + UpperCase: true, + }} +} + +func (n Node) Create(srcDir, pkg, pkgDir, dir string) error { + if err := createGenericFiles(srcDir, pkg, dir, n.Lang); err != nil { + return err + } + + if err := createRunTemplate(srcDir, n.Run); err != nil { + return err + } + + if err := createPkgDir(pkgDir); err != nil { + return err + } + + if err := createPackageJson(srcDir, n.PackageJson); err != nil { + return err + } + + templateNode := strings.ReplaceAll(n.File, nameBin, pkg) + file := fmt.Sprintf("%s.%s", pkgDir, n.FileFormat) + if err := fileutil.WriteFile(file, []byte(templateNode)); err != nil { + return err + } + + return nil +} + +type Shell struct { + Lang +} + +func NewShell() Shell { + return Shell{Lang{ + FileFormat: "java", + StartFile: "Main", + Main: tpl_shell.Main, + Makefile: tpl_shell.Makefile, + Dockerfile: tpl_shell.Dockerfile, + File: tpl_shell.File, + Compiled: false, + UpperCase: true, + }} +} + +func (s Shell) Create(srcDir, pkg, pkgDir, dir string) error { + if err := createGenericFiles(srcDir, pkg, dir, s.Lang); err != nil { + return err + } + + if err := createPkgDir(pkgDir); err != nil { + return err + } + + pkgFile := fmt.Sprintf("%s/%s.%s", dir, pkgDir, s.FileFormat) + if err := fileutil.WriteFile(pkgFile, []byte(s.File)); err != nil { + return err + } + + return nil +} From 87fb6ed86536ea5e9d02a1b940cb0cd854d15921 Mon Sep 17 00:00:00 2001 From: Victor Schumacher Date: Wed, 27 May 2020 14:41:57 -0300 Subject: [PATCH 19/26] fixed structures --- pkg/formula/creator.go | 2 +- pkg/formula/lang.go | 22 ++++++++++++---------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/pkg/formula/creator.go b/pkg/formula/creator.go index 940401975..cc9683e9d 100644 --- a/pkg/formula/creator.go +++ b/pkg/formula/creator.go @@ -235,7 +235,7 @@ func createSrcFiles(dir, pkg, lang string) error { case "Go": pkgDir := fmt.Sprintf("%s/pkg/%s", srcDir, pkg) golang := NewGo() - if err := golang.Create(srcDir, pkgDir, pkg, dir); err != nil { + if err := golang.Create(srcDir, pkg, pkgDir, dir); err != nil { return nil } case "Java": diff --git a/pkg/formula/lang.go b/pkg/formula/lang.go index 34ffa7e63..4b64d8a86 100644 --- a/pkg/formula/lang.go +++ b/pkg/formula/lang.go @@ -26,6 +26,7 @@ type Lang struct { Dockerfile string PackageJson string File string + Pkg string Compiled bool UpperCase bool } @@ -98,8 +99,8 @@ func (j Java) Create(srcDir, pkg, pkgDir, dir string) error { templateFileJava := strings.ReplaceAll(j.File, nameBin, pkg) firstUpper := strings.Title(strings.ToLower(pkg)) templateFileJava = strings.ReplaceAll(templateFileJava, nameBinFirstUpper, firstUpper) - file := fmt.Sprintf("%s/%s.%s", pkgDir, firstUpper, j.FileFormat) - if err := fileutil.WriteFile(file, []byte(templateFileJava)); err != nil { + pkgFile := fmt.Sprintf("%s/%s.%s", pkgDir, firstUpper, j.FileFormat) + if err := fileutil.WriteFile(pkgFile, []byte(templateFileJava)); err != nil { return err } @@ -117,6 +118,7 @@ func NewGo() Go { Main: tpl_go.Main, Makefile: tpl_go.Makefile, Dockerfile: tpl_go.Dockerfile, + Pkg: tpl_go.Pkg, Compiled: false, UpperCase: true, }} @@ -135,9 +137,9 @@ func (g Go) Create(srcDir, pkg, pkgDir, dir string) error { return err } - templateGo := strings.ReplaceAll(tpl_go.Pkg, nameModule, pkg) - file := fmt.Sprintf("%s/%s.%s", dir, pkgDir, g.FileFormat) - if err := fileutil.WriteFile(file, []byte(templateGo)); err != nil { + templateGo := strings.ReplaceAll(g.Pkg, nameModule, pkg) + pkgFile := fmt.Sprintf("%s/%s.%s", pkgDir,pkg ,g.FileFormat) + if err := fileutil.WriteFile(pkgFile, []byte(templateGo)); err != nil { return err } return nil @@ -180,8 +182,8 @@ func (n Node) Create(srcDir, pkg, pkgDir, dir string) error { } templateNode := strings.ReplaceAll(n.File, nameBin, pkg) - file := fmt.Sprintf("%s.%s", pkgDir, n.FileFormat) - if err := fileutil.WriteFile(file, []byte(templateNode)); err != nil { + pkgFile := fmt.Sprintf("%s/%s.%s", pkgDir, pkg, n.FileFormat) + if err := fileutil.WriteFile(pkgFile, []byte(templateNode)); err != nil { return err } @@ -194,8 +196,8 @@ type Shell struct { func NewShell() Shell { return Shell{Lang{ - FileFormat: "java", - StartFile: "Main", + FileFormat: "sh", + StartFile: "main", Main: tpl_shell.Main, Makefile: tpl_shell.Makefile, Dockerfile: tpl_shell.Dockerfile, @@ -214,7 +216,7 @@ func (s Shell) Create(srcDir, pkg, pkgDir, dir string) error { return err } - pkgFile := fmt.Sprintf("%s/%s.%s", dir, pkgDir, s.FileFormat) + pkgFile := fmt.Sprintf("%s/%s.%s", pkgDir, pkg, s.FileFormat) if err := fileutil.WriteFile(pkgFile, []byte(s.File)); err != nil { return err } From 33013884338b23cb74b9d15ac433c61f22cb3baa Mon Sep 17 00:00:00 2001 From: Victor Schumacher Date: Wed, 27 May 2020 14:59:14 -0300 Subject: [PATCH 20/26] not allowed chars as a const --- pkg/cmd/create_formula.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/cmd/create_formula.go b/pkg/cmd/create_formula.go index 6842e794b..3a0afd98d 100644 --- a/pkg/cmd/create_formula.go +++ b/pkg/cmd/create_formula.go @@ -15,7 +15,7 @@ import ( ) var ErrNotAllowedCharacter = errors.New(`not allowed character on formula name \/,><@`) - +const notAllowedChars = `\/><,@` // createFormulaCmd type for add formula command type createFormulaCmd struct { @@ -59,8 +59,8 @@ func (c createFormulaCmd) runPrompt() CommandRunnerFunc { var localRepoDir string fCmd, err := c.Text("Enter the new formula command [ex.: rit group verb noun]", true) - notAllowed := `\/><,@` - if strings.ContainsAny(fCmd, notAllowed){ + + if strings.ContainsAny(fCmd, notAllowedChars){ return ErrNotAllowedCharacter } From 3ebeb3ddb0bdfc5ed374ca5c9334f250abc8a914 Mon Sep 17 00:00:00 2001 From: Victor Schumacher Date: Wed, 27 May 2020 16:01:39 -0300 Subject: [PATCH 21/26] add struct at create formula --- pkg/cmd/create_formula.go | 50 +++++++++++----------- pkg/cmd/mocks_test.go | 2 +- pkg/formula/creator.go | 5 ++- pkg/formula/creator_test.go | 82 +++++++++++++++++++------------------ pkg/formula/formula.go | 8 +++- 5 files changed, 76 insertions(+), 71 deletions(-) diff --git a/pkg/cmd/create_formula.go b/pkg/cmd/create_formula.go index 3a0afd98d..a56131ec1 100644 --- a/pkg/cmd/create_formula.go +++ b/pkg/cmd/create_formula.go @@ -3,7 +3,6 @@ package cmd import ( "errors" "fmt" - "log" "os" "strings" @@ -15,6 +14,7 @@ import ( ) var ErrNotAllowedCharacter = errors.New(`not allowed character on formula name \/,><@`) + const notAllowedChars = `\/><,@` // createFormulaCmd type for add formula command @@ -25,13 +25,6 @@ type createFormulaCmd struct { prompt.InputBool } -// createFormula type for stdin json decoder -type createFormula struct { - FormulaCmd string `json:"formulaCmd"` - Lang string `json:"lang"` - LocalRepoDir string `json:"localRepoDir"` -} - // CreateFormulaCmd creates a new cmd instance func NewCreateFormulaCmd(cf formula.Creator, it prompt.InputText, il prompt.InputList, ib prompt.InputBool) *cobra.Command { c := createFormulaCmd{ @@ -55,19 +48,16 @@ func NewCreateFormulaCmd(cf formula.Creator, it prompt.InputText, il prompt.Inpu func (c createFormulaCmd) runPrompt() CommandRunnerFunc { return func(cmd *cobra.Command, args []string) error { - - var localRepoDir string - fCmd, err := c.Text("Enter the new formula command [ex.: rit group verb noun]", true) + if err != nil { + return err + } - if strings.ContainsAny(fCmd, notAllowedChars){ + if strings.ContainsAny(fCmd, notAllowedChars) { return ErrNotAllowedCharacter } fmt.Println("Creating Formula ...") - if err != nil { - return err - } lang, err := c.List("Choose the language: ", []string{"Go", "Java", "Node", "Python", "Shell"}) if err != nil { @@ -76,9 +66,10 @@ func (c createFormulaCmd) runPrompt() CommandRunnerFunc { homeDir, _ := os.UserHomeDir() ritFormulasPath := fmt.Sprintf("%s/my-ritchie-formulas", homeDir) repoQuestion := fmt.Sprintf("Use default repo (%s)?", ritFormulasPath) + var localRepoDir string choice, _ := c.Bool(repoQuestion, []string{"yes", "no"}) if !choice { - pathQuestion := fmt.Sprintf("Enter your path [ex.:%s]",ritFormulasPath) + pathQuestion := fmt.Sprintf("Enter your path [ex.:%s]", ritFormulasPath) localRepoDir, err = c.Text(pathQuestion, true) if err != nil { return err @@ -86,13 +77,19 @@ func (c createFormulaCmd) runPrompt() CommandRunnerFunc { } - f, err := c.Create(fCmd, lang, localRepoDir) + cf := formula.Create{ + FormulaCmd: fCmd, + Lang: lang, + LocalRepoDir: localRepoDir, + } + + f, err := c.Create(cf) if err != nil { return err } - log.Printf("Formula in %s successfully created!\n", lang) - log.Printf("Your formula is in %s", f.FormPath) + fmt.Printf("Formula in %s successfully created!\n", lang) + fmt.Printf("Your formula is in %s", f.FormPath) return nil } @@ -102,19 +99,18 @@ func (c createFormulaCmd) runStdin() CommandRunnerFunc { return func(cmd *cobra.Command, args []string) error { fmt.Println("Creating Formula ...") - cf := createFormula{} + var cf formula.Create - err := stdin.ReadJson(os.Stdin, &cf) - if err != nil { + if err := stdin.ReadJson(os.Stdin, &cf); err != nil { fmt.Println("The STDIN inputs weren't informed correctly. Check the JSON used to execute the command.") return err } - f, err := c.Create( - cf.FormulaCmd, - cf.Lang, - cf.LocalRepoDir, - ) + if strings.ContainsAny(cf.FormulaCmd, notAllowedChars) { + return ErrNotAllowedCharacter + } + + f, err := c.Create(cf) if err != nil { return err } diff --git a/pkg/cmd/mocks_test.go b/pkg/cmd/mocks_test.go index 4c4e4b494..7b4e0cd00 100644 --- a/pkg/cmd/mocks_test.go +++ b/pkg/cmd/mocks_test.go @@ -92,7 +92,7 @@ func (repoCleaner) Clean(name string) error { type formCreator struct{} -func (formCreator) Create(formulaCmd, lang, customRepoDir string) (formula.CreateManager, error) { +func (formCreator) Create(cf formula.Create) (formula.CreateManager, error) { return formula.CreateManager{}, nil } diff --git a/pkg/formula/creator.go b/pkg/formula/creator.go index cc9683e9d..762e761c4 100644 --- a/pkg/formula/creator.go +++ b/pkg/formula/creator.go @@ -28,8 +28,11 @@ func NewCreator(homePath string, tm TreeManager) CreateManager { return CreateManager{FormPath: fmt.Sprintf(FormCreatePathPattern, homePath), treeManager: tm} } -func (c CreateManager) Create(fCmd, lang, localRepoDir string) (CreateManager, error) { +func (c CreateManager) Create(cf Create) (CreateManager, error) { _ = fileutil.CreateDirIfNotExists(c.FormPath, os.ModePerm) + localRepoDir := cf.LocalRepoDir + fCmd := cf.FormulaCmd + lang := cf.Lang if localRepoDir != "" { diff --git a/pkg/formula/creator_test.go b/pkg/formula/creator_test.go index fd3d70109..1440bf16e 100644 --- a/pkg/formula/creator_test.go +++ b/pkg/formula/creator_test.go @@ -78,12 +78,14 @@ func TestCreator(t *testing.T) { fullDir := createFullDir() treeMan := NewTreeManager("../../testdata", repoListerMock{}, api.SingleCoreCmds) + // + // type in struct { + // fCmd string + // lang string + // customRepoDir string + // } + type in Create - type in struct { - fCmd string - lang string - customRepoDir string - } type out struct { err error @@ -92,14 +94,14 @@ func TestCreator(t *testing.T) { creator := NewCreator(fmt.Sprintf(FormCreatePathPattern, os.TempDir()), treeMan) tests := []struct { name string - in *in + in *Create out *out }{ { name: "command exists", - in: &in{ - fCmd: fCmdExists, - lang: langGo, + in: &Create{ + FormulaCmd: fCmdExists, + Lang: langGo, }, out: &out{ err: errors.New("this command already exists"), @@ -107,9 +109,9 @@ func TestCreator(t *testing.T) { }, { name: "command correct-go", - in: &in{ - fCmd: fCmdCorrectGo, - lang: langGo, + in: &Create{ + FormulaCmd: fCmdCorrectGo, + Lang: langGo, }, out: &out{ err: nil, @@ -117,9 +119,9 @@ func TestCreator(t *testing.T) { }, { name: "command correct-java", - in: &in{ - fCmd: fCmdCorrectJava, - lang: langJava, + in: &Create{ + FormulaCmd: fCmdCorrectJava, + Lang: langJava, }, out: &out{ err: nil, @@ -127,9 +129,9 @@ func TestCreator(t *testing.T) { }, { name: "command correct-node", - in: &in{ - fCmd: fCmdCorrectNode, - lang: langNode, + in: &Create{ + FormulaCmd: fCmdCorrectNode, + Lang: langNode, }, out: &out{ err: nil, @@ -137,9 +139,9 @@ func TestCreator(t *testing.T) { }, { name: "command correct-python", - in: &in{ - fCmd: fCmdCorrectPython, - lang: langPython, + in: &Create{ + FormulaCmd: fCmdCorrectPython, + Lang: langPython, }, out: &out{ err: nil, @@ -147,9 +149,9 @@ func TestCreator(t *testing.T) { }, { name: "command correct-shell", - in: &in{ - fCmd: fCmdCorrectShell, - lang: langShell, + in: &Create{ + FormulaCmd: fCmdCorrectShell, + Lang: langShell, }, out: &out{ err: nil, @@ -157,9 +159,9 @@ func TestCreator(t *testing.T) { }, { name: "command incorrect", - in: &in{ - fCmd: fCmdIncorrect, - lang: langGo, + in: &Create{ + FormulaCmd: fCmdIncorrect, + Lang: langGo, }, out: &out{ err: errors.New("the formula's command needs to start with \"rit\" [ex.: rit group verb ]"), @@ -167,10 +169,10 @@ func TestCreator(t *testing.T) { }, { name: "command to custom repo with missing packge.json", - in: &in{ - fCmd: fCmdCorrectGo, - lang: langGo, - customRepoDir: makefileDir, + in: &Create{ + FormulaCmd: fCmdCorrectGo, + Lang: langGo, + LocalRepoDir: makefileDir, }, out: &out{ err: ErrTreeJsonNotFound, @@ -178,10 +180,10 @@ func TestCreator(t *testing.T) { }, { name: "command to custom repo with missing Makefile", - in: &in{ - fCmd: fCmdCorrectGo, - lang: langGo, - customRepoDir: jsonDir, + in: &Create{ + FormulaCmd: fCmdCorrectGo, + Lang: langGo, + LocalRepoDir: jsonDir, }, out: &out{ err: ErrMakefileNotFound, @@ -189,10 +191,10 @@ func TestCreator(t *testing.T) { }, { name: "command to custom repo correct", - in: &in{ - fCmd: fCmdCorrectGo, - lang: langGo, - customRepoDir: fullDir, + in: &Create{ + FormulaCmd: fCmdCorrectGo, + Lang: langGo, + LocalRepoDir: fullDir, }, out: &out{ err: nil, @@ -204,7 +206,7 @@ func TestCreator(t *testing.T) { t.Run(tt.name, func(t *testing.T) { in := tt.in out := tt.out - _, got := creator.Create(in.fCmd, in.lang, in.customRepoDir) + _, got := creator.Create(*in) if got != nil && got.Error() != out.err.Error() { t.Errorf("Create(%s) got %v, want %v", tt.name, got, out.err) } diff --git a/pkg/formula/formula.go b/pkg/formula/formula.go index f3bb70864..7d4312365 100644 --- a/pkg/formula/formula.go +++ b/pkg/formula/formula.go @@ -56,7 +56,11 @@ type Cache struct { Qtd int `json:"qtd"` NewLabel string `json:"newLabel"` } - +type Create struct { + FormulaCmd string `json:"formulaCmd"` + Lang string `json:"lang"` + LocalRepoDir string `json:"localRepoDir"` +} // Definition type that represents a Formula type Definition struct { Path string @@ -167,5 +171,5 @@ type Runner interface { // Creator defines the formula creator process type Creator interface { - Create(formulaCmd, lang, customRepoDir string) (CreateManager, error) + Create(cf Create) (CreateManager, error) } From 8889a7cf6f54f0aa55e3ca13f835ac0eb0a0b63f Mon Sep 17 00:00:00 2001 From: Victor Schumacher Date: Wed, 27 May 2020 16:06:14 -0300 Subject: [PATCH 22/26] removed unused comment --- pkg/formula/creator_test.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/pkg/formula/creator_test.go b/pkg/formula/creator_test.go index 1440bf16e..17d95a125 100644 --- a/pkg/formula/creator_test.go +++ b/pkg/formula/creator_test.go @@ -78,14 +78,8 @@ func TestCreator(t *testing.T) { fullDir := createFullDir() treeMan := NewTreeManager("../../testdata", repoListerMock{}, api.SingleCoreCmds) - // - // type in struct { - // fCmd string - // lang string - // customRepoDir string - // } - type in Create + type in Create type out struct { err error From 56591ac19ed2836afa705410194ef8637837be2b Mon Sep 17 00:00:00 2001 From: Victor Schumacher Date: Wed, 27 May 2020 16:10:56 -0300 Subject: [PATCH 23/26] remove unused var --- pkg/formula/creator_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/pkg/formula/creator_test.go b/pkg/formula/creator_test.go index 17d95a125..dc622f150 100644 --- a/pkg/formula/creator_test.go +++ b/pkg/formula/creator_test.go @@ -79,8 +79,6 @@ func TestCreator(t *testing.T) { treeMan := NewTreeManager("../../testdata", repoListerMock{}, api.SingleCoreCmds) - type in Create - type out struct { err error } From d5316afc75a7fcf03a4397c80b8fff977524485c Mon Sep 17 00:00:00 2001 From: Victor Schumacher Date: Wed, 27 May 2020 16:41:06 -0300 Subject: [PATCH 24/26] lang name as a const --- pkg/formula/creator.go | 12 ++++++------ pkg/formula/lang.go | 7 +++++++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/pkg/formula/creator.go b/pkg/formula/creator.go index 762e761c4..05dba85c7 100644 --- a/pkg/formula/creator.go +++ b/pkg/formula/creator.go @@ -235,23 +235,23 @@ func createSrcFiles(dir, pkg, lang string) error { return err } switch lang { - case "Go": + case GoName: pkgDir := fmt.Sprintf("%s/pkg/%s", srcDir, pkg) golang := NewGo() if err := golang.Create(srcDir, pkg, pkgDir, dir); err != nil { return nil } - case "Java": + case JavaName: java := NewJava() if err := java.Create(srcDir, pkg, pkgDir, dir); err != nil { return err } - case "Node": + case NodeName: node := NewNode() if err := node.Create(srcDir, pkg, pkgDir, dir); err != nil { return err } - case "Python": + case PythonName: python := NewPython() if err := python.Create(srcDir, pkg, pkgDir, dir); err != nil { return err @@ -339,7 +339,7 @@ func updateTree(fCmd string, t Tree, lang string, i int) (Tree, error) { pathValue := strings.Join(fc, "/") fn := fc[len(fc)-1] var commands []api.Command - if lang == "Python" { + if lang == PythonName { commands = append(t.Commands, api.Command{ Usage: fn, Help: fmt.Sprintf("%s %s", fc[i-1], fc[i]), @@ -354,7 +354,7 @@ func updateTree(fCmd string, t Tree, lang string, i int) (Tree, error) { }, Parent: parent, }) - } else if lang == "Go" { + } else if lang == GoName { commands = append(t.Commands, api.Command{ Usage: fn, Help: fmt.Sprintf("%s %s", fc[i-1], fc[i]), diff --git a/pkg/formula/lang.go b/pkg/formula/lang.go index 4b64d8a86..4842b86bf 100644 --- a/pkg/formula/lang.go +++ b/pkg/formula/lang.go @@ -13,6 +13,13 @@ import ( "github.com/ZupIT/ritchie-cli/pkg/formula/tpl/tpl_shell" ) +const ( + PythonName = "Python" + JavaName = "Java" + GoName = "Go" + NodeName = "Node" +) + type LangCreator interface { Create(srcDir, pkg, pkgDir, dir string) error } From 9d601701258471725d3b903df38ff74035225dea Mon Sep 17 00:00:00 2001 From: Victor Schumacher Date: Wed, 27 May 2020 17:22:21 -0300 Subject: [PATCH 25/26] const on lang structs --- pkg/formula/lang.go | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/pkg/formula/lang.go b/pkg/formula/lang.go index 4842b86bf..faa942447 100644 --- a/pkg/formula/lang.go +++ b/pkg/formula/lang.go @@ -14,10 +14,18 @@ import ( ) const ( + main = "main" + Main = "Main" + index = "index" PythonName = "Python" + PyFormat = "py" JavaName = "Java" + JavaFormat = "java" GoName = "Go" + GoFormat = "go" NodeName = "Node" + NodeFormat = "js" + ShellFormat = "sh" ) type LangCreator interface { @@ -44,8 +52,8 @@ type Python struct { func NewPython() Python { return Python{Lang{ - FileFormat: "py", - StartFile: "main", + FileFormat: PyFormat, + StartFile: main, Main: tpl_python.Main, Makefile: tpl_python.Makefile, Dockerfile: tpl_python.Dockerfile, @@ -78,8 +86,8 @@ type Java struct { func NewJava() Java { return Java{Lang{ - FileFormat: "java", - StartFile: "Main", + FileFormat: JavaFormat, + StartFile: Main, Main: tpl_java.Main, Makefile: tpl_java.Makefile, Run: tpl_java.Run, @@ -120,8 +128,8 @@ type Go struct { func NewGo() Go { return Go{Lang{ - FileFormat: "go", - StartFile: "main", + FileFormat: GoFormat, + StartFile: main, Main: tpl_go.Main, Makefile: tpl_go.Makefile, Dockerfile: tpl_go.Dockerfile, @@ -158,8 +166,8 @@ type Node struct { func NewNode() Node { return Node{Lang{ - FileFormat: "js", - StartFile: "index", + FileFormat: NodeFormat, + StartFile: index, Main: tpl_node.Index, Makefile: tpl_node.Makefile, Run: tpl_node.Run, @@ -203,8 +211,8 @@ type Shell struct { func NewShell() Shell { return Shell{Lang{ - FileFormat: "sh", - StartFile: "main", + FileFormat: ShellFormat, + StartFile: main, Main: tpl_shell.Main, Makefile: tpl_shell.Makefile, Dockerfile: tpl_shell.Dockerfile, From e0738bc0d5a3d7da4c90231f0be91b4ae96375af Mon Sep 17 00:00:00 2001 From: Victor Schumacher Date: Wed, 27 May 2020 17:45:42 -0300 Subject: [PATCH 26/26] change name to lang --- pkg/formula/creator.go | 12 ++++++------ pkg/formula/lang.go | 22 +++++++++++----------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/pkg/formula/creator.go b/pkg/formula/creator.go index 05dba85c7..897a04fe0 100644 --- a/pkg/formula/creator.go +++ b/pkg/formula/creator.go @@ -235,23 +235,23 @@ func createSrcFiles(dir, pkg, lang string) error { return err } switch lang { - case GoName: + case Golang: pkgDir := fmt.Sprintf("%s/pkg/%s", srcDir, pkg) golang := NewGo() if err := golang.Create(srcDir, pkg, pkgDir, dir); err != nil { return nil } - case JavaName: + case Javalang: java := NewJava() if err := java.Create(srcDir, pkg, pkgDir, dir); err != nil { return err } - case NodeName: + case Nodelang: node := NewNode() if err := node.Create(srcDir, pkg, pkgDir, dir); err != nil { return err } - case PythonName: + case Pythonlang: python := NewPython() if err := python.Create(srcDir, pkg, pkgDir, dir); err != nil { return err @@ -339,7 +339,7 @@ func updateTree(fCmd string, t Tree, lang string, i int) (Tree, error) { pathValue := strings.Join(fc, "/") fn := fc[len(fc)-1] var commands []api.Command - if lang == PythonName { + if lang == Pythonlang { commands = append(t.Commands, api.Command{ Usage: fn, Help: fmt.Sprintf("%s %s", fc[i-1], fc[i]), @@ -354,7 +354,7 @@ func updateTree(fCmd string, t Tree, lang string, i int) (Tree, error) { }, Parent: parent, }) - } else if lang == GoName { + } else if lang == Golang { commands = append(t.Commands, api.Command{ Usage: fn, Help: fmt.Sprintf("%s %s", fc[i-1], fc[i]), diff --git a/pkg/formula/lang.go b/pkg/formula/lang.go index faa942447..62d25541a 100644 --- a/pkg/formula/lang.go +++ b/pkg/formula/lang.go @@ -14,17 +14,17 @@ import ( ) const ( - main = "main" - Main = "Main" - index = "index" - PythonName = "Python" - PyFormat = "py" - JavaName = "Java" - JavaFormat = "java" - GoName = "Go" - GoFormat = "go" - NodeName = "Node" - NodeFormat = "js" + main = "main" + Main = "Main" + index = "index" + Pythonlang = "Python" + PyFormat = "py" + Javalang = "Java" + JavaFormat = "java" + Golang = "Go" + GoFormat = "go" + Nodelang = "Node" + NodeFormat = "js" ShellFormat = "sh" )