Skip to content

Commit

Permalink
Merge pull request #9 from swfz/feature/display-selected-value
Browse files Browse the repository at this point in the history
feature/display selected value
  • Loading branch information
swfz authored Oct 31, 2022
2 parents 52c5263 + ab45c8b commit c00ddcc
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions survey.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
)

func askOneProjectId(projects []Project) (projectId string) {
projectIds := make([]string, len(projects))
projectNames := make([]string, len(projects))
for i, node := range projects {
projectIds[i] = node.Id
projectNames[i] = node.Title
if node.Id == "" {
log.Print(`[Warning] This extension requires permission for the "project" scope. You may not currently have permission to retrieve project information, please check`)
}
Expand All @@ -23,23 +23,23 @@ func askOneProjectId(projects []Project) (projectId string) {
Name: "ProjectId",
Prompt: &survey.Select{
Message: "Choose a Project",
Options: projectIds,
Options: projectNames,
Description: func(value string, index int) string {
return projects[index].Title + " (" + projects[index].Type + ")"
},
Filter: func(filterValue string, optValue string, optIndex int) bool {
return strings.Contains(projects[optIndex].Title, filterValue)
return projects[index].Type
},
},
},
}
answers := struct{ ProjectId string }{}
answers := map[string]interface{}{}

err := survey.Ask(qs, &answers)
if err != nil {
log.Fatal(err.Error())
}

return answers.ProjectId
optionAnswer := answers["ProjectId"].(survey.OptionAnswer)

return projects[optionAnswer.Index].Id
}

func askOneContentType(itemTypes []string) string {
Expand Down Expand Up @@ -154,30 +154,25 @@ func askNumberFieldValue(fieldName string) float64 {

func askOneSelectFieldValue(fieldName string, options []Option) string {
fieldOptionSize := len(options)
optionIds := make([]string, fieldOptionSize)
optionNames := make([]string, fieldOptionSize)
for i, opt := range options {
optionIds[i] = opt.Id
optionNames[i] = opt.Name
}
qs := []*survey.Question{
{
Name: fieldName,
Prompt: &survey.Select{
Message: fieldName,
Options: optionIds,
Description: func(value string, index int) string {
return options[index].Name
},
Filter: func(filterValue string, optValue string, optIndex int) bool {
return strings.Contains(options[optIndex].Name, filterValue)
},
Options: optionNames,
},
},
}
answers := map[string]string{}
answers := map[string]interface{}{}
err := survey.Ask(qs, &answers)
if err != nil {
log.Fatal(err.Error())
}
optionAnswer := answers[fieldName].(survey.OptionAnswer)

return answers[fieldName]
return options[optionAnswer.Index].Id
}

0 comments on commit c00ddcc

Please sign in to comment.