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

Feature/surveyprompt #210

Merged
merged 5 commits into from
Jun 25, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 9 additions & 7 deletions cmd/single/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ func buildCommands() *cobra.Command {
ritchieHomeDir := api.RitchieHomeDir()

// prompt
inputText := prompt.NewInputText()
inputInt := prompt.NewInputInt()
inputBool := prompt.NewInputBool()
inputPassword := prompt.NewInputPassword()
inputList := prompt.NewInputList()
inputURL := prompt.NewInputURL()
inputText := prompt.NewSurveyText()
inputInt := prompt.NewSurveyInt()
inputBool := prompt.NewSurveyBool()
inputPassword := prompt.NewSurveyPassword()
inputList := prompt.NewSurveyList()
inputURL := prompt.NewSurveyURL()
inputMultiline := prompt.NewSurveyMultiline()

// deps
sessionManager := session.NewManager(ritchieHomeDir)
Expand Down Expand Up @@ -117,7 +118,8 @@ func buildCommands() *cobra.Command {
inputText,
inputBool,
inputList,
inputPassword)
inputPassword,
inputMultiline)
deleteCtxCmd := cmd.NewDeleteContextCmd(ctxFindRemover, inputBool, inputList)
setCtxCmd := cmd.NewSetContextCmd(ctxFindSetter, inputText, inputList)
showCtxCmd := cmd.NewShowContextCmd(ctxFinder)
Expand Down
16 changes: 9 additions & 7 deletions cmd/team/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ func buildCommands() *cobra.Command {
ritchieHomeDir := api.RitchieHomeDir()

// prompt
inputText := prompt.NewInputText()
inputInt := prompt.NewInputInt()
inputBool := prompt.NewInputBool()
inputPassword := prompt.NewInputPassword()
inputList := prompt.NewInputList()
inputURL := prompt.NewInputURL()
inputText := prompt.NewSurveyText()
inputInt := prompt.NewSurveyInt()
inputBool := prompt.NewSurveyBool()
inputPassword := prompt.NewSurveyPassword()
inputList := prompt.NewSurveyList()
inputURL := prompt.NewSurveyURL()
inputMultiline := prompt.NewSurveyMultiline()

// deps
sessionManager := session.NewManager(ritchieHomeDir)
Expand Down Expand Up @@ -142,7 +143,8 @@ func buildCommands() *cobra.Command {
inputText,
inputBool,
inputList,
inputPassword)
inputPassword,
inputMultiline)
deleteCtxCmd := cmd.NewDeleteContextCmd(ctxFindRemover, inputBool, inputList)
setCtxCmd := cmd.NewSetContextCmd(ctxFindSetter, inputText, inputList)
showCtxCmd := cmd.NewShowContextCmd(ctxFinder)
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/ZupIT/ritchie-cli
go 1.14

require (
github.com/AlecAivazis/survey/v2 v2.0.7
github.com/denisbrodbeck/machineid v1.0.1
github.com/fatih/color v1.9.0 // indirect
github.com/gofrs/flock v0.7.1
Expand All @@ -12,9 +13,11 @@ require (
github.com/kaduartur/go-cli-spinner v1.0.0
github.com/manifoldco/promptui v0.7.0
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/onsi/ginkgo v1.11.0
github.com/onsi/gomega v1.7.0
github.com/pkg/errors v0.9.1
github.com/radovskyb/watcher v1.0.7
github.com/spf13/cobra v1.0.0
github.com/thoas/go-funk v0.6.0
k8s.io/kubectl v0.18.4 // indirect
k8s.io/kubectl v0.18.4
)
2 changes: 1 addition & 1 deletion pkg/cmd/delete_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (d deleteContextCmd) runPrompt() CommandRunnerFunc {
}
}

ctx, err := d.List("All:", ctxHolder.All)
ctx, err := d.List("Contexts:", ctxHolder.All)
if err != nil {
return err
}
Expand Down
15 changes: 9 additions & 6 deletions pkg/cmd/set_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type setCredentialCmd struct {
prompt.InputBool
prompt.InputList
prompt.InputPassword
prompt.InputMultiline
}

