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

Commit

Permalink
Merge pull request #97 from sandokandias/feature/stdin
Browse files Browse the repository at this point in the history
[FEATURE] STDIN Implementation
  • Loading branch information
Marcos Guimarães authored May 20, 2020
2 parents fd266b8 + a748881 commit 06d72d9
Show file tree
Hide file tree
Showing 32 changed files with 590 additions and 72 deletions.
2 changes: 2 additions & 0 deletions cmd/single/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ func buildCommands() *cobra.Command {
inputText,
inputPassword)

rootCmd.PersistentFlags().Bool("stdin", false, "input by stdin")

// level 1
autocompleteCmd := cmd.NewAutocompleteCmd()
addCmd := cmd.NewAddCmd()
Expand Down
2 changes: 2 additions & 0 deletions cmd/team/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ func buildCommands() *cobra.Command {
inputText,
inputPassword)

rootCmd.PersistentFlags().Bool("stdin", false, "input by stdin")

// level 1
autocompleteCmd := cmd.NewAutocompleteCmd()
addCmd := cmd.NewAddCmd()
Expand Down
9 changes: 7 additions & 2 deletions functional/core_feature.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,14 @@
"action":"main"
},
{
"key":"Name of the repository:",
"key":"Choose a repository to delete:",
"value":"Leonidas",
"action":"sendkey"
"action":"select"
},
{
"key":"Want to delete",
"value":"yes",
"action":"select"
}
],
"result":"\"Leonidas\" has been removed from your repositories"
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35 // indirect
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.5
github.com/thoas/go-funk v0.6.0
golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
Expand Down
22 changes: 22 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"fmt"
"os/user"
"strings"
)

