Skip to content

Commit 01a0555

Browse files
author
Clément Denoix
authored
feat(cmd): Add apikeys get command (#153)
1 parent 119d337 commit 01a0555

File tree

5 files changed

+185
-15
lines changed

5 files changed

+185
-15
lines changed

pkg/cmd/apikeys/apikeys.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/algolia/cli/pkg/cmd/apikeys/create"
77
"github.com/algolia/cli/pkg/cmd/apikeys/delete"
8+
"github.com/algolia/cli/pkg/cmd/apikeys/get"
89
"github.com/algolia/cli/pkg/cmd/apikeys/list"
910
"github.com/algolia/cli/pkg/cmdutil"
1011
)
@@ -20,6 +21,7 @@ func NewAPIKeysCmd(f *cmdutil.Factory) *cobra.Command {
2021
cmd.AddCommand(list.NewListCmd(f, nil))
2122
cmd.AddCommand(create.NewCreateCmd(f, nil))
2223
cmd.AddCommand(delete.NewDeleteCmd(f, nil))
24+
cmd.AddCommand(get.NewGetCmd(f, nil))
2325

2426
return cmd
2527
}

pkg/cmd/apikeys/get/get.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package get
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/MakeNowJust/heredoc"
7+
"github.com/algolia/algoliasearch-client-go/v3/algolia/search"
8+
"github.com/spf13/cobra"
9+
10+
"github.com/algolia/cli/pkg/cmd/apikeys/shared"
11+
"github.com/algolia/cli/pkg/cmdutil"
12+
"github.com/algolia/cli/pkg/config"
13+
"github.com/algolia/cli/pkg/iostreams"
14+
"github.com/algolia/cli/pkg/validators"
15+
)
16+
17+
// GetOptions represents the options for the get command
18+
type GetOptions struct {
19+
config config.IConfig
20+
IO *iostreams.IOStreams
21+
22+
SearchClient func() (*search.Client, error)
23+
24+
APIKey string
25+
26+
PrintFlags *cmdutil.PrintFlags
27+
}
28+
29+
// NewGetCmd returns a new instance of DeleteCmd
30+
func NewGetCmd(f *cmdutil.Factory, runF func(*GetOptions) error) *cobra.Command {
31+
opts := &GetOptions{
32+
IO: f.IOStreams,
33+
config: f.Config,
34+
SearchClient: f.SearchClient,
35+
PrintFlags: cmdutil.NewPrintFlags().WithDefaultOutput("json"),
36+
}
37+
38+
cmd := &cobra.Command{
39+
Use: "get <api-key>",
40+
Short: "Get API key",
41+
Long: heredoc.Doc(`
42+
Get the details of a given API Key (ACLs, description, indexes, and other attributes).
43+
`),
44+
Example: heredoc.Doc(`
45+
# Get an API key
46+
$ algolia --application-id app-id apikeys get abcdef1234567890
47+
`),
48+
Args: validators.ExactArgs(1),
49+
RunE: func(cmd *cobra.Command, args []string) error {
50+
opts.APIKey = args[0]
51+
52+
if runF != nil {
53+
return runF(opts)
54+
}
55+
56+
return runGetCmd(opts)
57+
},
58+
}
59+
60+
return cmd
61+
}
62+
63+
// runGetCmd runs the get command
64+
func runGetCmd(opts *GetOptions) error {
65+
opts.config.Profile().APIKey = opts.APIKey
66+
client, err := opts.SearchClient()
67+
if err != nil {
68+
return err
69+
}
70+
71+
key, err := client.GetAPIKey(opts.APIKey)
72+
if err != nil {
73+
return fmt.Errorf("API key %q does not exist", opts.APIKey)
74+
}
75+
76+
p, err := opts.PrintFlags.ToPrinter()
77+
if err != nil {
78+
return err
79+
}
80+
keyResult := shared.JSONKey{
81+
ACL: key.ACL,
82+
CreatedAt: key.CreatedAt,
83+
Description: key.Description,
84+
Indexes: key.Indexes,
85+
MaxQueriesPerIPPerHour: key.MaxQueriesPerIPPerHour,
86+
MaxHitsPerQuery: key.MaxHitsPerQuery,
87+
Referers: key.Referers,
88+
QueryParameters: key.QueryParameters,
89+
Validity: key.Validity,
90+
Value: key.Value,
91+
}
92+
93+
if err := p.Print(opts.IO, keyResult); err != nil {
94+
return err
95+
}
96+
97+
return nil
98+
}

