From 89f8330bf5edc99e0852b717d7114a14155b2ff4 Mon Sep 17 00:00:00 2001 From: Antonio Oliveira Date: Thu, 25 Jun 2020 11:16:33 -0300 Subject: [PATCH 1/6] adding new files --- pkg/cmd/create_formula.go | 2 +- pkg/formula/creator.go | 5 +++ pkg/formula/lang.go | 42 ++++++++++++++++++++++ pkg/formula/tpl/tpl_php/tpl_php.go | 56 ++++++++++++++++++++++++++++++ 4 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 pkg/formula/tpl/tpl_php/tpl_php.go diff --git a/pkg/cmd/create_formula.go b/pkg/cmd/create_formula.go index d37d90b1d..dd99e8ccc 100644 --- a/pkg/cmd/create_formula.go +++ b/pkg/cmd/create_formula.go @@ -58,7 +58,7 @@ func (c createFormulaCmd) runPrompt() CommandRunnerFunc { return ErrNotAllowedCharacter } - lang, err := c.List("Choose the language: ", []string{"Go", "Java", "Node", "Python", "Shell"}) + lang, err := c.List("Choose the language: ", []string{"Go", "Java", "Node", "PHP", "Python", "Shell"}) if err != nil { return err } diff --git a/pkg/formula/creator.go b/pkg/formula/creator.go index 7f7c33650..e3bc86621 100644 --- a/pkg/formula/creator.go +++ b/pkg/formula/creator.go @@ -266,6 +266,11 @@ func createSrcFiles(dir, pkg, lang string) error { if err := node.Create(srcDir, pkg, pkgDir, dir); err != nil { return err } + case PHPlang: + php := NewPHP() + if err := php.Create(srcDir, pkg, pkgDir, dir); err != nil { + return err + } case Pythonlang: python := NewPython() if err := python.Create(srcDir, pkg, pkgDir, dir); err != nil { diff --git a/pkg/formula/lang.go b/pkg/formula/lang.go index d7a8bcf7f..8e3a64f17 100644 --- a/pkg/formula/lang.go +++ b/pkg/formula/lang.go @@ -9,6 +9,7 @@ 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_php" "github.com/ZupIT/ritchie-cli/pkg/formula/tpl/tpl_python" "github.com/ZupIT/ritchie-cli/pkg/formula/tpl/tpl_shell" ) @@ -26,6 +27,8 @@ const ( Nodelang = "Node" NodeFormat = "js" ShellFormat = "sh" + PHPlang = "PHP" + PHPFormat = "php" ) type LangCreator interface { @@ -205,6 +208,45 @@ func (n Node) Create(srcDir, pkg, pkgDir, dir string) error { return nil } +// ------------------------------------------------ + +type PHP struct { + Lang +} + +func NewPHP() PHP { + return PHP{Lang{ + FileFormat: PHPFormat, + StartFile: index, + Main: tpl_php.Index, + Makefile: tpl_php.Makefile, + Run: tpl_php.Run, + Dockerfile: tpl_php.Dockerfile, + File: tpl_php.File, + Compiled: false, + UpperCase: false, + }} +} + +func (p PHP) 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 Shell struct { Lang } diff --git a/pkg/formula/tpl/tpl_php/tpl_php.go b/pkg/formula/tpl/tpl_php/tpl_php.go new file mode 100644 index 000000000..f3fc2abe6 --- /dev/null +++ b/pkg/formula/tpl/tpl_php/tpl_php.go @@ -0,0 +1,56 @@ +package tpl_php + +const ( + Index = ` +` + + Makefile = `# Make Run PHP +BINARY_NAME={{bin-name}}.sh +BINARY_NAME_WINDOWS={{bin-name}}.bat +DIST=../dist +DIST_DIR=$(DIST)/commons/bin +build: + mkdir -p $(DIST_DIR) + cp index.php $(DIST_DIR) && cp -r {{bin-name}} Dockerfile set_umask.sh $(DIST_DIR) + chmod +x $(DIST_DIR)/index.php + echo 'php index.php' >> $(DIST_DIR)/$(BINARY_NAME_WINDOWS)` + + Dockerfile = ` +FROM php:7.4-cli + +COPY . . + +RUN chmod +x set_umask.sh + +WORKDIR /app + +ENTRYPOINT ["/set_umask.sh"] + +CMD ["php", "./index.php"]` + +Run = `#!/bin/sh + +php -f index.php + +` + + File = ` +` +) From eaaf74d1f8bf8ca9a4ee5cf78dc0138116ecc3aa Mon Sep 17 00:00:00 2001 From: Antonio Oliveira Date: Thu, 25 Jun 2020 14:18:01 -0300 Subject: [PATCH 2/6] Adjusting makefile --- pkg/formula/lang.go | 7 ++++++- pkg/formula/tpl/tpl_php/tpl_php.go | 12 ++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/pkg/formula/lang.go b/pkg/formula/lang.go index 8e3a64f17..ec66d1ee3 100644 --- a/pkg/formula/lang.go +++ b/pkg/formula/lang.go @@ -233,12 +233,17 @@ func (p PHP) Create(srcDir, pkg, pkgDir, dir string) error { return err } + if err := createRunTemplate(srcDir, p.Run); err != nil { + return err + } + if err := createPkgDir(pkgDir); err != nil { return err } + templatePHP := strings.ReplaceAll(p.File, nameBin, pkg) pkgFile := fmt.Sprintf("%s/%s.%s", pkgDir, pkg, p.FileFormat) - if err := fileutil.WriteFile(pkgFile, []byte(p.File)); err != nil { + if err := fileutil.WriteFile(pkgFile, []byte(templatePHP)); err != nil { return err } diff --git a/pkg/formula/tpl/tpl_php/tpl_php.go b/pkg/formula/tpl/tpl_php/tpl_php.go index f3fc2abe6..712f02637 100644 --- a/pkg/formula/tpl/tpl_php/tpl_php.go +++ b/pkg/formula/tpl/tpl_php/tpl_php.go @@ -14,15 +14,19 @@ const ( ` Makefile = `# Make Run PHP -BINARY_NAME={{bin-name}}.sh +BINARY_NAME_UNIX={{bin-name}}.sh BINARY_NAME_WINDOWS={{bin-name}}.bat DIST=../dist DIST_DIR=$(DIST)/commons/bin build: mkdir -p $(DIST_DIR) - cp index.php $(DIST_DIR) && cp -r {{bin-name}} Dockerfile set_umask.sh $(DIST_DIR) - chmod +x $(DIST_DIR)/index.php - echo 'php index.php' >> $(DIST_DIR)/$(BINARY_NAME_WINDOWS)` + cp run_template $(BINARY_NAME_UNIX) && chmod +x $(BINARY_NAME_UNIX) + echo 'php index.php' >> $(DIST_DIR)/$(BINARY_NAME_WINDOWS) + + cp -r . $(DIST_DIR) + + #Clean files + rm $(BINARY_NAME_UNIX)` Dockerfile = ` FROM php:7.4-cli From c504249db9a23ce200cbc2d8ddf810df62e23f8b Mon Sep 17 00:00:00 2001 From: Antonio Oliveira Date: Thu, 25 Jun 2020 14:33:06 -0300 Subject: [PATCH 3/6] Removing uneccessary comments --- pkg/formula/lang.go | 4 ---- pkg/formula/tpl/tpl_php/tpl_php.go | 21 ++++++++++++--------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/pkg/formula/lang.go b/pkg/formula/lang.go index ec66d1ee3..cd4183753 100644 --- a/pkg/formula/lang.go +++ b/pkg/formula/lang.go @@ -208,8 +208,6 @@ func (n Node) Create(srcDir, pkg, pkgDir, dir string) error { return nil } -// ------------------------------------------------ - type PHP struct { Lang } @@ -250,8 +248,6 @@ func (p PHP) Create(srcDir, pkg, pkgDir, dir string) error { return nil } -// ------------------------------------------------ - type Shell struct { Lang } diff --git a/pkg/formula/tpl/tpl_php/tpl_php.go b/pkg/formula/tpl/tpl_php/tpl_php.go index 712f02637..c29518db1 100644 --- a/pkg/formula/tpl/tpl_php/tpl_php.go +++ b/pkg/formula/tpl/tpl_php/tpl_php.go @@ -3,12 +3,15 @@ package tpl_php const ( Index = ` ` @@ -50,10 +53,10 @@ php -f index.php File = ` ` From c4a13115a2117737b0d2546aa609cac971bfd2b7 Mon Sep 17 00:00:00 2001 From: Antonio Oliveira Date: Thu, 25 Jun 2020 16:47:16 -0300 Subject: [PATCH 4/6] Adding PHP to creator_test file --- pkg/formula/creator_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pkg/formula/creator_test.go b/pkg/formula/creator_test.go index bb3f965ac..86e2d40a2 100644 --- a/pkg/formula/creator_test.go +++ b/pkg/formula/creator_test.go @@ -14,12 +14,14 @@ const ( fCmdCorrectGo = "rit scaffold generate test-go" fCmdCorrectJava = "rit scaffold generate test-java" fCmdCorrectNode = "rit scaffold generate test-node" + fCmdCorrectPHP = "rit scaffold generate test-php" fCmdCorrectPython = "rit scaffold generate test-python" fCmdCorrectShell = "rit scaffold generate test-shell" fCmdIncorrect = "git scaffold generate testing" langGo = "Go" langJava = "Java" langNode = "Node" + langPHP = "PHP" langPython = "Python" langShell = "Shell" ) @@ -128,6 +130,16 @@ func TestCreator(t *testing.T) { err: nil, }, }, + { + name: "command correct-php", + in: &Create{ + FormulaCmd: fCmdCorrectPHP, + Lang: langPHP, + }, + out: &out{ + err: nil, + }, + }, { name: "command correct-python", in: &Create{ From 6f70803d21802595380dd810f9074bc75bbec3be Mon Sep 17 00:00:00 2001 From: Antonio Oliveira Date: Fri, 26 Jun 2020 19:18:41 -0300 Subject: [PATCH 5/6] Adding PHP to testdata/Makefile --- testdata/Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/testdata/Makefile b/testdata/Makefile index 78e51d624..795c0d2eb 100644 --- a/testdata/Makefile +++ b/testdata/Makefile @@ -5,6 +5,7 @@ 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_PHP=scaffold/coffee-php SC_COFFEE_PYTHON=scaffold/coffee-python SC_COFFEE_NODE=scaffold/coffee-node SC_SPRING_STARTER=scaffold/spring-starter @@ -13,7 +14,7 @@ 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) +FORMULAS=$(TERRAFORM) $(SC_COFFEE_GO) $(SC_COFFEE_JAVA) $(SC_COFFEE_SHELL) $(SC_COFFEE_PHP) $(SC_COFFEE_PYTHON) $(SC_COFFEE_NODE) $(SC_SPRING_STARTER) $(KAFKA) $(DOCKER) $(NAVIGATE_HANDBOOK) $(SEARCH_HANDBOOK) $(KUBERNETES) $(FAST_MERGE) PWD_INITIAL=$(shell pwd) From 66fc0e31b06af832b8496e38baeee25d21f063f8 Mon Sep 17 00:00:00 2001 From: Antonio Oliveira Date: Mon, 29 Jun 2020 11:47:45 -0300 Subject: [PATCH 6/6] Fix issue in execution with "--docker" param --- pkg/formula/tpl/tpl_php/tpl_php.go | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/pkg/formula/tpl/tpl_php/tpl_php.go b/pkg/formula/tpl/tpl_php/tpl_php.go index c29518db1..472af16e7 100644 --- a/pkg/formula/tpl/tpl_php/tpl_php.go +++ b/pkg/formula/tpl/tpl_php/tpl_php.go @@ -5,13 +5,11 @@ const ( include '{{bin-name}}/{{bin-name}}.php'; - echo 'PHP formula start\n'; +$input1 = getenv('SAMPLE_TEXT'); +$input2 = getenv('SAMPLE_LIST'); +$input3 = getenv('SAMPLE_BOOL'); - $input1 = getenv('SAMPLE_TEXT'); - $input2 = getenv('SAMPLE_LIST'); - $input3 = getenv('SAMPLE_BOOL'); - - Run($input1, $input2, $input3); +Run($input1, $input2, $input3); ?> ` @@ -32,17 +30,14 @@ build: rm $(BINARY_NAME_UNIX)` Dockerfile = ` -FROM php:7.4-cli +FROM php:latest COPY . . RUN chmod +x set_umask.sh - -WORKDIR /app - +RUN mkdir app ENTRYPOINT ["/set_umask.sh"] - -CMD ["php", "./index.php"]` +CMD ["php /index.php"]` Run = `#!/bin/sh