Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add functionality for users to retrieve template by Name as well as UUID
Browse files Browse the repository at this point in the history
Signed-off-by: William Belcher <[email protected]>
Co-Authored-by: Manuel Mendez <[email protected]>
Signed-off-by: Manuel Mendez <[email protected]>
Belchy06 and mmlb committed Apr 18, 2022

Partially verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
We cannot verify signatures from co-authors, and some of the co-authors attributed to this commit require their commits to be signed.
1 parent b39b56d commit 1c544ec
Showing 2 changed files with 53 additions and 23 deletions.
42 changes: 32 additions & 10 deletions cmd/tink-cli/cmd/get/get.go
Original file line number Diff line number Diff line change
@@ -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

@@ -62,16 +65,7 @@ func NewGetCommand(opt Options) *cobra.Command {

client := clientctx.Get(cmd.Context())
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(), client, requestedID)
if err != nil {
continue
}
data = append(data, s)
}
data, err = retrieveMulti(cmd.Context(), opt, client, args)
} else {
data, err = opt.RetrieveData(cmd.Context(), client)
}
@@ -117,3 +111,31 @@ func NewGetCommand(opt Options) *cobra.Command {
cmd.PersistentFlags().BoolVar(&opt.NoHeaders, "no-headers", false, "Table contains an header with the columns' name. You can disable it from being printed out")
return cmd
}

func retrieveMulti(ctx context.Context, opt Options, fc *client.FullClient, 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(ctx, fc, arg)
if err != nil {
continue
}
data = append(data, s)
}

return data, nil
}
34 changes: 21 additions & 13 deletions cmd/tink-cli/cmd/template/get.go
Original file line number Diff line number Diff line change
@@ -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,
}
}

0 comments on commit 1c544ec

Please sign in to comment.