From 0936873287c3af79bed43a7418b17f10d4f99996 Mon Sep 17 00:00:00 2001 From: Chris Laprun Date: Wed, 18 Sep 2019 12:00:47 +0200 Subject: [PATCH] refactor: move selectOrCheckExisting to ui package --- pkg/hal/cli/capability/create.go | 20 +++----------------- pkg/ui/ui.go | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/pkg/hal/cli/capability/create.go b/pkg/hal/cli/capability/create.go index 4a857c2..5c8dc9c 100644 --- a/pkg/hal/cli/capability/create.go +++ b/pkg/hal/cli/capability/create.go @@ -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 { @@ -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"} diff --git a/pkg/ui/ui.go b/pkg/ui/ui.go index 5432f0b..3203c43 100644 --- a/pkg/ui/ui.go +++ b/pkg/ui/ui.go @@ -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 @@ -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() }