// NewSingleSetCredentialCmd creates a new cmd instance
Expand All @@ -31,8 +32,9 @@ func NewSingleSetCredentialCmd(
it prompt.InputText,
ib prompt.InputBool,
il prompt.InputList,
ip prompt.InputPassword) *cobra.Command {
s := &setCredentialCmd{st, nil, api.Single, it, ib, il, ip}
ip prompt.InputPassword,
im prompt.InputMultiline) *cobra.Command {
s := &setCredentialCmd{st, nil, api.Single, it, ib, il, ip, im}

return newCmd(s)
}
Expand All @@ -44,8 +46,9 @@ func NewTeamSetCredentialCmd(
it prompt.InputText,
ib prompt.InputBool,
il prompt.InputList,
ip prompt.InputPassword) *cobra.Command {
s := &setCredentialCmd{st, si, api.Team, it, ib, il, ip}
ip prompt.InputPassword,
im prompt.InputMultiline) *cobra.Command {
s := &setCredentialCmd{st, si, api.Team, it, ib, il, ip, im}

return newCmd(s)
}
Expand Down Expand Up @@ -101,7 +104,7 @@ func (s setCredentialCmd) singlePrompt() (credential.Detail, error) {
cred := credential.Credential{}
addMore := true
for addMore {
kv, err := s.Text("Type your credential using the format key=value (e.g. email=example@example.com): ", true)
kv, err := s.MultiLineText("Type your credential using the format key=value (e.g. email=example@example.com): ", true)
if err != nil {
return credDetail, err
}
Expand Down Expand Up @@ -181,7 +184,7 @@ func (s setCredentialCmd) teamPrompt() (credential.Detail, error) {
}
credentials[field] = val
}

credDetail.Credential = credentials
credDetail.Service = service

Expand Down
27 changes: 25 additions & 2 deletions pkg/prompt/bool.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package prompt

import "github.com/manifoldco/promptui"
import (
"github.com/AlecAivazis/survey/v2"
"github.com/manifoldco/promptui"
)

var (
boolOpts = map[string]bool{"yes": true, "no": false, "true": true, "false": false}
Expand All @@ -12,15 +15,35 @@ type InputBool interface {

type inputBool struct{}

type surveyBool struct{}

func NewInputBool() inputBool {
return inputBool{}
}

func NewSurveyBool() surveyBool {
return surveyBool{}
}

func (surveyBool) Bool(name string, items []string) (bool, error) {

choice := ""
prompt := &survey.Select{
Message: name,
Options: items,
}
if err := survey.AskOne(prompt, &choice); err != nil {
return false, err
}

return boolOpts[choice], nil
}

// Bool show a prompt with options and parse to bool.
func (inputBool) Bool(name string, items []string) (bool, error) {
prompt := promptui.Select{
Items: items,
Pointer: promptui.PipeCursor,
Pointer: promptui.PipeCursor,
Templates: defaultSelectTemplate(name),
}
_, result, err := prompt.Run()
Expand Down
24 changes: 24 additions & 0 deletions pkg/prompt/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package prompt

import (
"github.com/ZupIT/ritchie-cli/pkg/validator"
"github.com/AlecAivazis/survey/v2"
"github.com/manifoldco/promptui"
)

Expand All @@ -11,10 +12,16 @@ type InputEmail interface {

type inputEmail struct{}

type surveyEmail struct{}

func NewInputEmail() inputEmail {
return inputEmail{}
}

func NewSurveyEmail() surveyEmail {
return surveyEmail{}
}

// Email show a prompt and parse the string to email.
func (inputEmail) Email(name string) (string, error) {
prompt := promptui.Prompt{
Expand All @@ -26,3 +33,20 @@ func (inputEmail) Email(name string) (string, error) {

return prompt.Run()
}

func (surveyEmail) Email(name string) (string, error) {

var value string

validationQs := []*survey.Question{
{
Name: "name",
Prompt: &survey.Input{
Message: name,
},
Validate: validator.IsValidSurveyEmail,
},
}

return value, survey.Ask(validationQs, &value)
}
37 changes: 34 additions & 3 deletions pkg/prompt/int.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package prompt

import (
"github.com/manifoldco/promptui"
"strconv"

"github.com/AlecAivazis/survey/v2"
"github.com/manifoldco/promptui"
)

type InputInt interface {
Expand All @@ -11,16 +13,22 @@ type InputInt interface {

type inputInt struct{}

type surveyInt struct{}

func NewInputInt() inputInt {
return inputInt{}
}

// Int show a prompt and parse to int.
func NewSurveyInt() surveyInt {
return surveyInt{}
}

//Int show a prompt and parse to int.
func (inputInt) Int(name string) (int64, error) {
prompt := promptui.Prompt{
Label: name,
Pointer: promptui.PipeCursor,
Validate: validateIntegerNumberInput,
Validate: validateIntIn,
Templates: defaultTemplate(),
}

Expand All @@ -35,3 +43,26 @@ func (inputInt) Int(name string) (int64, error) {
}
return parseInt, nil
}

func (surveyInt) Int(name string) (int64, error) {

var value string

validationQs := []*survey.Question{
{
Name: "name",
Prompt: &survey.Input{Message: name},
Validate: validateSurveyIntIn,
},
}
if err := survey.Ask(validationQs, &value); err != nil {
return 0, err
}

parseInt, err := strconv.ParseInt(value, 0, 64)
if err != nil {
return 0, err
}
return parseInt, nil
}

25 changes: 24 additions & 1 deletion pkg/prompt/list.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
package prompt

import "github.com/manifoldco/promptui"
import (
"github.com/AlecAivazis/survey/v2"
"github.com/manifoldco/promptui"
)

type InputList interface {
List(name string, items []string) (string, error)
}

type inputList struct{}

type surveyList struct{}

func NewInputList() inputList {
return inputList{}
}

func NewSurveyList() surveyList {
return surveyList{}
}

// List show a prompt with options and parse to string.
func (inputList) List(name string, items []string) (string, error) {
prompt := promptui.Select{
Expand All @@ -22,3 +31,17 @@ func (inputList) List(name string, items []string) (string, error) {
_, result, err := prompt.Run()
return result, err
}

// List show a prompt with options and parse to string.
func (surveyList) List(name string, items []string) (string, error) {
choice := ""
prompt := &survey.Select{
Message: name,
Options: items,
}
if err := survey.AskOne(prompt, &choice); err != nil{
return "", err
}

return choice, nil
}
44 changes: 44 additions & 0 deletions pkg/prompt/multiline.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package prompt

import (
"github.com/AlecAivazis/survey/v2"
)


type InputMultiline interface {
MultiLineText(name string, required bool) (string, error)
}


type surveyMultiline struct{}

func NewSurveyMultiline() surveyMultiline {
return surveyMultiline{}
}

func (surveyMultiline) MultiLineText(name string, required bool) (string, error) {

var value string

var validationQs []*survey.Question


if required{
validationQs = []*survey.Question{
{
Name: "name",
Prompt: &survey.Multiline{Message: name},
Validate: survey.Required,
},
}
}else {
validationQs = []*survey.Question{
{
Name: "name",
Prompt: &survey.Multiline{Message: name},
},
}
}
return value, survey.Ask(validationQs, &value)
}

Loading