Skip to content

Commit

Permalink
feat: migrate apikeys create
Browse files Browse the repository at this point in the history
  • Loading branch information
kai687 committed Jan 31, 2025
1 parent 9687d3c commit 4ef6662
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
21 changes: 13 additions & 8 deletions pkg/cmd/apikeys/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"time"

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

Expand All @@ -20,7 +20,7 @@ type CreateOptions struct {
config config.IConfig
IO *iostreams.IOStreams

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

ACL []string
Description string
Expand All @@ -34,7 +34,7 @@ func NewCreateCmd(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
opts := &CreateOptions{
IO: f.IOStreams,
config: f.Config,
SearchClient: f.SearchClient,
SearchClient: f.V4SearchClient,
}
cmd := &cobra.Command{
Use: "create",
Expand Down Expand Up @@ -143,19 +143,24 @@ func NewCreateCmd(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co

// runCreateCmd executes the create command
func runCreateCmd(opts *CreateOptions) error {
key := search.Key{
ACL: opts.ACL,
var acls []search.Acl
for _, a := range opts.ACL {
acls = append(acls, search.Acl(a))
}
validity := int32(opts.Validity.Seconds())
key := search.ApiKey{
Acl: acls,
Indexes: opts.Indices,
Validity: opts.Validity,
Validity: &validity,
Referers: opts.Referers,
Description: opts.Description,
Description: &opts.Description,
}

client, err := opts.SearchClient()
if err != nil {
return err
}
res, err := client.AddAPIKey(key)
res, err := client.AddApiKey(client.NewApiAddApiKeyRequest(&key))
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/cmd/apikeys/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import (
"testing"
"time"

"github.com/algolia/algoliasearch-client-go/v3/algolia/search"
"github.com/algolia/algoliasearch-client-go/v4/algolia/search"
"github.com/google/shlex"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/algolia/cli/pkg/cmdutil"
"github.com/algolia/cli/pkg/httpmock"
"github.com/algolia/cli/pkg/httpmock/v4"
"github.com/algolia/cli/pkg/iostreams"
"github.com/algolia/cli/test"
"github.com/algolia/cli/test/v4"
)

func TestNewCreateCmd(t *testing.T) {
Expand Down Expand Up @@ -107,7 +107,7 @@ func Test_runCreateCmd(t *testing.T) {
r := httpmock.Registry{}
r.Register(
httpmock.REST("POST", "1/keys"),
httpmock.JSONResponse(search.CreateKeyRes{Key: "foo"}),
httpmock.JSONResponse(search.AddApiKeyResponse{Key: "foo"}),
)

f, out := test.NewFactory(tt.isTTY, &r, nil, "")
Expand Down

0 comments on commit 4ef6662

Please sign in to comment.