Conversation
The function is about API key generation for authentication purposes, making internal/auth the natural home. Export it as GenerateRandomAPIKey so cmd/root.go can call it via the auth package interface. - Add internal/auth/apikey.go with GenerateRandomAPIKey() - Add internal/auth/apikey_test.go (moved from cmd/root_test.go) - Update cmd/root.go: add auth import, remove crypto/rand & encoding/hex - Remove TestGenerateRandomAPIKey from cmd/root_test.go (moved to auth) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR refactors API key generation by moving generateRandomAPIKey out of the CLI entrypoint (internal/cmd) into the authentication layer (internal/auth) as an exported helper, making the functionality reusable across the codebase.
Changes:
- Introduces
auth.GenerateRandomAPIKey()in a newinternal/auth/apikey.go. - Updates the CLI startup path to call
auth.GenerateRandomAPIKey()when no API key is configured. - Migrates the API key generation unit test from
internal/cmd/root_test.gointointernal/auth/apikey_test.go.
Show a summary per file
| File | Description |
|---|---|
| internal/cmd/root.go | Switches API key generation call site to auth.GenerateRandomAPIKey() and removes low-level crypto/encoding imports. |
| internal/cmd/root_test.go | Removes the old generateRandomAPIKey test after migration. |
| internal/auth/apikey.go | Adds exported API key generator implementation to the auth package. |
| internal/auth/apikey_test.go | Adds unit test for auth.GenerateRandomAPIKey() in the auth package. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 4/4 changed files
- Comments generated: 2
| func GenerateRandomAPIKey() (string, error) { | ||
| bytes := make([]byte, 32) | ||
| if _, err := rand.Read(bytes); err != nil { | ||
| return "", fmt.Errorf("failed to generate random API key: %w", err) | ||
| } |
There was a problem hiding this comment.
GenerateRandomAPIKey wraps the rand.Read error with "failed to generate random API key", and the caller in run() wraps again with the same message. If this error ever surfaces, it will read redundantly ("failed...: failed..."). Prefer returning the raw rand.Read error from GenerateRandomAPIKey (or using a more specific/sentinel error) and let callers add context once.
| bytes := make([]byte, 32) | ||
| if _, err := rand.Read(bytes); err != nil { | ||
| return "", fmt.Errorf("failed to generate random API key: %w", err) | ||
| } | ||
| return hex.EncodeToString(bytes), nil |
There was a problem hiding this comment.
Minor naming: bytes is a very generic identifier and can be confused with the standard bytes package in Go codebases. Consider renaming it to something specific like keyBytes or buf to make the intent clearer.
| bytes := make([]byte, 32) | |
| if _, err := rand.Read(bytes); err != nil { | |
| return "", fmt.Errorf("failed to generate random API key: %w", err) | |
| } | |
| return hex.EncodeToString(bytes), nil | |
| keyBytes := make([]byte, 32) | |
| if _, err := rand.Read(keyBytes); err != nil { | |
| return "", fmt.Errorf("failed to generate random API key: %w", err) | |
| } | |
| return hex.EncodeToString(keyBytes), nil |
🤖 This pull request was created by Repo Assist, an automated AI assistant.
Summary
Moves
generateRandomAPIKeyfrominternal/cmd/root.gotointernal/auth/as the exportedGenerateRandomAPIKeyfunction. API key generation is an authentication concern; theinternal/authpackage is the natural home.Changes
internal/auth/apikey.go— new file withGenerateRandomAPIKey()functioninternal/auth/apikey_test.go— test migrated fromcmd/root_test.gointernal/cmd/root.go— call site updated toauth.GenerateRandomAPIKey();crypto/randandencoding/heximports removedinternal/cmd/root_test.go—TestGenerateRandomAPIKeyremoved (now lives inauthpackage)Motivation
crypto/rand,encoding/hex) from the CLI entry-point packageGenerateRandomAPIKeyreusable by any package that importsinternal/authTest Status
go.modrequires Go 1.25.0, and the toolchain download is blocked by the network firewall.make agent-finishedcould not complete. The changes are syntactically verified by code review:Tests will run normally in the GitHub Actions CI environment where Go 1.25.0 is available.