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

Create journey tips #284

Closed
wants to merge 9 commits into from
2 changes: 2 additions & 0 deletions cmd/single/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func buildCommands() *cobra.Command {
updateCmd := cmd.NewUpdateCmd()
buildCmd := cmd.NewBuildCmd()
upgradeCmd := cmd.NewUpgradeCmd(api.Single, defaultUpgradeResolver, upgradeManager, defaultUrlFinder)
tutorialCmd := cmd.NewTutorialCmd(ritchieHomeDir, inputBool)

// level 2
setCredentialCmd := cmd.NewSingleSetCredentialCmd(
Expand Down Expand Up @@ -178,6 +179,7 @@ func buildCommands() *cobra.Command {
updateCmd,
buildCmd,
upgradeCmd,
tutorialCmd,
},
},
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var (
{Parent: "root", Usage: "build"},
{Parent: "root_build", Usage: "formula"},
{Parent: "root", Usage: "upgrade"},
{Parent: "root", Usage: "tutorial"},
}

SingleCoreCmds = CoreCmds
Expand All @@ -59,11 +60,11 @@ var (

// Command type
type Command struct {
Parent string `json:"parent"`
Usage string `json:"usage"`
Help string `json:"help"`
Parent string `json:"parent"`
Usage string `json:"usage"`
Help string `json:"help"`
Formula *Formula `json:"formula,omitempty"`
Repo string `json:"Repo,omitempty"`
Repo string `json:"Repo,omitempty"`
}

type Commands []Command
Expand Down
20 changes: 19 additions & 1 deletion pkg/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import (
"fmt"
"os"

"github.com/ZupIT/ritchie-cli/pkg/api"
"github.com/ZupIT/ritchie-cli/pkg/formula"
"github.com/ZupIT/ritchie-cli/pkg/security/otp"
"github.com/ZupIT/ritchie-cli/pkg/validator"

"github.com/spf13/cobra"

"github.com/ZupIT/ritchie-cli/pkg/prompt"
"github.com/ZupIT/ritchie-cli/pkg/security"
"github.com/ZupIT/ritchie-cli/pkg/server"
"github.com/ZupIT/ritchie-cli/pkg/stdin"
"github.com/ZupIT/ritchie-cli/pkg/validator"
)

const (
Expand Down Expand Up @@ -92,6 +93,8 @@ func (o initSingleCmd) runPrompt() CommandRunnerFunc {
return err
}

tutorialInit()

return o.Load()
}
}
Expand All @@ -114,6 +117,8 @@ func (o initSingleCmd) runStdin() CommandRunnerFunc {
return err
}

tutorialInit()

return o.Load()
}
}
Expand Down Expand Up @@ -211,6 +216,8 @@ func (o initTeamCmd) runPrompt() CommandRunnerFunc {
fmt.Println("Login successfully!")
}

tutorialInit()

return nil
}
}
Expand All @@ -232,3 +239,14 @@ func (o initTeamCmd) runStdin() CommandRunnerFunc {
return nil
}
}

func tutorialInit() {
homePath := api.RitchieHomeDir()
pathTutorial := fmt.Sprintf(TutorialFilePattern, homePath)

tutorialStatus, _ := currentTutorial(pathTutorial)

if tutorialStatus == tutorialStatusOn {
prompt.Info("\n[TUTORIAL] The next step is \"rit set context\"")
}
}
126 changes: 126 additions & 0 deletions pkg/cmd/tutorial.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package cmd

import (
"fmt"
"os"

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

type tutorialSingleCmd struct {
homePath string
prompt.InputBool
}

const (
tutorialStatusOn = "on"
tutorialStatusOff = "off"
TutorialFilePattern = "%s/tutorial"
)

// NewTutorialCmd creates tutorial command
func NewTutorialCmd(homePath string, ib prompt.InputBool) *cobra.Command {
o := tutorialSingleCmd{homePath, ib}

cmd := &cobra.Command{
Use: "tutorial",
Short: "Turns the tutorial on or off",
Long: "Turns the tutorial on or off",
RunE: RunFuncE(o.runStdin(), o.runPrompt()),
}

cmd.LocalFlags()

return cmd
}

func (o tutorialSingleCmd) runStdin() CommandRunnerFunc {
return func(cmd *cobra.Command, args []string) error {

obj := struct {
Tutorial string `json:"tutorial"`
}{}

err := stdin.ReadJson(os.Stdin, &obj)
if err != nil {
fmt.Println(stdin.MsgInvalidInput)
return err
}

fmt.Println(obj)

return nil
}
}

func (o tutorialSingleCmd) runPrompt() CommandRunnerFunc {
return func(cmd *cobra.Command, args []string) error {
pathTutorial := fmt.Sprintf(TutorialFilePattern, o.homePath)
msg := "Enable tutorial?"
tutorialStatus, _ := currentTutorial(pathTutorial)
tutorialEnabled := tutorialStatus == tutorialStatusOn
fmt.Println("STATUS TUTORIAL: ", tutorialStatus)

if tutorialEnabled {
msg = "Disable tutorial?"
}

y, err := o.Bool(msg, []string{"yes", "no"})
if err != nil {
return err
}

if y {
invertsTutorialStatus(pathTutorial, tutorialStatus)
}

fmt.Println("TUDO OK! SUA RESPOSTA: ", y)
tutorialStatus, _ = currentTutorial(pathTutorial)
fmt.Println("STATUS TUTORIAL: ", tutorialStatus)
return nil
}
}

func currentTutorial(path string) (string, error) {
currentStatus := tutorialStatusOn

if fileutil.Exists(path) {
status, err := fileutil.ReadFile(path)
if err != nil {
return tutorialStatusOn, err
}
currentStatus = string(status)
} else {
err := createTutorial(path)
if err != nil {
return tutorialStatusOn, err
}
}

return string(currentStatus), nil
}

func createTutorial(path string) error {
if err := fileutil.WriteFile(path, []byte(tutorialStatusOn)); err != nil {
return err
}

return nil
}

func invertsTutorialStatus(path string, currentStatus string) error {
nextStatus := tutorialStatusOn

if currentStatus == tutorialStatusOn {
nextStatus = tutorialStatusOff
}

if err := fileutil.WriteFile(path, []byte(nextStatus)); err != nil {
return err
}

return nil
}
19 changes: 19 additions & 0 deletions pkg/cmd/tutorial_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cmd

import (
"os"
"testing"
)

func TestNewTutorialCmd(t *testing.T) {
cmd := NewTutorialCmd(os.TempDir(), inputFalseMock{})
cmd.PersistentFlags().Bool("stdin", false, "input by stdin")

if cmd == nil {
t.Errorf("NewTutorialCmd got %v", cmd)
}

if err := cmd.Execute(); err != nil {
t.Errorf("%s = %v, want %v", cmd.Use, err, nil)
}
}