-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cmd): Add apikeys get command (#153)
- Loading branch information
1 parent
119d337
commit 01a0555
Showing
5 changed files
with
185 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |