github.com/taubyte/tau-cli@v0.1.13-0.20240326000942-487f0d57edfc/cli/commands/resources/common/query.go (about) 1 package resources 2 3 import ( 4 structureSpec "github.com/taubyte/go-specs/structure" 5 "github.com/taubyte/tau-cli/cli/common" 6 "github.com/taubyte/tau-cli/flags" 7 projectLib "github.com/taubyte/tau-cli/lib/project" 8 "github.com/urfave/cli/v2" 9 ) 10 11 type Query[T structureSpec.Structure] struct { 12 LibListResources func() ([]T, error) 13 TableList func([]T) 14 15 PromptsGetOrSelect func(ctx *cli.Context) (T, error) 16 TableQuery func(T) 17 } 18 19 func (h *Query[T]) Default() common.Command { 20 return common.Create( 21 &cli.Command{ 22 Flags: h.BasicFlags(), 23 Action: h.Action(), 24 }, 25 ) 26 } 27 28 func (h *Query[T]) BasicFlags() []cli.Flag { 29 return []cli.Flag{ 30 flags.List, 31 } 32 } 33 34 func (h *Query[T]) Action() func(ctx *cli.Context) error { 35 PanicIfMissingValue(h) 36 37 return func(ctx *cli.Context) error { 38 err := projectLib.ConfirmSelectedProject() 39 if err != nil { 40 return err 41 } 42 43 // will call list if the --list flag is set 44 if ctx.Bool(flags.List.Name) { 45 return h.list(ctx) 46 } 47 48 // Prompts.GetOrSelect will get the struct from --name or offer a selection menu 49 resource, err := h.PromptsGetOrSelect(ctx) 50 if err != nil { 51 return err 52 } 53 54 // Table.Query will display a detailed table for the selected resource 55 h.TableQuery(resource) 56 57 return nil 58 } 59 } 60 61 func (h *Query[T]) list(ctx *cli.Context) error { 62 // Lib.ListResources will return a []structureSpec.Resource 63 // for accessing values within all of 64 // the relative(project/application) values 65 resources, err := h.LibListResources() 66 if err != nil { 67 return err 68 } 69 70 // Table.List will display a table of all of the resources 71 h.TableList(resources) 72 return nil 73 }