|
| 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 | +} |
0 commit comments