diff --git a/cmd/tink-cli/cmd/get/get.go b/cmd/tink-cli/cmd/get/get.go index b8e345d69..6936e0352 100644 --- a/cmd/tink-cli/cmd/get/get.go +++ b/cmd/tink-cli/cmd/get/get.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" + "github.com/google/uuid" "github.com/jedib0t/go-pretty/table" "github.com/pkg/errors" "github.com/spf13/cobra" @@ -19,6 +20,8 @@ type Options struct { RetrieveData func(context.Context, *client.FullClient) ([]interface{}, error) // RetrieveByID is used when a get command has a list of arguments RetrieveByID func(context.Context, *client.FullClient, string) (interface{}, error) + // RetrieveByName is used when a get command has a list of arguments + RetrieveByName func(context.Context, *client.FullClient, string) (interface{}, error) // PopulateTable populates a table with the data retrieved with the RetrieveData function. PopulateTable func([]interface{}, table.Writer) error @@ -95,16 +98,7 @@ func NewGetCommand(opt Options) *cobra.Command { t.SetOutputMirror(cmd.OutOrStdout()) if len(args) != 0 { - if opt.RetrieveByID == nil { - return errors.New("option RetrieveByID is not implemented for this resource yet. Please have a look at the issue in GitHub or open a new one") - } - for _, requestedID := range args { - s, err := opt.RetrieveByID(cmd.Context(), opt.fullClient, requestedID) - if err != nil { - continue - } - data = append(data, s) - } + data, err = retrieveMulti(opt, cmd, args) } else { data, err = opt.RetrieveData(cmd.Context(), opt.fullClient) } @@ -154,3 +148,31 @@ func NewGetCommand(opt Options) *cobra.Command { opt.clientConnOpt.SetFlags(cmd.PersistentFlags()) return cmd } + +func retrieveMulti(opt Options, cmd *cobra.Command, args []string) ([]interface{}, error) { + var data []interface{} + for _, arg := range args { + var retriever func(context.Context, *client.FullClient, string) (interface{}, error) + if _, err := uuid.Parse(arg); err != nil { + // arg is invalid UUID, search for arg in `name` field of db + if opt.RetrieveByName == nil { + return nil, errors.New("get by Name is not implemented for this resource yet, please have a look at the issue in GitHub or open a new one") + } + retriever = opt.RetrieveByName + } else { + // arg is a valid UUID, search for arg in `id` field of db + if opt.RetrieveByID == nil { + return nil, errors.New("get by ID is not implemented for this resource yet, please have a look at the issue in GitHub or open a new one") + } + retriever = opt.RetrieveByID + } + + s, err := retriever(cmd.Context(), opt.fullClient, arg) + if err != nil { + continue + } + data = append(data, s) + } + + return data, nil +} diff --git a/cmd/tink-cli/cmd/template/get.go b/cmd/tink-cli/cmd/template/get.go index fdfb6b535..99f5da295 100644 --- a/cmd/tink-cli/cmd/template/get.go +++ b/cmd/tink-cli/cmd/template/get.go @@ -26,19 +26,18 @@ var GetCmd = &cobra.Command{ if len(args) == 0 { return fmt.Errorf("%v requires an argument", c.UseLine()) } - for _, arg := range args { - if _, err := uuid.Parse(arg); err != nil { - return fmt.Errorf("invalid uuid: %s", arg) - } - } return nil }, Run: func(c *cobra.Command, args []string) { for _, arg := range args { - req := template.GetRequest{ - GetBy: &template.GetRequest_Id{ - Id: arg, - }, + req := template.GetRequest{} + // Parse arg[0] to see if it is a UUID + if _, err := uuid.Parse(arg); err == nil { + // UUID + req.GetBy = &template.GetRequest_Id{Id: arg} + } else { + // String (Name) + req.GetBy = &template.GetRequest_Name{Name: arg} } t, err := client.TemplateClient.GetTemplate(context.Background(), &req) if err != nil { @@ -61,6 +60,14 @@ func (h *getTemplate) RetrieveByID(ctx context.Context, cl *client.FullClient, r }) } +func (h *getTemplate) RetrieveByName(_ context.Context, cl *client.FullClient, requestName string) (interface{}, error) { + return cl.TemplateClient.GetTemplate(context.Background(), &template.GetRequest{ + GetBy: &template.GetRequest_Name{ + Name: requestName, + }, + }) +} + func (h *getTemplate) RetrieveData(ctx context.Context, cl *client.FullClient) ([]interface{}, error) { list, err := cl.TemplateClient.ListTemplates(ctx, &template.ListRequest{ FilterBy: &template.ListRequest_Name{ @@ -98,9 +105,10 @@ func (h *getTemplate) PopulateTable(data []interface{}, t table.Writer) error { func NewGetOptions() get.Options { h := getTemplate{} return get.Options{ - Headers: []string{"ID", "Name", "Created At", "Updated At"}, - RetrieveByID: h.RetrieveByID, - RetrieveData: h.RetrieveData, - PopulateTable: h.PopulateTable, + Headers: []string{"ID", "Name", "Created At", "Updated At"}, + RetrieveByID: h.RetrieveByID, + RetrieveByName: h.RetrieveByName, + RetrieveData: h.RetrieveData, + PopulateTable: h.PopulateTable, } }