Skip to content

Commit

Permalink
refactor: move selectOrCheckExisting to ui package
Browse files Browse the repository at this point in the history
  • Loading branch information
metacosm committed Oct 7, 2019
1 parent 99520ba commit 0936873
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
20 changes: 3 additions & 17 deletions pkg/hal/cli/capability/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ func (o *createOptions) Build() runtime.Object {
}

func (o *createOptions) Complete(name string, cmd *cobra.Command, args []string) error {
o.selectOrCheckExisting(&o.category, "Category", o.getCategories(), o.isValidCategory)
o.selectOrCheckExisting(&o.subCategory, "Type", o.getTypesFor(o.category), o.isValidTypeGivenCategory)
o.selectOrCheckExisting(&o.version, "Version", o.getVersionsFor(o.category, o.subCategory), o.isValidVersionGivenCategoryAndType)
ui.SelectOrCheckExisting(&o.category, "Category", o.getCategories(), o.isValidCategory)
ui.SelectOrCheckExisting(&o.subCategory, "Type", o.getTypesFor(o.category), o.isValidTypeGivenCategory)
ui.SelectOrCheckExisting(&o.version, "Version", o.getVersionsFor(o.category, o.subCategory), o.isValidVersionGivenCategoryAndType)

for _, pair := range o.paramPairs {
if e := o.addToParams(pair); e != nil {
Expand Down Expand Up @@ -102,20 +102,6 @@ func (p parameterInfo) AsValidatable() validation.Validatable {
return p.Validatable
}

func (o *createOptions) selectOrCheckExisting(parameterValue *string, capitalizedParameterName string, validValues []string, validator func() bool) {
if len(*parameterValue) == 0 {
*parameterValue = ui.Select(capitalizedParameterName, validValues)
} else {
lowerCaseParameterName := strings.ToLower(capitalizedParameterName)
if !validator() {
s := ui.SelectFromOtherErrorMessage("Unknown "+lowerCaseParameterName, *parameterValue)
ui.Select(s, validValues)
} else {
ui.OutputSelection("Selected "+lowerCaseParameterName, *parameterValue)
}
}
}

func (o *createOptions) getCategories() []string {
// todo: implement operator querying of available capabilities
return []string{"database"}
Expand Down
15 changes: 15 additions & 0 deletions pkg/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"halkyon.io/hal/pkg/validation"
"os"
"sort"
"strings"
)

// HandleError handles UI-related errors, in particular useful to gracefully handle ctrl-c interrupts gracefully
Expand Down Expand Up @@ -125,6 +126,20 @@ func SelectFromOtherErrorMessage(msg, wrong string) string {
return fmt.Sprintf("%s%s: %s%s\nSelect other(s) from:", ansi.Red, msg, wrong, ansi.ColorCode("default"))
}

func SelectOrCheckExisting(parameterValue *string, capitalizedParameterName string, validValues []string, validator func() bool) {
if len(*parameterValue) == 0 {
*parameterValue = Select(capitalizedParameterName, validValues)
} else {
lowerCaseParameterName := strings.ToLower(capitalizedParameterName)
if !validator() {
s := SelectFromOtherErrorMessage("Unknown "+lowerCaseParameterName, *parameterValue)
Select(s, validValues)
} else {
OutputSelection("Selected "+lowerCaseParameterName, *parameterValue)
}
}
}

func init() {
core.SetFancyIcons()
}

0 comments on commit 0936873

Please sign in to comment.