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

Commit

Permalink
Improve performance to "rit create formula" command (#805)
Browse files Browse the repository at this point in the history
* Fix create formula msg

Signed-off-by: Kadu Artur Prussek <[email protected]>

* Improve create formula tests

Signed-off-by: Kadu Artur Prussek <[email protected]>

* Improve create formula tests

Signed-off-by: Kadu Artur Prussek <[email protected]>

* Fix functional tests

Signed-off-by: Kadu Artur Prussek <[email protected]>
  • Loading branch information
kaduartur authored Dec 21, 2020
1 parent 13b1f44 commit 84b0008
Show file tree
Hide file tree
Showing 4 changed files with 489 additions and 139 deletions.
6 changes: 3 additions & 3 deletions functional/stdin/stdin_feature.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
"action": "main"
}
],
"result": "Rit formula's command needs at least 2 words"
"result": "rit formula's command needs at least 2 words following \"rit\" [ex.: rit group verb]"
},
{
"entry": "Create formula STDIN - no rit on formula name",
Expand All @@ -157,7 +157,7 @@
"action": "main"
}
],
"result": "Rit formula's command needs to start with"
"result": "rit formula's command needs to start with \"rit\" [ex.: rit group verb <noun>]"
},
{
"entry": "Create formula STDIN - not allowed char",
Expand All @@ -173,7 +173,7 @@
"action": "main"
}
],
"result": "not allowed character on formula name"
"result": "these characters are not allowed in the formula command [\\ /,> <@ -]"
},
{
"entry": "Create formula STDIN - core command after rit",
Expand Down
87 changes: 87 additions & 0 deletions internal/mocks/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/ZupIT/ritchie-cli/pkg/api"
"github.com/ZupIT/ritchie-cli/pkg/env"
"github.com/ZupIT/ritchie-cli/pkg/formula"
"github.com/ZupIT/ritchie-cli/pkg/formula/creator/template"
"github.com/ZupIT/ritchie-cli/pkg/git"
"github.com/ZupIT/ritchie-cli/pkg/rtutorial"
)
Expand Down Expand Up @@ -136,6 +137,15 @@ func (i *InputPasswordMock) Password(label string, helper ...string) (string, er
return args.String(0), args.Error(1)
}

type InputTextMock struct {
mock.Mock
}

func (i *InputTextMock) Text(name string, required bool, helper ...string) (string, error) {
args := i.Called(name, required, helper)
return args.String(0), args.Error(1)
}

type InputTextValidatorMock struct {
mock.Mock
}
Expand All @@ -146,6 +156,59 @@ func (i *InputTextValidatorMock) Text(name string, validate func(interface{}) er
return args.String(0), args.Error(1)
}

type FormCreator struct {
mock.Mock
}

func (f *FormCreator) Create(cf formula.Create) error {
args := f.Called(cf)
return args.Error(0)
}

func (f *FormCreator) Build(info formula.BuildInfo) error {
args := f.Called(info)
return args.Error(0)
}

type WorkspaceForm struct {
mock.Mock
}

func (w *WorkspaceForm) Add(workspace formula.Workspace) error {
args := w.Called(workspace)
return args.Error(0)
}

func (w *WorkspaceForm) Delete(workspace formula.Workspace) error {
args := w.Called(workspace)
return args.Error(0)
}

func (w *WorkspaceForm) List() (formula.Workspaces, error) {
args := w.Called()
return args.Get(0).(formula.Workspaces), args.Error(1)
}

func (w *WorkspaceForm) Validate(workspace formula.Workspace) error {
args := w.Called(workspace)
return args.Error(0)
}

func (w *WorkspaceForm) CurrentHash(formulaPath string) (string, error) {
args := w.Called(formulaPath)
return args.String(0), args.Error(1)
}

func (w *WorkspaceForm) PreviousHash(formulaPath string) (string, error) {
args := w.Called(formulaPath)
return args.String(0), args.Error(1)
}

func (w *WorkspaceForm) UpdateHash(formulaPath string, hash string) error {
args := w.Called(formulaPath, hash)
return args.Error(0)
}

