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

Commit

Permalink
Tutorial flags (#846)
Browse files Browse the repository at this point in the history
* Rewrite tutorial file

Signed-off-by: Henrique Moraes <[email protected]>

* Tutorial flags implementation

Signed-off-by: Henrique Moraes <[email protected]>

* Review corrections

Signed-off-by: Henrique Moraes <[email protected]>
  • Loading branch information
henriquemoraeszup authored Feb 9, 2021
1 parent ba97781 commit aafbea6
Show file tree
Hide file tree
Showing 10 changed files with 209 additions and 328 deletions.
2 changes: 1 addition & 1 deletion pkg/cmd/create_formula_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ func createFormulaCmdDeps(ritchieHomeDir string, dirManager stream.DirManager, f
createBuilder := formula.NewCreateBuilder(createManager, formBuildLocal)
buildLocal := builder.NewBuildLocal(ritchieHomeDir, dirManager, repoAdder)
wspaceManager := workspace.New(ritchieHomeDir, os.TempDir(), dirManager, buildLocal)
tutorialFinder := rtutorial.NewFinder(ritchieHomeDir, fileManager)
tutorialFinder := rtutorial.NewFinder(ritchieHomeDir)

return createFormulaCmd{
formula: createBuilder,
Expand Down
97 changes: 67 additions & 30 deletions pkg/cmd/tutorial.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,86 +17,123 @@
package cmd

import (
"errors"
"fmt"
"reflect"

"github.com/spf13/cobra"

"github.com/ZupIT/ritchie-cli/pkg/prompt"
"github.com/ZupIT/ritchie-cli/pkg/rtutorial"
"github.com/ZupIT/ritchie-cli/pkg/stdin"
"github.com/spf13/cobra"
)

type tutorialCmd struct {
homePath string
prompt.InputList
tutorial rtutorial.FindSetter
}

const (
tutorialStatusEnabled = "enabled"
tutorialStatusDisabled = "disabled"
enabledFlagName = "enabled"
)

var tutorialFlags = flags{
{
name: enabledFlagName,
kind: reflect.Bool,
defValue: false,
description: "enable the tutorial",
},
}

// NewTutorialCmd creates tutorial command.
func NewTutorialCmd(homePath string, il prompt.InputList, fs rtutorial.FindSetter) *cobra.Command {
o := tutorialCmd{homePath, il, fs}
func NewTutorialCmd(il prompt.InputList, tt rtutorial.FindSetter) *cobra.Command {
o := tutorialCmd{il, tt}

cmd := &cobra.Command{
Use: "tutorial",
Short: "Enable or disable the tutorial",
Long: "Enable or disable the tutorial",
RunE: RunFuncE(o.runStdin(), o.runPrompt()),
RunE: RunFuncE(o.runStdin(), o.runFormula()),
ValidArgs: []string{""},
Args: cobra.OnlyValidArgs,
}

cmd.LocalFlags()
addReservedFlags(cmd.Flags(), tutorialFlags)

return cmd
}

func (o tutorialCmd) runStdin() CommandRunnerFunc {
func (t *tutorialCmd) runFormula() CommandRunnerFunc {
return func(cmd *cobra.Command, args []string) error {
obj := struct {
Tutorial string `json:"tutorial"`
}{}

if err := stdin.ReadJson(cmd.InOrStdin(), &obj); err != nil {
enabled, err := t.resolveInput(cmd)
if err != nil {
return err
}

if _, err := o.tutorial.Set(obj.Tutorial); err != nil {
_, err = t.tutorial.Set(enabled)
if err != nil {
return err
}
prompt.Success("Tutorial " + enabled + "!")
return nil
}
}

prompt.Success("Tutorial " + obj.Tutorial + "!")
func (t *tutorialCmd) resolveInput(cmd *cobra.Command) (string, error) {
if IsFlagInput(cmd) {
return t.resolveFlags(cmd)
}
return t.resolvePrompt()
}

return nil
func (t *tutorialCmd) resolveFlags(cmd *cobra.Command) (string, error) {
enabled, err := cmd.Flags().GetBool(enabledFlagName)
if err != nil {
return "", errors.New(missingFlagText(enabledFlagName))
} else if enabled {
return tutorialStatusEnabled, nil
}
return tutorialStatusDisabled, nil
}

func (o tutorialCmd) runPrompt() CommandRunnerFunc {
return func(cmd *cobra.Command, args []string) error {
msg := "Status tutorial?"
var statusTypes = []string{tutorialStatusEnabled, tutorialStatusDisabled}
func (t *tutorialCmd) resolvePrompt() (string, error) {
msg := "Status tutorial?"
var statusTypes = []string{tutorialStatusEnabled, tutorialStatusDisabled}

tutorialHolder, err := o.tutorial.Find()
if err != nil {
return err
}
tutorialHolder, err := t.tutorial.Find()
if err != nil {
return "", err
}

tutorialStatusCurrent := tutorialHolder.Current
fmt.Println("Current tutorial status: ", tutorialStatusCurrent)
tutorialStatusCurrent := tutorialHolder.Current
fmt.Println("Current tutorial status: ", tutorialStatusCurrent)

response, err := o.List(msg, statusTypes)
if err != nil {
response, err := t.List(msg, statusTypes)
if err != nil {
return "", err
}
return response, nil
}

func (t *tutorialCmd) runStdin() CommandRunnerFunc {
return func(cmd *cobra.Command, args []string) error {
obj := struct {
Tutorial string `json:"tutorial"`
}{}

if err := stdin.ReadJson(cmd.InOrStdin(), &obj); err != nil {
return err
}

_, err = o.tutorial.Set(response)
if err != nil {
if _, err := t.tutorial.Set(obj.Tutorial); err != nil {
return err
}
prompt.Success("Tutorial " + response + "!")

prompt.Success("Tutorial " + obj.Tutorial + "!")

return nil
}
}
Loading

0 comments on commit aafbea6

Please sign in to comment.