Skip to content

Commit

Permalink
Move survey questions from backend to asker pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
valaparthvi committed Feb 15, 2022
1 parent fc0fd67 commit 331dc19
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 39 deletions.
56 changes: 54 additions & 2 deletions pkg/init/asker/asker.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package asker

import (
"sort"

"fmt"
"github.com/AlecAivazis/survey/v2"
"sort"

"github.com/redhat-developer/odo/pkg/catalog"
)
Expand Down Expand Up @@ -77,3 +77,55 @@ func (o *Survey) AskName(defaultName string) (string, error) {
}
return answer, nil
}

func (o *Survey) AskAddPort(containers []string) (containerNameAnswer, newPortAnswer string, err error) {
containerNameQuestion := &survey.Select{
Message: "Enter container name: ",
Options: containers,
}
err = survey.AskOne(containerNameQuestion, &containerNameAnswer)
if err != nil {
return
}
newPortQuestion := &survey.Input{
Message: "Enter port number:",
}
err = survey.AskOne(newPortQuestion, &newPortAnswer)
if err != nil {
return
}
return
}

func (o *Survey) AskAddEnvVar() (newEnvNameAnswer, newEnvValueAnswer string, err error) {
newEnvNameQuesion := &survey.Input{
Message: "Enter new environment variable name:",
}
// Ask for env name
survey.AskOne(newEnvNameQuesion, &newEnvNameAnswer)
newEnvValueQuestion := &survey.Input{
Message: fmt.Sprintf("Enter value for %q environment variable:", newEnvNameAnswer),
}

// Ask for env value
err = survey.AskOne(newEnvValueQuestion, &newEnvValueAnswer)
if err != nil {
return
}
return
}

func (o *Survey) AskPersonalizeConfiguration(options []string) (configChangeAnswer string, configChangeIndex int, err error) {
configChangeQuestion := &survey.Select{
Message: "What configuration do you want change?",
Default: options[0],
Options: options,
}

err = survey.AskOne(configChangeQuestion, &configChangeIndex)
if err != nil {
return
}
configChangeAnswer = options[configChangeIndex]
return
}
9 changes: 9 additions & 0 deletions pkg/init/asker/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,13 @@ type Asker interface {

// AskName asks for a devfile component name
AskName(defaultName string) (string, error)

// AskPersonalizeConfiguration asks the configuration user wants to change
AskPersonalizeConfiguration(options []string) (configChangeAnswer string, configChangeIndex int, err error)

// AskAddPort asks the container name and port that user wants to add
AskAddPort(containers []string) (containerNameAnswer, newPortAnswer string, err error)

// AskAddEnvVar asks the key and value for env var
AskAddEnvVar() (newEnvNameAnswer, newEnvValueAnswer string, err error)
}
63 changes: 26 additions & 37 deletions pkg/init/backend/interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strconv"
"strings"

"github.com/AlecAivazis/survey/v2"
"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/library/pkg/devfile/parser"
parsercommon "github.com/devfile/library/pkg/devfile/parser/data/v2/common"
Expand Down Expand Up @@ -109,7 +108,19 @@ func (o *InteractiveBackend) PersonalizeName(devfile parser.DevfileObj, flags ma
return devfile.SetMetadataName(name)
}

// type devfileConfig struct {
// // ops will be add or remove
// Ops string
// // kind will be port or env var
// Kind string
// // key will be container name in case of port ops, and env var key in case of env var
// Key string
// // value will be an array of ports in case of port ops, and env var value in case of env var
// Value string
// }