type RepoManager struct {
mock.Mock
}
Expand Down Expand Up @@ -267,3 +330,27 @@ func (t *TreeManager) Check() []api.CommandID {
args := t.Called()
return args.Get(0).([]api.CommandID)
}

type TemplateManagerMock struct {
mock.Mock
}

func (tm *TemplateManagerMock) Languages() ([]string, error) {
args := tm.Called()
return args.Get(0).([]string), args.Error(1)
}

func (tm *TemplateManagerMock) LangTemplateFiles(lang string) ([]template.File, error) {
args := tm.Called(lang)
return args.Get(0).([]template.File), args.Error(1)
}

func (tm *TemplateManagerMock) ResolverNewPath(oldPath, newDir, lang, workspacePath string) (string, error) {
args := tm.Called(oldPath, newDir, lang, workspacePath)
return args.String(0), args.Error(1)
}

func (tm *TemplateManagerMock) Validate() error {
args := tm.Called()
return args.Error(0)
}
101 changes: 54 additions & 47 deletions pkg/cmd/create_formula.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,46 +22,54 @@ import (
"os"
"path/filepath"
"strings"
"time"

"github.com/kaduartur/go-cli-spinner/pkg/spinner"
"github.com/spf13/cobra"

"github.com/ZupIT/ritchie-cli/pkg/api"
"github.com/ZupIT/ritchie-cli/pkg/formula"
"github.com/ZupIT/ritchie-cli/pkg/formula/creator/template"
"github.com/ZupIT/ritchie-cli/pkg/formula/tree"
"github.com/ZupIT/ritchie-cli/pkg/prompt"
"github.com/ZupIT/ritchie-cli/pkg/rtutorial"
"github.com/ZupIT/ritchie-cli/pkg/stdin"
)

const newWorkspace = "Type new formula workspace?"
const (
newWorkspace = "Type new formula workspace?"
formulaCmdLabel = "Enter the new formula command: "
formulaCmdHelper = "You must create your command based in this example [rit group verb noun]"
)

var (
ErrFormulaCmdNotBeEmpty = errors.New("this input must not be empty")
ErrFormulaCmdMustStartWithRit = errors.New("rit formula's command needs to start with \"rit\" [ex.: rit group verb <noun>]")
ErrInvalidFormulaCmdSize = errors.New("rit formula's command needs at least 2 words following \"rit\" [ex.: rit group verb]")
ErrInvalidCharactersFormulaCmd = errors.New(`these characters are not allowed in the formula command [\ /,> <@ -]`)
)

// createFormulaCmd type for add formula command.
type createFormulaCmd struct {
homeDir string
formula formula.CreateBuilder
workspace formula.WorkspaceAddLister
workspace formula.WorkspaceAddListHasher
inText prompt.InputText
inTextValidator prompt.InputTextValidator
inList prompt.InputList
tplM template.Manager
template template.Manager
tutorial rtutorial.Finder
tree tree.CheckerManager
tree formula.TreeChecker
}

