Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

[FEATURE] Formula creator improvements #104

Merged
merged 27 commits into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
f40c775
create mainfile and makefile
victor-schumacher May 18, 2020
e0b14d5
refactor run template and template names
victor-schumacher May 19, 2020
0da7866
extrator on pkg dir
victor-schumacher May 19, 2020
ec5b0f8
change make to use the two last words
victor-schumacher May 20, 2020
1d9c1ee
enable to save formula in another folder
victor-schumacher May 21, 2020
ab6cd7b
merge master
victor-schumacher May 21, 2020
193d129
extractor files checker
victor-schumacher May 21, 2020
ebd6308
added unit tests
victor-schumacher May 21, 2020
eaa7bc0
refactor
victor-schumacher May 22, 2020
72b9cd4
fix lint
victor-schumacher May 22, 2020
ae35145
fix lint
victor-schumacher May 22, 2020
cb299ef
remove useless todo and add the correct create on stdin
victor-schumacher May 22, 2020
02d635a
create files when custom repo is empty
victor-schumacher May 25, 2020
1a7cf5a
refactor
victor-schumacher May 26, 2020
c13679e
homedir and remove commentaries
victor-schumacher May 26, 2020
bf6f6cb
reafactor switchie case
victor-schumacher May 27, 2020
039cc7a
move function to create src
victor-schumacher May 27, 2020
db35f15
Fix create pkgDir file node
kaduartur May 27, 2020
ed87cc2
Create interface LangCreator
kaduartur May 27, 2020
87fb6ed
fixed structures
victor-schumacher May 27, 2020
3301388
not allowed chars as a const
victor-schumacher May 27, 2020
3ebeb3d
add struct at create formula
victor-schumacher May 27, 2020
8889a7c
removed unused comment
victor-schumacher May 27, 2020
56591ac
remove unused var
victor-schumacher May 27, 2020
d5316af
lang name as a const
victor-schumacher May 27, 2020
9d60170
const on lang structs
victor-schumacher May 27, 2020
e0738bc
change name to lang
victor-schumacher May 27, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/single/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,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)
Expand Down
2 changes: 1 addition & 1 deletion cmd/team/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,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)
Expand Down
72 changes: 48 additions & 24 deletions pkg/cmd/create_formula.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package cmd

import (
"errors"
"fmt"
"log"
"os"
"strings"

"github.com/spf13/cobra"

Expand All @@ -12,32 +13,32 @@ import (
"github.com/ZupIT/ritchie-cli/pkg/stdin"
)

var ErrNotAllowedCharacter = errors.New(`not allowed character on formula name \/,><@`)

const notAllowedChars = `\/><,@`

// createFormulaCmd type for add formula command
type createFormulaCmd struct {
formula.Creator
prompt.InputText
prompt.InputList
}

// createFormula type for stdin json decoder
type createFormula struct {
FormulaCmd string `json:"formulaCmd"`
Lang string `json:"lang"`
prompt.InputBool
}

// CreateFormulaCmd creates a new cmd instance
func NewCreateFormulaCmd(cf formula.Creator, it prompt.InputText, il prompt.InputList) *cobra.Command {
func NewCreateFormulaCmd(cf formula.Creator, it prompt.InputText, il prompt.InputList, ib prompt.InputBool) *cobra.Command {
c := createFormulaCmd{
cf,
it,
il,
ib,
}

cmd := &cobra.Command{
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()
Expand All @@ -47,25 +48,48 @@ func NewCreateFormulaCmd(cf formula.Creator, it prompt.InputText, il prompt.Inpu

func (c createFormulaCmd) runPrompt() 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 <noun>]", true)
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) {
return ErrNotAllowedCharacter
}

fmt.Println("Creating Formula ...")

lang, err := c.List("Choose the language: ", []string{"Go", "Java", "Node", "Python", "Shell"})
if err != nil {
return err
}
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)
localRepoDir, err = c.Text(pathQuestion, true)
if err != nil {
return err
}

}

cf := formula.Create{
FormulaCmd: fCmd,
Lang: lang,
LocalRepoDir: localRepoDir,
}

f, err := c.Create(fCmd, lang)
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
}
Expand All @@ -75,24 +99,24 @@ 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,
)
if strings.ContainsAny(cf.FormulaCmd, notAllowedChars) {
return ErrNotAllowedCharacter
}

f, err := c.Create(cf)
if err != nil {
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)
kaduartur marked this conversation as resolved.
Show resolved Hide resolved
fmt.Printf("Your formula is in %s", f.FormPath)

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/create_formula_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

func TestNewCreateFormulaCmd(t *testing.T) {
cmd := NewCreateFormulaCmd(formCreator{}, inputTextMock{}, inputListMock{})
cmd := NewCreateFormulaCmd(formCreator{}, inputTextMock{}, inputListMock{}, inputTrueMock{})
cmd.PersistentFlags().Bool("stdin", false, "input by stdin")
if cmd == nil {
t.Errorf("NewCreateFormulaCmd got %v", cmd)
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(cf formula.Create) (formula.CreateManager, error) {
return formula.CreateManager{}, nil
}

Expand Down
Loading