Skip to content

Commit

Permalink
feat(cmd): Add apikeys get command (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
clemfromspace authored Mar 26, 2024
1 parent 119d337 commit 01a0555
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 15 deletions.
2 changes: 2 additions & 0 deletions pkg/cmd/apikeys/apikeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/algolia/cli/pkg/cmd/apikeys/create"
"github.com/algolia/cli/pkg/cmd/apikeys/delete"
"github.com/algolia/cli/pkg/cmd/apikeys/get"
"github.com/algolia/cli/pkg/cmd/apikeys/list"
"github.com/algolia/cli/pkg/cmdutil"
)
Expand All @@ -20,6 +21,7 @@ func NewAPIKeysCmd(f *cmdutil.Factory) *cobra.Command {
cmd.AddCommand(list.NewListCmd(f, nil))
cmd.AddCommand(create.NewCreateCmd(f, nil))
cmd.AddCommand(delete.NewDeleteCmd(f, nil))
cmd.AddCommand(get.NewGetCmd(f, nil))

return cmd
}
98 changes: 98 additions & 0 deletions pkg/cmd/apikeys/get/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package get

import (
"fmt"

"github.com/MakeNowJust/heredoc"
"github.com/algolia/algoliasearch-client-go/v3/algolia/search"
"github.com/spf13/cobra"

"github.com/algolia/cli/pkg/cmd/apikeys/shared"
"github.com/algolia/cli/pkg/cmdutil"
"github.com/algolia/cli/pkg/config"
"github.com/algolia/cli/pkg/iostreams"
"github.com/algolia/cli/pkg/validators"
)

// GetOptions represents the options for the get command
type GetOptions struct {
config config.IConfig
IO *iostreams.IOStreams

SearchClient func() (*search.Client, error)

APIKey string

PrintFlags *cmdutil.PrintFlags
}

// NewGetCmd returns a new instance of DeleteCmd
func NewGetCmd(f *cmdutil.Factory, runF func(*GetOptions) error) *cobra.Command {
opts := &GetOptions{
IO: f.IOStreams,
config: f.Config,
SearchClient: f.SearchClient,
PrintFlags: cmdutil.NewPrintFlags().WithDefaultOutput("json"),
}

cmd := &cobra.Command{
Use: "get <api-key>",
Short: "Get API key",
Long: heredoc.Doc(`
Get the details of a given API Key (ACLs, description, indexes, and other attributes).
`),
Example: heredoc.Doc(`
# Get an API key
$ algolia --application-id app-id apikeys get abcdef1234567890
`),
Args: validators.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opts.APIKey = args[0]

if runF != nil {
return runF(opts)
}

return runGetCmd(opts)
},
}

return cmd
}

// runGetCmd runs the get command
func runGetCmd(opts *GetOptions) error {
opts.config.Profile().APIKey = opts.APIKey
client, err := opts.SearchClient()
if err != nil {
return err
}

key, err := client.GetAPIKey(opts.APIKey)
if err != nil {
return fmt.Errorf("API key %q does not exist", opts.APIKey)
}

p, err := opts.PrintFlags.ToPrinter()
if err != nil {
return err
}
keyResult := shared.JSONKey{
ACL: key.ACL,
CreatedAt: key.CreatedAt,
Description: key.Description,
Indexes: key.Indexes,
MaxQueriesPerIPPerHour: key.MaxQueriesPerIPPerHour,
MaxHitsPerQuery: key.MaxHitsPerQuery,
Referers: key.Referers,
QueryParameters: key.QueryParameters,
Validity: key.Validity,
Value: key.Value,
}

if err := p.Print(opts.IO, keyResult); err != nil {
return err
}

return nil
}
62 changes: 62 additions & 0 deletions pkg/cmd/apikeys/get/get_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package get

import (
"testing"

"github.com/algolia/algoliasearch-client-go/v3/algolia/search"
"github.com/stretchr/testify/assert"

"github.com/algolia/cli/pkg/httpmock"
"github.com/algolia/cli/test"
)

func Test_runGetCmd(t *testing.T) {
tests := []struct {
name string
key string
wantErr string
}{
{
name: "get a key (success)",
key: "foo",
},
{
name: "get a key (error)",
key: "bar",
wantErr: "API key \"bar\" does not exist",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := httpmock.Registry{}
if tt.key == "foo" {
r.Register(
httpmock.REST("GET", "1/keys/foo"),
httpmock.JSONResponse(search.Key{
Value: "foo",
Description: "test",
ACL: []string{"*"},
Validity: 0,
MaxHitsPerQuery: 0,
MaxQueriesPerIPPerHour: 0,
Referers: []string{},
}),
)
} else {
r.Register(
httpmock.REST("GET", "1/keys/bar"),
httpmock.ErrorResponse(),
)
}

f, out := test.NewFactory(false, &r, nil, "")
cmd := NewGetCmd(f, nil)
_, err := test.Execute(cmd, tt.key, out)
if err != nil {
assert.Equal(t, tt.wantErr, err.Error())
return
}
})
}
}
17 changes: 2 additions & 15 deletions pkg/cmd/apikeys/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/dustin/go-humanize"
"github.com/spf13/cobra"

"github.com/algolia/cli/pkg/cmd/apikeys/shared"
"github.com/algolia/cli/pkg/cmdutil"
"github.com/algolia/cli/pkg/config"
"github.com/algolia/cli/pkg/iostreams"
Expand Down Expand Up @@ -54,20 +55,6 @@ func NewListCmd(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
return cmd
}

// JSONKey is the same as search.Key without omitting values
type JSONKey struct {
ACL []string `json:"acl"`
CreatedAt time.Time `json:"createdAt"`
Description string `json:"description"`
Indexes []string `json:"indexes"`
MaxQueriesPerIPPerHour int `json:"maxQueriesPerIPPerHour"`
MaxHitsPerQuery int `json:"maxHitsPerQuery"`
Referers []string `json:"referers"`
QueryParameters search.KeyQueryParams `json:"queryParameters"`
Validity time.Duration `json:"validity"`
Value string `json:"value"`
}

// runListCmd executes the list command
func runListCmd(opts *ListOptions) error {
client, err := opts.SearchClient()
Expand All @@ -88,7 +75,7 @@ func runListCmd(opts *ListOptions) error {
return err
}
for _, key := range res.Keys {
keyResult := JSONKey{
keyResult := shared.JSONKey{
ACL: key.ACL,
CreatedAt: key.CreatedAt,
Description: key.Description,
Expand Down
21 changes: 21 additions & 0 deletions pkg/cmd/apikeys/shared/shared.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package shared

import (
"time"

"github.com/algolia/algoliasearch-client-go/v3/algolia/search"
)

// JSONKey is the same as search.Key without omitting values
type JSONKey struct {
ACL []string `json:"acl"`
CreatedAt time.Time `json:"createdAt"`
Description string `json:"description"`
Indexes []string `json:"indexes"`
MaxQueriesPerIPPerHour int `json:"maxQueriesPerIPPerHour"`
MaxHitsPerQuery int `json:"maxHitsPerQuery"`
Referers []string `json:"referers"`
QueryParameters search.KeyQueryParams `json:"queryParameters"`
Validity time.Duration `json:"validity"`
Value string `json:"value"`
}

0 comments on commit 01a0555

Please # to comment.