github.com/taubyte/tau-cli@v0.1.13-0.20240326000942-487f0d57edfc/prompts/database/select.go (about) 1 package databasePrompts 2 3 import ( 4 "errors" 5 "fmt" 6 "strings" 7 8 structureSpec "github.com/taubyte/go-specs/structure" 9 "github.com/taubyte/tau-cli/flags" 10 databaseI18n "github.com/taubyte/tau-cli/i18n/database" 11 databaseLib "github.com/taubyte/tau-cli/lib/database" 12 "github.com/taubyte/tau-cli/prompts" 13 14 "github.com/urfave/cli/v2" 15 ) 16 17 /* 18 GetOrSelect will try to get the database from a name flag 19 if it is not set in the flag it will offer a selection menu 20 */ 21 func GetOrSelect(ctx *cli.Context) (*structureSpec.Database, error) { 22 name := ctx.String(flags.Name.Name) 23 24 resources, err := databaseLib.ListResources() 25 if err != nil { 26 return nil, err 27 } 28 29 // Try to select a database 30 if len(name) == 0 && len(resources) > 0 { 31 options := make([]string, len(resources)) 32 for idx, p := range resources { 33 options[idx] = p.Name 34 } 35 36 name, err = prompts.SelectInterface(options, SelectPrompt, options[0]) 37 if err != nil { 38 return nil, databaseI18n.SelectPromptFailed(err) 39 } 40 } 41 42 if len(name) != 0 { 43 for _, database := range resources { 44 if strings.EqualFold(name, database.Name) { 45 return database, nil 46 } 47 } 48 49 return nil, fmt.Errorf(NotFound, name) 50 } 51 52 return nil, errors.New(NoneFound) 53 }