pkg/cmd/apikeys/get/get_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package get
2+
3+
import (
4+
"testing"
5+
6+
"github.com/algolia/algoliasearch-client-go/v3/algolia/search"
7+
"github.com/stretchr/testify/assert"
8+
9+
"github.com/algolia/cli/pkg/httpmock"
10+
"github.com/algolia/cli/test"
11+
)
12+
13+
func Test_runGetCmd(t *testing.T) {
14+
tests := []struct {
15+
name string
16+
key string
17+
wantErr string
18+
}{
19+
{
20+
name: "get a key (success)",
21+
key: "foo",
22+
},
23+
{
24+
name: "get a key (error)",
25+
key: "bar",
26+
wantErr: "API key \"bar\" does not exist",
27+
},
28+
}
29+
30+
for _, tt := range tests {
31+
t.Run(tt.name, func(t *testing.T) {
32+
r := httpmock.Registry{}
33+
if tt.key == "foo" {
34+
r.Register(
35+
httpmock.REST("GET", "1/keys/foo"),
36+
httpmock.JSONResponse(search.Key{
37+
Value: "foo",
38+
Description: "test",
39+
ACL: []string{"*"},
40+
Validity: 0,
41+
MaxHitsPerQuery: 0,
42+
MaxQueriesPerIPPerHour: 0,
43+
Referers: []string{},
44+
}),
45+
)
46+
} else {
47+
r.Register(
48+
httpmock.REST("GET", "1/keys/bar"),
49+
httpmock.ErrorResponse(),
50+
)
51+
}
52+
53+
f, out := test.NewFactory(false, &r, nil, "")
54+
cmd := NewGetCmd(f, nil)
55+
_, err := test.Execute(cmd, tt.key, out)
56+
if err != nil {
57+
assert.Equal(t, tt.wantErr, err.Error())
58+
return
59+
}
60+
})
61+
}
62+
}

pkg/cmd/apikeys/list/list.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/dustin/go-humanize"
1010
"github.com/spf13/cobra"
1111

12+
"github.com/algolia/cli/pkg/cmd/apikeys/shared"
1213
"github.com/algolia/cli/pkg/cmdutil"
1314
"github.com/algolia/cli/pkg/config"
1415
"github.com/algolia/cli/pkg/iostreams"
@@ -54,20 +55,6 @@ func NewListCmd(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
5455
return cmd
5556
}
5657

57-
// JSONKey is the same as search.Key without omitting values
58-
type JSONKey struct {
59-
ACL []string `json:"acl"`
60-
CreatedAt time.Time `json:"createdAt"`
61-
Description string `json:"description"`
62-
Indexes []string `json:"indexes"`
63-
MaxQueriesPerIPPerHour int `json:"maxQueriesPerIPPerHour"`
64-
MaxHitsPerQuery int `json:"maxHitsPerQuery"`
65-
Referers []string `json:"referers"`
66-
QueryParameters search.KeyQueryParams `json:"queryParameters"`
67-
Validity time.Duration `json:"validity"`
68-
Value string `json:"value"`
69-
}
70-
7158
// runListCmd executes the list command
7259
func runListCmd(opts *ListOptions) error {
7360
client, err := opts.SearchClient()
@@ -88,7 +75,7 @@ func runListCmd(opts *ListOptions) error {
8875
return err
8976
}
9077
for _, key := range res.Keys {
91-
keyResult := JSONKey{
78+
keyResult := shared.JSONKey{
9279
ACL: key.ACL,
9380
CreatedAt: key.CreatedAt,
9481
Description: key.Description,

pkg/cmd/apikeys/shared/shared.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package shared
2+
3+
import (
4+
"time"
5+
6+
"github.com/algolia/algoliasearch-client-go/v3/algolia/search"
7+
)
8+
9+
// JSONKey is the same as search.Key without omitting values
10+
type JSONKey struct {
11+
ACL []string `json:"acl"`
12+
CreatedAt time.Time `json:"createdAt"`
13+
Description string `json:"description"`
14+
Indexes []string `json:"indexes"`
15+
MaxQueriesPerIPPerHour int `json:"maxQueriesPerIPPerHour"`
16+
MaxHitsPerQuery int `json:"maxHitsPerQuery"`
17+
Referers []string `json:"referers"`
18+
QueryParameters search.KeyQueryParams `json:"queryParameters"`
19+
Validity time.Duration `json:"validity"`
20+
Value string `json:"value"`
21+
}

0 commit comments

Comments
 (0)