PickPlan shows an interactive selector over the candidate plans.
(candidates []dashboard.Plan)
| 113 | |
| 114 | // PickPlan shows an interactive selector over the candidate plans. |
| 115 | func PickPlan(candidates []dashboard.Plan) (*dashboard.Plan, error) { |
| 116 | if len(candidates) == 0 { |
| 117 | return nil, fmt.Errorf("no plans are available") |
| 118 | } |
| 119 | labels := make([]string, len(candidates)) |
| 120 | for i, p := range candidates { |
| 121 | labels[i] = fmt.Sprintf("%s — %s", p.Name, p.Price) |
| 122 | } |
| 123 | var selected int |
| 124 | if err := prompt.SurveyAskOne( |
| 125 | &survey.Select{ |
| 126 | Message: "Select a plan:", |
| 127 | Options: labels, |
| 128 | }, |
| 129 | &selected, |
| 130 | ); err != nil { |
| 131 | return nil, err |
| 132 | } |
| 133 | return &candidates[selected], nil |
| 134 | } |