const (
Expand Down Expand Up @@ -78,6 +79,26 @@ type Formula struct {
// Edition type that represents Single or Team.
type Edition string

// TermInputType represents the source of the inputs will be readed
type TermInputType int

const (
//Prompt input
Prompt TermInputType = iota
// Stdin input
Stdin
)

func (t TermInputType) String() string {
return [...]string{"Prompt", "Stdin"}[t]
}

// ToLower converts the input type to lower case
func (t TermInputType) ToLower() string {
return strings.ToLower(t.String())
}

//UserHomeDir returns the home dir of the user
func UserHomeDir() string {
usr, err := user.Current()
if err != nil {
Expand All @@ -86,6 +107,7 @@ func UserHomeDir() string {
return usr.HomeDir
}

//RitchieHomeDir returns the home dir of the ritchie
func RitchieHomeDir() string {
return fmt.Sprintf(ritchieHomePattern, UserHomeDir())
}
34 changes: 29 additions & 5 deletions pkg/cmd/add_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package cmd

import (
"fmt"

"github.com/ZupIT/ritchie-cli/pkg/formula"
"os"

"github.com/spf13/cobra"

"github.com/ZupIT/ritchie-cli/pkg/formula"
"github.com/ZupIT/ritchie-cli/pkg/prompt"
"github.com/ZupIT/ritchie-cli/pkg/stdin"
)

// addRepoCmd type for add repo command
Expand All @@ -19,7 +20,7 @@ type addRepoCmd struct {
prompt.InputBool
}

// NewRepoAddCmd creates a new cmd instance
// NewAddRepoCmd creates a new cmd instance
func NewAddRepoCmd(
adl formula.AddLister,
it prompt.InputText,
Expand All @@ -38,13 +39,17 @@ func NewAddRepoCmd(
Use: "repo",
Short: "Add a repository.",
Example: "rit add repo ",
RunE: a.runFunc(),
//1
RunE: RunFuncE(a.runStdin(), a.runPrompt()),
}
//2
cmd.LocalFlags()

return cmd
}

func (a addRepoCmd) runFunc() CommandRunnerFunc {
//3
func (a addRepoCmd) runPrompt() CommandRunnerFunc {
return func(cmd *cobra.Command, args []string) error {
rn, err := a.Text("Name of the repository: ", true)
if err != nil {
Expand Down Expand Up @@ -88,5 +93,24 @@ func (a addRepoCmd) runFunc() CommandRunnerFunc {

return err
}
}

//4
func (a addRepoCmd) runStdin() CommandRunnerFunc {
return func(cmd *cobra.Command, args []string) error {

r := formula.Repository{}

err := stdin.ReadJson(os.Stdin, &r)
if err != nil {
fmt.Println("The STDIN inputs weren't informed correctly. Check the JSON used to execute the command.")
return err
}

if err := a.Add(r); err != nil {
return err
}

return nil
}
}
2 changes: 2 additions & 0 deletions pkg/cmd/add_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

func TestNewAddRepoCmd(t *testing.T) {
cmd := NewAddRepoCmd(repoAdder{}, inputTextMock{}, inputURLMock{}, inputIntMock{}, inputTrueMock{})
cmd.PersistentFlags().Bool("stdin", false, "input by stdin")
if cmd == nil {
t.Errorf("NewAddRepoCmd got %v", cmd)

Expand All @@ -14,4 +15,5 @@ func TestNewAddRepoCmd(t *testing.T) {
if err := cmd.Execute(); err != nil {
t.Errorf("%s = %v, want %v", cmd.Use, err, nil)
}

}
35 changes: 33 additions & 2 deletions pkg/cmd/clean_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package cmd

import (
"fmt"
"os"

"github.com/ZupIT/ritchie-cli/pkg/formula"
"github.com/ZupIT/ritchie-cli/pkg/stdin"

"github.com/spf13/cobra"

Expand All @@ -15,6 +18,11 @@ type cleanRepoCmd struct {
prompt.InputText
}

// cleanRepo type for stdin json decoder
type cleanRepo struct {
Name string `json:"name"`
}

// NewCleanRepoCmd creates a new cmd instance
func NewCleanRepoCmd(cl formula.Cleaner, it prompt.InputText) *cobra.Command {
c := &cleanRepoCmd{cl, it}
Expand All @@ -23,13 +31,15 @@ func NewCleanRepoCmd(cl formula.Cleaner, it prompt.InputText) *cobra.Command {
Use: "repo",
Short: "clean a repository.",
Example: "rit clean repo ",
RunE: c.runFunc(),
RunE: RunFuncE(c.runStdin(), c.runPrompt()),
}

cmd.LocalFlags()

return cmd
}

func (c cleanRepoCmd) runFunc() CommandRunnerFunc {
func (c cleanRepoCmd) runPrompt() CommandRunnerFunc {
return func(cmd *cobra.Command, args []string) error {
n, err := c.Text("Name of the repository: ", true)
if err != nil {
Expand All @@ -45,3 +55,24 @@ func (c cleanRepoCmd) runFunc() CommandRunnerFunc {
return nil
}
}

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

f := cleanRepo{}

err := stdin.ReadJson(os.Stdin, &f)
if err != nil {
fmt.Println("The STDIN inputs weren't informed correctly. Check the JSON used to execute the command.")
return err
}

if err := c.Clean(f.Name); err != nil {
return err
}

fmt.Printf("%q has been cleaned successfully\n", f.Name)

return nil
}
}
1 change: 1 addition & 0 deletions pkg/cmd/clean_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

func TestNewCleanRepoCmd(t *testing.T) {
cmd := NewCleanRepoCmd(repoCleaner{}, inputTextMock{})
cmd.PersistentFlags().Bool("stdin", false, "input by stdin")
if cmd == nil {
t.Errorf("NewCleanRepoCmd got %v", cmd)

Expand Down
21 changes: 20 additions & 1 deletion pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
package cmd

import "github.com/spf13/cobra"
import (
"github.com/ZupIT/ritchie-cli/pkg/api"
"github.com/spf13/cobra"
)

// CommandRunnerFunc represents that runner func for commands
type CommandRunnerFunc func(cmd *cobra.Command, args []string) error

// RunFuncE delegates to stdinFunc if --stdin flag is passed otherwise delegates to promptFunc
func RunFuncE(stdinFunc, promptFunc CommandRunnerFunc) CommandRunnerFunc {
return func(cmd *cobra.Command, args []string) error {
stdin, err := cmd.Flags().GetBool(api.Stdin.ToLower())
if err != nil {
return err
}

if stdin {
return stdinFunc(cmd, args)
}
return promptFunc(cmd, args)
}
}
57 changes: 53 additions & 4 deletions pkg/cmd/create_formula.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package cmd
import (
"fmt"
"log"
"os"

"github.com/spf13/cobra"

"github.com/ZupIT/ritchie-cli/pkg/formula"
"github.com/ZupIT/ritchie-cli/pkg/prompt"
"github.com/ZupIT/ritchie-cli/pkg/stdin"
)

// createFormulaCmd type for add formula command
Expand All @@ -17,34 +19,81 @@ type createFormulaCmd struct {
prompt.InputList
}

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

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

cmd := &cobra.Command{
Use: "formula",
Short: "Create a new formula",
Example: "rit create formula",
RunE: c.runFunc(),
RunE: RunFuncE(c.runStdin(), c.runPrompt()),
}

cmd.LocalFlags()

return cmd
}

func (c createFormulaCmd) runFunc() CommandRunnerFunc {
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)
if err != nil {
return err
}

lang, err := c.List("Choose the language: ", []string{"Go", "Java", "Node", "Python", "Shell"})
if err != nil {
return err
}

f, err := c.Create(fCmd, lang)
if err != nil {
return err
}

log.Printf("Formula in %s successfully created!\n", lang)
log.Printf("Your formula is in %s", f.FormPath)

return nil
}
}

func (c createFormulaCmd) runStdin() CommandRunnerFunc {
return func(cmd *cobra.Command, args []string) error {
fmt.Println("Creating Formula ...")

cf := createFormula{}

err := stdin.ReadJson(os.Stdin, &cf)
if 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 err != nil {
return err
}

log.Printf("Formula in %s successfully created!\n", cf.Lang)
log.Printf("Your formula is in %s", f.FormPath)

return nil
}
}
1 change: 1 addition & 0 deletions pkg/cmd/create_formula_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

func TestNewCreateFormulaCmd(t *testing.T) {
cmd := NewCreateFormulaCmd(formCreator{}, inputTextMock{}, inputListMock{})
cmd.PersistentFlags().Bool("stdin", false, "input by stdin")
if cmd == nil {
t.Errorf("NewCreateFormulaCmd got %v", cmd)

Expand Down
Loading

0 comments on commit 06d72d9

Please sign in to comment.