-
Notifications
You must be signed in to change notification settings - Fork 0
feat(loo-4729): api-key command
#2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f2f1937
add api client
notnmeyer f1765e5
/api-key
notnmeyer bc095a2
test an api key before persisting it
notnmeyer be2f595
support for env files in dev
notnmeyer 4311d1d
fix up govuln errors
notnmeyer d5279b6
add a default 5s timeout to client requests
notnmeyer 0c6eba9
we dont use this
notnmeyer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 @@ | ||
| LOOPS_API_KEY=your-key |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -25,7 +25,8 @@ go.work | |
| go.work.sum | ||
|
|
||
| # env file | ||
| .env | ||
| .env* | ||
| !.env.template | ||
|
|
||
| # Editor/IDE | ||
| # .idea/ | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -4,6 +4,8 @@ version: "3" | |
|
|
||
| tasks: | ||
| default: | ||
| dotenv: | ||
| - .env.{{.ENV}} | ||
| cmds: | ||
| - go run ./... {{.CLI_ARGS}} | ||
|
|
||
|
|
||
This file contains hidden or 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,33 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/loops-so/cli/internal/api" | ||
| "github.com/loops-so/cli/internal/config" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var apiKeyCmd = &cobra.Command{ | ||
| Use: "api-key", | ||
| Short: "Validate your API key", | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| cfg, err := config.Load() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| client := api.NewClient(cfg.EndpointURL, cfg.APIKey) | ||
| result, err := client.GetAPIKey() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| fmt.Printf("Valid API key for team: %s\n", result.TeamName) | ||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| func init() { | ||
| rootCmd.AddCommand(apiKeyCmd) | ||
| } |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| module github.com/loops-so/cli | ||
|
|
||
| go 1.25.0 | ||
| go 1.26.1 | ||
|
|
||
| require ( | ||
| github.com/spf13/cobra v1.10.2 | ||
|
|
||
This file contains hidden or 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,39 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| ) | ||
|
|
||
| type APIKeyResponse struct { | ||
| TeamName string `json:"teamName"` | ||
| } | ||
|
|
||
| func (c *Client) GetAPIKey() (*APIKeyResponse, error) { | ||
| req, err := c.newRequest(http.MethodGet, "/api-key") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| resp, err := c.httpClient.Do(req) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("request failed: %w", err) | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| if resp.StatusCode == http.StatusUnauthorized { | ||
| return nil, fmt.Errorf("invalid API key") | ||
| } | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return nil, fmt.Errorf("unexpected status: %d", resp.StatusCode) | ||
| } | ||
|
|
||
| var result APIKeyResponse | ||
| if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { | ||
| return nil, fmt.Errorf("failed to decode response: %w", err) | ||
| } | ||
|
|
||
| return &result, nil | ||
| } |
This file contains hidden or 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,84 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestGetAPIKey(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| statusCode int | ||
| body string | ||
| wantErr string | ||
| wantTeam string | ||
| }{ | ||
| { | ||
| name: "success", | ||
| statusCode: http.StatusOK, | ||
| body: `{"success":true,"teamName":"Acme"}`, | ||
| wantTeam: "Acme", | ||
| }, | ||
| { | ||
| name: "unauthorized", | ||
| statusCode: http.StatusUnauthorized, | ||
| body: `{"success":false,"error":"Invalid API key"}`, | ||
| wantErr: "invalid API key", | ||
| }, | ||
| { | ||
| name: "unexpected status", | ||
| statusCode: http.StatusInternalServerError, | ||
| body: ``, | ||
| wantErr: "unexpected status: 500", | ||
| }, | ||
| { | ||
| name: "invalid json", | ||
| statusCode: http.StatusOK, | ||
| body: `not json`, | ||
| wantErr: "failed to decode response", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| w.WriteHeader(tt.statusCode) | ||
| w.Write([]byte(tt.body)) | ||
| })) | ||
| defer server.Close() | ||
|
|
||
| client := NewClient(server.URL, "test-key") | ||
| result, err := client.GetAPIKey() | ||
|
|
||
| if tt.wantErr != "" { | ||
| if err == nil { | ||
| t.Fatalf("expected error containing %q, got nil", tt.wantErr) | ||
| } | ||
| if !contains(err.Error(), tt.wantErr) { | ||
| t.Errorf("error = %q, want it to contain %q", err.Error(), tt.wantErr) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if result.TeamName != tt.wantTeam { | ||
| t.Errorf("TeamName = %q, want %q", result.TeamName, tt.wantTeam) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func contains(s, substr string) bool { | ||
| return len(s) >= len(substr) && (s == substr || len(substr) == 0 || | ||
| func() bool { | ||
| for i := 0; i <= len(s)-len(substr); i++ { | ||
| if s[i:i+len(substr)] == substr { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| }()) | ||
| } |
This file contains hidden or 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,31 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/http" | ||
| "time" | ||
| ) | ||
|
|
||
| type Client struct { | ||
| baseURL string | ||
| apiKey string | ||
| httpClient *http.Client | ||
| } | ||
|
|
||
| func NewClient(baseURL, apiKey string) *Client { | ||
| return &Client{ | ||
| baseURL: baseURL, | ||
| apiKey: apiKey, | ||
| httpClient: &http.Client{Timeout: 5 * time.Second}, | ||
| } | ||
| } | ||
|
|
||
| func (c *Client) newRequest(method, path string) (*http.Request, error) { | ||
| url := fmt.Sprintf("%s%s", c.baseURL, path) | ||
| req, err := http.NewRequest(method, url, nil) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| req.Header.Set("Authorization", "Bearer "+c.apiKey) | ||
| return req, nil | ||
| } |
This file contains hidden or 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,50 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestNewRequest(t *testing.T) { | ||
| client := NewClient("https://example.com/api/v1", "test-key") | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| method string | ||
| path string | ||
| }{ | ||
| {"GET", http.MethodGet, "/api-key"}, | ||
| {"POST", http.MethodPost, "/some-resource"}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| req, err := client.newRequest(tt.method, tt.path) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
|
|
||
| if req.Method != tt.method { | ||
| t.Errorf("method = %q, want %q", req.Method, tt.method) | ||
| } | ||
|
|
||
| wantURL := "https://example.com/api/v1" + tt.path | ||
| if req.URL.String() != wantURL { | ||
| t.Errorf("url = %q, want %q", req.URL.String(), wantURL) | ||
| } | ||
|
|
||
| wantAuth := "Bearer test-key" | ||
| if got := req.Header.Get("Authorization"); got != wantAuth { | ||
| t.Errorf("Authorization = %q, want %q", got, wantAuth) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestNewRequest_InvalidURL(t *testing.T) { | ||
| client := NewClient("://bad-url", "test-key") | ||
| _, err := client.newRequest(http.MethodGet, "/path") | ||
| if err == nil { | ||
| t.Error("expected error for invalid URL, got nil") | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think in general I prefer
[noun] [verb]command patterns for this kind of thing. So like on auth I'd probably doauth login,auth logout, andauth statusThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
login was added in #1. can make a linear for it though, wont address it here. i was debating originally whether to have the top level be
author not. 👍