Skip to content

Commit b3b16d2

Browse files
authored
chore: auth login --skip-verify (#30)
1 parent 702577c commit b3b16d2

3 files changed

Lines changed: 34 additions & 8 deletions

File tree

cmd/auth_login.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
)
1414

1515
var loginName string
16+
var loginSkipVerify bool
1617

1718
var loginCmd = &cobra.Command{
1819
Use: "login",
@@ -34,23 +35,37 @@ var loginCmd = &cobra.Command{
3435
return fmt.Errorf("API key cannot be empty")
3536
}
3637

37-
result, err := runAuthLogin(apiKey, loginName)
38+
result, err := runAuthLogin(apiKey, loginName, loginSkipVerify)
3839
if err != nil {
3940
return err
4041
}
4142

4243
if isJSONOutput() {
43-
return printJSON(cmd.OutOrStdout(), Result{Success: true, Message: fmt.Sprintf("Authenticated as team: %s", result.TeamName)})
44+
msg := fmt.Sprintf("API key saved as %q", loginName)
45+
if result != nil {
46+
msg = fmt.Sprintf("Authenticated as team: %s", result.TeamName)
47+
}
48+
return printJSON(cmd.OutOrStdout(), Result{Success: true, Message: msg})
49+
}
50+
if result != nil {
51+
fmt.Fprintf(cmd.OutOrStdout(), "API key saved as %q. Authenticated as team: %s\n", loginName, result.TeamName)
52+
} else {
53+
fmt.Fprintf(cmd.OutOrStdout(), "API key saved as %q.\n", loginName)
4454
}
45-
fmt.Fprintf(cmd.OutOrStdout(), "API key saved as %q. Authenticated as team: %s\n", loginName, result.TeamName)
4655
return nil
4756
},
4857
}
4958

50-
func runAuthLogin(apiKey, name string) (*api.APIKeyResponse, error) {
59+
func runAuthLogin(apiKey, name string, skipVerify bool) (*api.APIKeyResponse, error) {
5160
if name == "" {
5261
return nil, errors.New("use --name to give this key a name")
5362
}
63+
if skipVerify {
64+
if err := config.Save(apiKey, name); err != nil {
65+
return nil, err
66+
}
67+
return nil, nil
68+
}
5469
result, err := api.NewClient(config.EndpointURL(), apiKey, debugFlag).GetAPIKey()
5570
if err != nil {
5671
return nil, fmt.Errorf("API key verification failed: %w", err)
@@ -63,5 +78,6 @@ func runAuthLogin(apiKey, name string) (*api.APIKeyResponse, error) {
6378

6479
func init() {
6580
loginCmd.Flags().StringVarP(&loginName, "name", "n", "", "Name for this API key (e.g. my-team)")
81+
loginCmd.Flags().BoolVar(&loginSkipVerify, "skip-verify", false, "Save the API key without verifying it")
6682
authCmd.AddCommand(loginCmd)
6783
}

cmd/auth_login_test.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
func TestRunAuthLogin(t *testing.T) {
1212
t.Run("saves key and returns team name", func(t *testing.T) {
1313
serveJSON(t, http.StatusOK, `{"teamName":"Acme"}`)
14-
result, err := runAuthLogin("test-key", "acme")
14+
result, err := runAuthLogin("test-key", "acme", false)
1515
if err != nil {
1616
t.Fatalf("unexpected error: %v", err)
1717
}
@@ -23,17 +23,27 @@ func TestRunAuthLogin(t *testing.T) {
2323

2424
t.Run("returns error on api failure", func(t *testing.T) {
2525
serveJSON(t, http.StatusUnauthorized, `{"error":"Invalid API key"}`)
26-
_, err := runAuthLogin("bad-key", "acme")
26+
_, err := runAuthLogin("bad-key", "acme", false)
2727
if err == nil {
2828
t.Fatal("expected error, got nil")
2929
}
3030
})
3131

3232
t.Run("returns error when name is empty", func(t *testing.T) {
3333
serveJSON(t, http.StatusOK, `{"teamName":"Acme"}`)
34-
_, err := runAuthLogin("test-key", "")
34+
_, err := runAuthLogin("test-key", "", false)
3535
if err == nil {
3636
t.Fatal("expected error, got nil")
3737
}
3838
})
39+
40+
t.Run("skip-verify saves key without calling api", func(t *testing.T) {
41+
result, err := runAuthLogin("test-key", "acme", true)
42+
if err != nil {
43+
t.Fatalf("unexpected error: %v", err)
44+
}
45+
if result != nil {
46+
t.Errorf("expected nil result, got %+v", result)
47+
}
48+
})
3949
}

cmd/auth_logout_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
func TestRunAuthLogout(t *testing.T) {
99
t.Run("succeeds", func(t *testing.T) {
1010
serveJSON(t, http.StatusOK, `{}`)
11-
runAuthLogin("test-key", "acme")
11+
runAuthLogin("test-key", "acme", true)
1212
if err := runAuthLogout("acme"); err != nil {
1313
t.Fatalf("unexpected error: %v", err)
1414
}

0 commit comments

Comments
 (0)