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

Clean formulas command #288

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
4 changes: 4 additions & 0 deletions cmd/single/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func buildCommands() *cobra.Command {
addCmd := cmd.NewAddCmd()
createCmd := cmd.NewCreateCmd()
deleteCmd := cmd.NewDeleteCmd()
cleanCmd := cmd.NewCleanCmd()
initCmd := cmd.NewSingleInitCmd(inputPassword, passphraseManager, repoLoader)
listCmd := cmd.NewListCmd()
setCmd := cmd.NewSetCmd()
Expand Down Expand Up @@ -146,11 +147,13 @@ func buildCommands() *cobra.Command {

createFormulaCmd := cmd.NewCreateFormulaCmd(userHomeDir, createBuilder, formulaWorkspace, inputText, inputTextValidator, inputList)
buildFormulaCmd := cmd.NewBuildFormulaCmd(userHomeDir, formulaBuilder, formulaWorkspace, watchManager, dirManager, inputText, inputList)
cleanFormulasCmd := cmd.NewCleanFormulasCmd()

autocompleteCmd.AddCommand(autocompleteZsh, autocompleteBash, autocompleteFish, autocompletePowerShell)
addCmd.AddCommand(addRepoCmd)
createCmd.AddCommand(createFormulaCmd)
deleteCmd.AddCommand(deleteRepoCmd, deleteCtxCmd)
cleanCmd.AddCommand(cleanFormulasCmd)
listCmd.AddCommand(listRepoCmd)
setCmd.AddCommand(setCredentialCmd, setCtxCmd)
showCmd.AddCommand(showCtxCmd)
Expand All @@ -170,6 +173,7 @@ func buildCommands() *cobra.Command {
autocompleteCmd,
createCmd,
deleteCmd,
cleanCmd,
initCmd,
listCmd,
setCmd,
Expand Down
4 changes: 4 additions & 0 deletions cmd/team/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func buildCommands() *cobra.Command {
addCmd := cmd.NewAddCmd()
createCmd := cmd.NewCreateCmd()
deleteCmd := cmd.NewDeleteCmd()
cleanCmd := cmd.NewCleanCmd()
initCmd := cmd.NewTeamInitCmd(
inputText,
inputPassword,
Expand Down Expand Up @@ -182,11 +183,13 @@ func buildCommands() *cobra.Command {

createFormulaCmd := cmd.NewCreateFormulaCmd(userHomeDir, createBuilder, formulaWorkspace, inputText, inputTextValidator, inputList)
buildFormulaCmd := cmd.NewBuildFormulaCmd(userHomeDir, formulaBuilder, formulaWorkspace, watchManager, dirManager, inputText, inputList)
cleanFormulasCmd := cmd.NewCleanFormulasCmd()

autocompleteCmd.AddCommand(autocompleteZsh, autocompleteBash, autocompleteFish, autocompletePowerShell)
addCmd.AddCommand(addRepoCmd)
createCmd.AddCommand(createFormulaCmd)
deleteCmd.AddCommand(deleteRepoCmd, deleteCtxCmd)
cleanCmd.AddCommand(cleanFormulasCmd)
listCmd.AddCommand(listRepoCmd)
setCmd.AddCommand(setCredentialCmd, setCtxCmd)
showCmd.AddCommand(showCtxCmd)
Expand All @@ -206,6 +209,7 @@ func buildCommands() *cobra.Command {
autocompleteCmd,
createCmd,
deleteCmd,
cleanCmd,
initCmd,
listCmd,
loginCmd,
Expand Down
21 changes: 21 additions & 0 deletions pkg/cmd/clean.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cmd

import (
"github.com/spf13/cobra"
)

var descCleanLong = `
This command consists of multiple subcommands to interact with ritchie.

It can be used to clean formulas from your current ritchie build
`
victor-schumacher marked this conversation as resolved.
Show resolved Hide resolved

// NewCleanCmd create a new clean instance
func NewCleanCmd() *cobra.Command {
return &cobra.Command{
Use: "clean SUBCOMMAND",
Short: "Clean properties",
Long: descCleanLong,
}
}

89 changes: 89 additions & 0 deletions pkg/cmd/clean_formulas.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package cmd

import (
"fmt"
"os"

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

"github.com/spf13/cobra"

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

// cleanFormulasCmd type for clean formulas command
type cleanFormulasCmd struct {
prompt.InputBool
}

// cleanFormulas type for stdin json decoder
type cleanFormulasStdin struct {
Confirm bool `json:"confirm"`
}

// NewCleanFormulasCmd cleans formulas in .rit/formulas file
func NewCleanFormulasCmd() *cobra.Command {
cleanCmd := &cleanFormulasCmd{
prompt.NewSurveyBool(),
}

cmd := &cobra.Command{
Use: "formulas",
Short: "Cleans all downloaded formulas",
Example: "rit clean formulas",
RunE: RunFuncE(cleanCmd.runStdin(), cleanCmd.runPrompt()),
}

cmd.LocalFlags()

return cmd
}

func (cleanFormula cleanFormulasCmd) runPrompt() CommandRunnerFunc {
return func(cmd *cobra.Command, args []string) error {

choice, _ := cleanFormula.Bool(
fmt.Sprint("Are you sure you want to clean all your downloaded formulas?"),
[]string{"yes", "no"},
)
if !choice {
fmt.Println("Operation cancelled")
return nil
}

formulasDirectory := fmt.Sprintf("%s/formulas", api.RitchieHomeDir())
if err := os.RemoveAll(formulasDirectory); err != nil {
return err
}

prompt.Success(fmt.Sprint("Your formulas folder has been cleaned!\n"))
return nil
}
}

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

stdinArgs := cleanFormulasStdin{}

err := stdin.ReadJson(os.Stdin, &stdinArgs)
if err != nil {
prompt.Error(stdin.MsgInvalidInput)
return err
}

if !stdinArgs.Confirm {
fmt.Println("Operation cancelled")
return nil
}

formulasDirectory := fmt.Sprintf("%s/formulas", api.RitchieHomeDir())
if err := os.RemoveAll(formulasDirectory); err != nil {
return err
}

prompt.Success(fmt.Sprint("Your formulas folder has been cleaned!\n"))
return nil
}
}
50 changes: 50 additions & 0 deletions pkg/cmd/clean_formulas_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package cmd

import (
"os"
"testing"

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

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

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

func TestNewCleanFormulasCmdStdin(t *testing.T) {
t.Run("confirm", func(t *testing.T) {
runStdinCommand(t, `{"confirm": true}`)
})

t.Run("not confirm", func(t *testing.T) {
runStdinCommand(t, `{"confirm": false}`)
})
}

func runStdinCommand(t *testing.T, content string) {
tmpfile, oldStdin, err := stdin.WriteToStdin(content)
defer os.Remove(tmpfile.Name())
defer func() { os.Stdin = oldStdin }()
if err != nil {
t.Errorf("TestNewCleanFormulasCmdStdin got error %v", err)
}

cmd := NewCleanFormulasCmd()
cmd.PersistentFlags().Bool("stdin", true, "input by stdin")
if cmd == nil {
t.Errorf("NewCleanFormulasCmd got %v", cmd)
}

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

17 changes: 17 additions & 0 deletions pkg/cmd/clean_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cmd

import (
"testing"
)

func TestNewCleanCmd(t *testing.T) {
cmd := NewCleanCmd()
if cmd == nil {
t.Errorf("NewCleanCmd got %v", cmd)
}

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

32 changes: 32 additions & 0 deletions pkg/stdin/stdin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package stdin
import (
"encoding/json"
"io"
"io/ioutil"
"os"
)

const (
Expand All @@ -13,3 +15,33 @@ const (
func ReadJson(reader io.Reader, v interface{}) error {
return json.NewDecoder(reader).Decode(v)
}

// This function aims to facilitate stdin manipulations especially on tests
// inputs:
// content string: the string content for the stdin
// outputs:
// tmpfile File: the tmp file read by stdin
// oldstdin File: the original stdin being overwritten
// NOTICE: do not forget to remove the tmpfile and restore the original stdin, use the commands
// defer func() { os.Stdin = oldStdin }()
// defer os.Remove(tmpfile.Name())
func WriteToStdin(content string) (tmpfile *os.File, oldStdin *os.File, error error) {
fileContent := []byte(content)
tmpfile, err := ioutil.TempFile("", "stdin")
if err != nil {
return nil, nil, err
}

if _, err := tmpfile.Write(fileContent); err != nil {
return nil, nil, err
}

if _, err := tmpfile.Seek(0, 0); err != nil {
return nil, nil, err
}

oldstdin := os.Stdin
os.Stdin = tmpfile

return tmpfile, oldstdin, nil
}
10 changes: 10 additions & 0 deletions pkg/stdin/stdin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"encoding/json"
"os"
"testing"
)

Expand Down Expand Up @@ -35,4 +36,13 @@ func TestReadJson(t *testing.T) {
if msg != tr.Test {
t.Errorf("Expected : %v but got %v", msg, tr.Test)
}
}

func TestWriteToStdin(t *testing.T) {
tmpfile, oldStdin, err := WriteToStdin(`{"confirm": true}`)
defer os.Remove(tmpfile.Name())
defer func() { os.Stdin = oldStdin }()
if err != nil {
t.Errorf("TestNewCleanFormulasCmdStdin got error %v", err)
}
}