@@ -3,6 +3,7 @@ package cmd
33import (
44 "bufio"
55 "context"
6+ "errors"
67 "fmt"
78 "io"
89 "os"
@@ -15,15 +16,14 @@ import (
1516 "golang.org/x/text/cases"
1617 "golang.org/x/text/language"
1718
18- "github.com/timescale/tiger-cli/internal/tiger/analytics"
1919 "github.com/timescale/tiger-cli/internal/tiger/api"
2020 "github.com/timescale/tiger-cli/internal/tiger/common"
2121 "github.com/timescale/tiger-cli/internal/tiger/config"
2222 "github.com/timescale/tiger-cli/internal/tiger/util"
2323)
2424
25- // validateAndGetAuthInfo can be overridden for testing
26- var validateAndGetAuthInfo = validateAndGetAuthInfoImpl
25+ // validateAPIKey can be overridden for testing
26+ var validateAPIKey = common . ValidateAPIKey
2727
2828// nextStepsMessage is the message shown after successful login
2929const nextStepsMessage = `
@@ -118,9 +118,15 @@ Examples:
118118 // Combine the keys in the format "public:secret" for storage
119119 apiKey := fmt .Sprintf ("%s:%s" , creds .publicKey , creds .secretKey )
120120
121+ // Create API client
122+ client , err := api .NewTigerClient (cfg , apiKey )
123+ if err != nil {
124+ return fmt .Errorf ("failed to create client: %w" , err )
125+ }
126+
121127 // Validate the API key and get auth info by calling the /auth/info endpoint
122128 fmt .Fprintln (cmd .OutOrStdout (), "Validating API key..." )
123- authInfo , err := validateAndGetAuthInfo (cmd .Context (), cfg , apiKey )
129+ authInfo , err := validateAPIKey (cmd .Context (), cfg , client )
124130 if err != nil {
125131 return fmt .Errorf ("API key validation failed: %w" , err )
126132 }
@@ -178,28 +184,20 @@ func buildStatusCmd() *cobra.Command {
178184 RunE : func (cmd * cobra.Command , args []string ) error {
179185 cmd .SilenceUsage = true
180186
181- // Get config
182- cfg , err := config .Load ()
183- if err != nil {
184- return fmt .Errorf ("failed to load config: %w" , err )
185- }
186-
187- apiKey , _ , err := config .GetCredentials ()
187+ // Load config and API client
188+ cfg , err := common .LoadConfig (cmd .Context ())
188189 if err != nil {
190+ if errors .Is (err , config .ErrNotLoggedIn ) {
191+ return common .ExitWithCode (common .ExitAuthenticationError , config .ErrNotLoggedIn )
192+ }
189193 return err
190194 }
191195
192- // Create API client
193- client , err := api .NewTigerClient (cfg , apiKey )
194- if err != nil {
195- return fmt .Errorf ("failed to create API client: %w" , err )
196- }
197-
198196 // Make API call to get auth information
199197 ctx , cancel := context .WithTimeout (cmd .Context (), 30 * time .Second )
200198 defer cancel ()
201199
202- resp , err := client .GetAuthInfoWithResponse (ctx )
200+ resp , err := cfg . Client .GetAuthInfoWithResponse (ctx )
203201 if err != nil {
204202 return fmt .Errorf ("failed to get auth information: %w" , err )
205203 }
@@ -316,45 +314,3 @@ func promptForCredentials(ctx context.Context, consoleURL string, creds credenti
316314
317315 return creds , nil
318316}
319-
320- // validateAndGetAuthInfoImpl validates the API key and returns authentication information
321- // by calling the /auth/info endpoint.
322- func validateAndGetAuthInfoImpl (ctx context.Context , cfg * config.Config , apiKey string ) (* api.AuthInfo , error ) {
323- client , err := api .NewTigerClient (cfg , apiKey )
324- if err != nil {
325- return nil , fmt .Errorf ("failed to create client: %w" , err )
326- }
327-
328- ctx , cancel := context .WithTimeout (ctx , 10 * time .Second )
329- defer cancel ()
330-
331- // Call the /auth/info endpoint to validate credentials and get auth info
332- resp , err := client .GetAuthInfoWithResponse (ctx )
333- if err != nil {
334- return nil , fmt .Errorf ("API call failed: %w" , err )
335- }
336-
337- // Check the response status
338- if resp .StatusCode () != 200 {
339- if resp .JSON4XX != nil {
340- return nil , resp .JSON4XX
341- }
342- return nil , fmt .Errorf ("unexpected API response: %d" , resp .StatusCode ())
343- }
344-
345- if resp .JSON200 == nil {
346- return nil , fmt .Errorf ("empty response from API" )
347- }
348-
349- authInfo := resp .JSON200
350-
351- // Identify the user with analytics
352- a := analytics .New (cfg , client , authInfo .ApiKey .Project .Id )
353- a .Identify (
354- analytics .Property ("userId" , authInfo .ApiKey .IssuingUser .Id ),
355- analytics .Property ("email" , string (authInfo .ApiKey .IssuingUser .Email )),
356- analytics .Property ("planType" , authInfo .ApiKey .Project .PlanType ),
357- )
358-
359- return authInfo , nil
360- }
0 commit comments