func (o *InteractiveBackend) PersonalizeDevfileConfig(devfileobj parser.DevfileObj) error {
// var d devfileConfig
var envs = map[string]string{}
var portsMap = map[string][]string{}
var deletePortMessage = "Delete port (container: %q): %q"
Expand Down Expand Up @@ -144,24 +155,15 @@ func (o *InteractiveBackend) PersonalizeDevfileConfig(devfileobj parser.DevfileO
for configChangeAnswer != "NOTHING - configuration is correct" {
printConfiguration(portsMap, envs)

configChangeQuestion := &survey.Select{
Message: "What configuration do you want change?",
Default: options[0],
Options: options,
}

err = survey.AskOne(configChangeQuestion, &configChangeIndex)
if err != nil {
return err
}
configChangeAnswer = options[configChangeIndex]
configChangeAnswer, configChangeIndex, err = o.asker.AskPersonalizeConfiguration(options)

if strings.HasPrefix(configChangeAnswer, "Delete port") {
containerName, portToDelete := options2[configChangeIndex][0], options2[configChangeIndex][1]
if !parser.InArray(portsMap[containerName], portToDelete) {
log.Warningf("unable to delete port %q, not found", portToDelete)
continue
}

// Delete port from the devfile
err = devfileobj.RemovePorts(map[string][]string{containerName: {portToDelete}})
if err != nil {
Expand All @@ -183,6 +185,12 @@ func (o *InteractiveBackend) PersonalizeDevfileConfig(devfileobj parser.DevfileO
}
}
} else if strings.HasPrefix(configChangeAnswer, "Delete environment variable") {
// d = devfileConfig{
// Ops: "Delete",
// Kind: "EnvVar",
// Key: "",
// Value: "",
// }
envToDelete := options2[configChangeIndex][0]
if _, ok := envs[envToDelete]; !ok {
log.Warningf("unable to delete env %q, not found", envToDelete)
Expand All @@ -203,22 +211,14 @@ func (o *InteractiveBackend) PersonalizeDevfileConfig(devfileobj parser.DevfileO
}
} else if configChangeAnswer == "Add new port" {
var containers []string
var containerNameAnswer, newPortAnswer string
for containerName, _ := range portsMap {
containers = append(containers, containerName)
}
containerNameQuestion := &survey.Select{
Message: "Enter container name: ",
Options: containers,
}
var containerNameAnswer string
survey.AskOne(containerNameQuestion, &containerNameAnswer)

newPortQuestion := &survey.Input{
Message: "Enter port number:",
containerNameAnswer, newPortAnswer, err = o.asker.AskAddPort(containers)
if err != nil {
return err
}
var newPortAnswer string
survey.AskOne(newPortQuestion, &newPortAnswer)

// Ensure the newPortAnswer is not already present; otherwise it will cause a duplicate endpoint error while parsing the devfile
if parser.InArray(portsMap[containerNameAnswer], newPortAnswer) {
log.Warningf("Port is %q already present in container %q.", newPortAnswer, containerNameAnswer)
Expand All @@ -234,19 +234,8 @@ func (o *InteractiveBackend) PersonalizeDevfileConfig(devfileobj parser.DevfileO
options = append(options, fmt.Sprintf(deletePortMessage, containerNameAnswer, newPortAnswer))
options2 = append(options2, [2]string{containerNameAnswer, newPortAnswer})
} else if configChangeAnswer == "Add new environment variable" {
newEnvNameQuesion := &survey.Input{
Message: "Enter new environment variable name:",
}
// Ask for env name
var newEnvNameAnswer string
survey.AskOne(newEnvNameQuesion, &newEnvNameAnswer)
newEnvValueQuestion := &survey.Input{
Message: fmt.Sprintf("Enter value for %q environment variable:", newEnvNameAnswer),
}

// Ask for env value
var newEnvValueAnswer string
survey.AskOne(newEnvValueQuestion, &newEnvValueAnswer)
var newEnvNameAnswer, newEnvValueAnswer string
newEnvNameAnswer, newEnvValueAnswer, err = o.asker.AskAddEnvVar()

// Add env var
err = devfileobj.AddEnvVars([]v1alpha2.EnvVar{
Expand Down

0 comments on commit 331dc19

Please sign in to comment.