feat: API get workspace#10
Conversation
|
Caution Review failedThe pull request is closed. WalkthroughA new Cobra command is introduced in the workspace package to retrieve workspace details. The command accepts either a workspace ID (validated as a UUID) or a slug, but not both simultaneously. It retrieves API credentials from the configuration to initialize the API client. Depending on the provided flag, it invokes the appropriate method to fetch workspace details and processes the response, including error handling for various failure scenarios. Changes
Sequence Diagram(s)sequenceDiagram
participant U as User
participant C as CLI Command
participant CFG as Config Service
participant API as API Client
U->>C: Execute get workspace command (--id or --slug)
C->>CFG: Retrieve API URL and Key
CFG-->>C: Return credentials
C->>API: Initialize API client with credentials
alt Workspace ID provided
C->>API: Validate UUID and fetch workspace by ID
else Slug provided
C->>API: Fetch workspace by slug
end
API-->>C: Return workspace details or error
C-->>U: Display result
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (4)
cmd/ctrlc/root/api/get/workspace/workspace.go (4)
41-46: Add verification for API configuration presence.You're retrieving API URL and key from viper, but not verifying if these values exist before creating the client.
apiURL := viper.GetString("url") apiKey := viper.GetString("api-key") + +if apiURL == "" { + return fmt.Errorf("API URL not configured. Run 'ctrlc config set url <api-url>' to set it") +} + +if apiKey == "" { + return fmt.Errorf("API key not configured. Run 'ctrlc config set api-key <your-api-key>' to set it") +} + client, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey)
62-67: Consider adding slug format validation.If there's a specific format expected for workspaceSlug, consider adding validation before making the API call.
// Get workspace by slug +if !isValidSlug(workspaceSlug) { + return fmt.Errorf("invalid workspace slug format: %s", workspaceSlug) +} + resp, err := client.GetWorkspaceBySlug(cmd.Context(), workspaceSlug)You would need to add a helper function like:
func isValidSlug(slug string) bool { // Implement validation logic based on your slug format requirements // For example: only alphanumeric characters and hyphens slugRegex := regexp.MustCompile(`^[a-z0-9]([a-z0-9\-]*[a-z0-9])?$`) return slugRegex.MatchString(slug) }
71-73: Consider adding more descriptive flag usage information.The flag definitions could benefit from more detailed descriptions to guide users.
// Add flags -cmd.Flags().StringVar(&workspaceId, "id", "", "ID of the workspace") -cmd.Flags().StringVar(&workspaceSlug, "slug", "", "Slug of the workspace") +cmd.Flags().StringVar(&workspaceId, "id", "", "UUID of the workspace to retrieve") +cmd.Flags().StringVar(&workspaceSlug, "slug", "", "URL-friendly slug of the workspace to retrieve")
32-69: Consider refactoring the RunE function for better readability.The RunE function is somewhat lengthy. Consider extracting the workspace retrieval logic into separate helper functions to improve readability and maintainability.
RunE: func(cmd *cobra.Command, args []string) error { if workspaceId == "" && workspaceSlug == "" { return fmt.Errorf("either --id or --slug must be provided") } if workspaceId != "" && workspaceSlug != "" { return fmt.Errorf("--id and --slug are mutually exclusive") } apiURL := viper.GetString("url") apiKey := viper.GetString("api-key") if apiURL == "" { return fmt.Errorf("API URL not configured. Run 'ctrlc config set url <api-url>' to set it") } if apiKey == "" { return fmt.Errorf("API key not configured. Run 'ctrlc config set api-key <your-api-key>' to set it") } client, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey) if err != nil { return fmt.Errorf("failed to create API client: %w", err) } + return getWorkspace(cmd, client, workspaceId, workspaceSlug) +}, + +// Helper function to get workspace by either ID or slug +func getWorkspace(cmd *cobra.Command, client *api.ClientWithResponses, workspaceId, workspaceSlug string) error { if workspaceId != "" { // Get workspace by ID wsId, err := uuid.Parse(workspaceId) if err != nil { return fmt.Errorf("invalid workspace ID: %w", err) } resp, err := client.GetWorkspace(cmd.Context(), wsId) if err != nil { return fmt.Errorf("failed to get workspace by ID: %w", err) } return cliutil.HandleOutput(cmd, resp) } // Get workspace by slug resp, err := client.GetWorkspaceBySlug(cmd.Context(), workspaceSlug) if err != nil { return fmt.Errorf("failed to get workspace by slug: %w", err) } return cliutil.HandleOutput(cmd, resp) -},
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
cmd/ctrlc/root/api/get/workspace/workspace.go(1 hunks)
🔇 Additional comments (5)
cmd/ctrlc/root/api/get/workspace/workspace.go (5)
3-12: LGTM! Good import organization.The imports are organized appropriately, separating standard library and third-party packages with a blank line, which improves readability.
14-31: Command definition looks clean with good documentation.The command definition includes detailed examples that demonstrate different usage patterns, which is excellent for user experience.
33-39: Good input validation checks.The mutual exclusivity check between ID and slug is well-implemented, ensuring users don't provide conflicting parameters.
48-60: UUID validation properly implemented.Good job validating the workspace ID as a UUID before making the API call. The error messages are clear and helpful.
14-76:Details
✅ Verification successful
Consider adding global template flag.
The example shows how to use a template flag, but it's not defined in the command. Either add the template flag or update the example to match the actual implementation.
🏁 Script executed:
#!/bin/bash # Check if template flag is already defined globally in the cliutil package grep -r "template" --include="*.go" ./internal/cliutil/Length of output: 557
Global Template Flag Confirmed
The global
--templateflag is already registered and handled in the CLI’s global output utility (seeinternal/cliutil/output.go), so the example usage is valid as is—no local flag addition is required in the workspace command.
Summary by CodeRabbit