// CreateFormulaCmd creates a new cmd instance.
func NewCreateFormulaCmd(
homeDir string,
formula formula.CreateBuilder,
tplM template.Manager,
workspace formula.WorkspaceAddLister,
workspace formula.WorkspaceAddListHasher,
inText prompt.InputText,
inTextValidator prompt.InputTextValidator,
inList prompt.InputList,
rtf rtutorial.Finder,
treeChecker tree.CheckerManager,
treeChecker formula.TreeChecker,
) *cobra.Command {
c := createFormulaCmd{
homeDir: homeDir,
Expand All @@ -70,7 +78,7 @@ func NewCreateFormulaCmd(
inText: inText,
inTextValidator: inTextValidator,
inList: inList,
tplM: tplM,
template: tplM,
tutorial: rtf,
tree: treeChecker,
}
Expand All @@ -91,20 +99,16 @@ func NewCreateFormulaCmd(

func (c createFormulaCmd) runPrompt() CommandRunnerFunc {
return func(cmd *cobra.Command, args []string) error {
formulaCmd, err := c.inTextValidator.Text(
"Enter the new formula command: ",
c.surveyCmdValidator,
"You must create your command based in this example [rit group verb noun]",
)
formulaCmd, err := c.inTextValidator.Text(formulaCmdLabel, c.surveyCmdValidator, formulaCmdHelper)
if err != nil {
return err
}

if err := c.tplM.Validate(); err != nil {
if err := c.template.Validate(); err != nil {
return err
}

languages, err := c.tplM.Languages()
languages, err := c.template.Languages()
if err != nil {
return err
}
Expand Down Expand Up @@ -138,9 +142,12 @@ func (c createFormulaCmd) runPrompt() CommandRunnerFunc {
}

check := c.tree.Check()

printConflictingCommandsWarning(check)

c.create(cf)
if err := c.create(cf); err != nil {
return err
}

return nil
}
Expand All @@ -159,52 +166,55 @@ func (c createFormulaCmd) runStdin() CommandRunnerFunc {
return err
}

c.create(cf)
if err := c.create(cf); err != nil {
return err
}

return nil
}
}

func (c createFormulaCmd) create(cf formula.Create) {
buildInfo := prompt.Bold("Creating and building formula...")
s := spinner.StartNew(buildInfo)
time.Sleep(2 * time.Second)

func (c createFormulaCmd) create(cf formula.Create) error {
if err := c.formula.Create(cf); err != nil {
err := prompt.NewError(err.Error())
s.Error(err)
return
return err
}

info := formula.BuildInfo{FormulaPath: cf.FormulaPath, Workspace: cf.Workspace}
if err := c.formula.Build(info); err != nil {
err := prompt.NewError(err.Error())
s.Error(err)
return
return err
}

hash, err := c.workspace.CurrentHash(cf.FormulaPath)
if err != nil {
return err
}

if err := c.workspace.UpdateHash(cf.FormulaPath, hash); err != nil {
return err
}

successMsg := fmt.Sprintf("%s formula successfully created!", cf.Lang)
prompt.Success(successMsg)

tutorialHolder, err := c.tutorial.Find()
if err != nil {
s.Error(err)
return
return err
}
createSuccess(s, cf.Lang)

buildSuccess(cf.FormulaPath, cf.FormulaCmd, tutorialHolder.Current)
}

func createSuccess(s *spinner.Spinner, lang string) {
msg := fmt.Sprintf("%s formula successfully created!", lang)
success := prompt.Green(msg)
s.Success(success)
return nil
}

func buildSuccess(formulaPath, formulaCmd, tutorialStatus string) {
prompt.Info(fmt.Sprintf("Formula path is %s", formulaPath))

if tutorialStatus == tutorialStatusEnabled {
tutorialCreateFormula(formulaCmd)
} else {
prompt.Info(fmt.Sprintf("Now you can run your formula with the following command %q", formulaCmd))
return
}

prompt.Info(fmt.Sprintf("Now you can run your formula with the following command %q", formulaCmd))
}

func formulaPath(workspacePath, cmd string) string {
Expand All @@ -223,19 +233,16 @@ func (c createFormulaCmd) surveyCmdValidator(cmd interface{}) error {

func formulaCommandValidator(formulaCmd string) error {
if len(strings.TrimSpace(formulaCmd)) < 1 {
return prompt.
NewError("this input must not be empty")
return ErrFormulaCmdNotBeEmpty
}

s := strings.Split(formulaCmd, " ")
if s[0] != "rit" {
return prompt.
NewError("Rit formula's command needs to start with \"rit\" [ex.: rit group verb <noun>]")
return ErrFormulaCmdMustStartWithRit
}

if len(s) <= 2 {
return prompt.
NewError("Rit formula's command needs at least 2 words following \"rit\" [ex.: rit group verb]")
return ErrInvalidFormulaCmdSize
}

if err := characterValidator(formulaCmd); err != nil {
Expand Down Expand Up @@ -266,7 +273,7 @@ func coreCmdValidator(formulaCmd string) error {

func characterValidator(formula string) error {
if strings.ContainsAny(formula, `\/><,@`) {
return prompt.NewError(`not allowed character on formula name \/,><@-`)
return ErrInvalidCharactersFormulaCmd
}
return nil
}
Expand Down
Loading

0 comments on commit 84b0008

Please sign in to comment.