Skip to content

Conversation

@eynopv
Copy link
Owner

@eynopv eynopv commented Apr 8, 2025

Summary by CodeRabbit

  • New Features

    • Introduced a unified configuration approach that streamlines command execution by consolidating request building, authentication, and output formatting.
  • Refactor

    • Simplified workflows for processing commands, managing headers, and handling templates to deliver a more robust and consistent execution experience.
    • Added a new Builder struct to encapsulate configuration for requests, improving modularity and usability.

@coderabbitai
Copy link

coderabbitai bot commented Apr 8, 2025

Walkthrough

This pull request refactors the command execution and authentication flow by introducing a new builder pattern. The runCommandFunction is updated to accept a single *builder.Builder parameter, consolidating previously separate arguments such as variables, headers, and client configuration. The changes remove the old runRequest function and update tests and authentication functions to use the new template.Template instead of request.Template. Additionally, a new file is added for the builder implementation, and several import paths and package names have been adjusted accordingly.

Changes

File(s) Change Summary
cmd/root.go
cmd/run.go
Refactored command execution by replacing direct parameter passing with a new builder.Builder struct; updated the runCommandFunction signature and removed the runRequest function; headers are now handled via a defaultHeaders map.
pkg/authentication/api.go
pkg/authentication/authentication.go
pkg/authentication/basic.go
pkg/authentication/bearer.go
pkg/authentication/api_test.go
pkg/authentication/authentication_test.go
pkg/authentication/basic_test.go
pkg/authentication/bearer_test.go
Updated function signatures and test cases by replacing *request.Template with *template.Template; adjusted import statements and YAML package aliasing; renamed variables in tests to reflect the new template package usage.
pkg/builder/builder.go New file added that defines the Builder struct with methods to build the client, template, request, authentication, and printer instances, centralizing configuration and construction logic.
pkg/client/client.go Modified the import path for the authentication package from pkg/request/authentication to pkg/authentication to consolidate dependency references.
pkg/template/template.go
pkg/template/template_test.go
Changed the package name from request to template; updated the signature of the Parse method to return a pointer to request.Request; introduced a new import for the request package to support the parsing operation.

Sequence Diagram(s)

sequenceDiagram
  participant U as User
  participant RC as rootCmd
  participant B as Builder
  participant RCF as runCommandFunction
  participant C as Client
  participant P as Printer

  U->>RC: Execute command
  RC->>B: Instantiate Builder (with config, template path, variables, headers)
  RC->>RCF: Call runCommandFunction(&Builder)
  RCF->>B: BuildTemplate(), BuildRequest(), BuildAuth(), BuildClient(), BuildPrinter()
  B-->>RCF: Return built components
  RCF->>C: Execute Request
  C-->>RCF: Return Response/Error
  RCF->>P: Print Response/Error
Loading

Possibly related PRs

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
pkg/authentication/basic_test.go (1)

127-135: Variable naming could be improved for clarity.

The variable name template is now used for an HTTP request object, which could be confusing since template is also used for template objects elsewhere in the code. Consider using a more descriptive name like httpRequest or req to clearly indicate this is an HTTP request.

-	template, err := http.NewRequest(http.MethodGet, "", nil)
-	assert.NotNil(t, template)
-	assert.NoError(t, err)
-	basic.Apply(template)
-	username, password, ok := template.BasicAuth()
+	req, err := http.NewRequest(http.MethodGet, "", nil)
+	assert.NotNil(t, req)
+	assert.NoError(t, err)
+	basic.Apply(req)
+	username, password, ok := req.BasicAuth()
pkg/builder/builder.go (1)

57-60: Consider simplifying BuildPrinter method

The method creates a temporary variable and then returns its address, which could be simplified.

func (b *Builder) BuildPrinter() *printer.Printer {
-	prntr := printer.NewPrinter(b.ClientConfig.PrinterConfig)
-	return &prntr
+	p := printer.NewPrinter(b.ClientConfig.PrinterConfig)
+	return &p
}

Or even more concisely:

func (b *Builder) BuildPrinter() *printer.Printer {
-	prntr := printer.NewPrinter(b.ClientConfig.PrinterConfig)
-	return &prntr
+	p := &printer.NewPrinter(b.ClientConfig.PrinterConfig)
+	return p
}
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5eaadaa and b4b44fd.

📒 Files selected for processing (14)
  • cmd/root.go (2 hunks)
  • cmd/run.go (1 hunks)
  • pkg/authentication/api.go (1 hunks)
  • pkg/authentication/api_test.go (5 hunks)
  • pkg/authentication/authentication.go (2 hunks)
  • pkg/authentication/authentication_test.go (6 hunks)
  • pkg/authentication/basic.go (1 hunks)
  • pkg/authentication/basic_test.go (9 hunks)
  • pkg/authentication/bearer.go (1 hunks)
  • pkg/authentication/bearer_test.go (5 hunks)
  • pkg/builder/builder.go (1 hunks)
  • pkg/client/client.go (1 hunks)
  • pkg/template/template.go (4 hunks)
  • pkg/template/template_test.go (1 hunks)
🧰 Additional context used
🧬 Code Definitions (9)
pkg/authentication/authentication_test.go (1)
pkg/template/template.go (1)
  • Template (18-18)
pkg/authentication/authentication.go (1)
pkg/template/template.go (1)
  • Template (18-18)
pkg/authentication/bearer_test.go (3)
pkg/template/template.go (1)
  • Template (18-18)
pkg/request/request.go (1)
  • NewRequest (94-115)
internal/assert/assert.go (3)
  • NotNil (65-71)
  • NoError (41-47)
  • Equal (9-15)
pkg/template/template.go (1)
pkg/request/request.go (3)
  • Request (21-26)
  • RequestData (14-19)
  • NewRequest (94-115)
cmd/root.go (4)
pkg/builder/builder.go (1)
  • Builder (13-18)
pkg/client/client.go (1)
  • ClientConfig (20-24)
pkg/variables/variables.go (1)
  • Variables (12-12)
pkg/request/request.go (1)
  • StringOrStringList (51-51)
pkg/authentication/bearer.go (1)
pkg/template/template.go (1)
  • Template (18-18)
cmd/run.go (1)
pkg/builder/builder.go (1)
  • Builder (13-18)
pkg/authentication/api_test.go (3)
pkg/template/template.go (1)
  • Template (18-18)
pkg/request/request.go (1)
  • NewRequest (94-115)
internal/assert/assert.go (3)
  • NotNil (65-71)
  • NoError (41-47)
  • Equal (9-15)
pkg/builder/builder.go (7)
cmd/root.go (2)
  • ClientConfig (53-53)
  • Variables (54-54)
pkg/request/request.go (2)
  • StringOrStringList (51-51)
  • Request (21-26)
pkg/client/client.go (2)
  • Client (14-18)
  • NewClient (26-32)
pkg/template/template.go (2)
  • Template (18-18)
  • NewTemplate (22-31)
pkg/utils/map_utils.go (1)
  • CombineMaps (31-38)
pkg/authentication/authentication.go (2)
  • Auth (22-25)
  • NewAuth (31-59)
pkg/printer/printer.go (3)
  • Printer (23-27)
  • NewPrinter (29-39)
  • PrinterConfig (16-21)
🔇 Additional comments (50)
pkg/authentication/api.go (2)

8-11: Import changes look good.

The updated imports properly reflect the transition from request.Template to template.Template while adding a YAML alias for improved readability.


19-19: Function signature appropriately updated.

The parameter type change from *request.Template to *template.Template is consistent with the builder pattern refactoring described in the PR.

pkg/client/client.go (1)

8-8: Import path correctly updated.

Good job updating the import path to directly use the authentication package rather than accessing it through the request package. This aligns with the architectural changes in the PR.

pkg/authentication/authentication.go (2)

8-11: Import changes look good.

The updated imports properly reflect the transition from request.Template to template.Template while adding a YAML alias for improved readability.


31-31: Function signature appropriately updated.

The parameter type change from *request.Template to *template.Template is consistent with the builder pattern refactoring described in the PR.

pkg/authentication/bearer.go (2)

8-11: Import changes look good.

The updated imports properly reflect the transition from request.Template to *template.Template while adding a YAML alias for improved readability.


18-18: Function signature appropriately updated.

The parameter type change from *request.Template to *template.Template is consistent with the builder pattern refactoring described in the PR.

pkg/template/template_test.go (1)

1-1: Package name updated correctly.

The package name has been changed from request to template to align with the directory structure and the broader refactoring effort. This change ensures consistency with the new builder pattern implementation.

pkg/authentication/basic.go (3)

8-8: Improved import with alias.

Using an alias for the yaml package improves code readability and prevents potential name conflicts.


11-11: Import path updated correctly.

The import has been updated from the request package to the template package, aligning with the broader refactoring effort.


19-19: Function signature updated to use the new package.

The parameter type has been changed from *request.Template to *template.Template to align with the new package structure. This is consistent with the builder pattern refactoring.

pkg/authentication/authentication_test.go (7)

8-8: Import path updated correctly.

The import has been updated from the request package to the template package, which is consistent with the refactoring changes.


13-16: Template instantiation updated.

The template instantiation has been correctly updated to use template.Template instead of request.Template.


26-32: JSON template instantiation updated.

The JSON template creation has been correctly updated to use template.Template.


42-46: No-auth template instantiation updated.

Template instantiation for the "no auth" test case has been correctly updated.


55-60: Basic auth template instantiation updated.

Template instantiation for the basic authentication test case has been correctly updated.


70-74: Bearer auth template instantiation updated.

Template instantiation for the bearer authentication test case has been correctly updated.


84-89: API auth template instantiation updated.

Template instantiation for the API authentication test case has been correctly updated.

pkg/authentication/basic_test.go (8)

9-9: Import path updated correctly.

The import has been updated from the request package to the template package, aligning with the refactoring changes.


20-21: Template instantiation updated.

The template instantiation has been correctly updated to use template.Template instead of request.Template.


38-39: JSON template instantiation updated.

The JSON template creation has been correctly updated.


50-51: Invalid template instantiation updated.

The invalid template instantiation has been correctly updated.


64-65: Template without auth instantiation updated.

Template instantiation for the "template without auth" test case has been correctly updated.


79-80: Only username template instantiation updated.

Template instantiation for the "only username" test case has been correctly updated.


95-96: Only password template instantiation updated.

Template instantiation for the "only password" test case has been correctly updated.


112-113: Empty username and password template instantiation updated.

Template instantiation for the "empty username and password" test case has been correctly updated.

pkg/template/template.go (4)

1-1: Good package name change to better reflect the file's purpose.

The package name change from request to template better aligns with the file's role - handling templates rather than requests directly.


11-11: Clean import addition for the request package.

The import of the request package is now necessary as this module uses types from that package after the refactoring.


38-40: Clean method signature change to use the request package types.

The Parse method now correctly returns a pointer to request.Request, and the requestData variable has been updated to use request.RequestData type. This change properly separates the template-handling logic from the request data structures.


50-50: Function call updated to use the request package.

The NewRequest function call has been updated to use the request package, which is consistent with the overall refactoring of separating the template and request handling.

pkg/authentication/bearer_test.go (3)

8-8: Import correctly updated to use the template package.

This change is consistent with the package restructuring in the codebase.


13-13: Template initialization updated to use the template package.

The template initialization statements have been correctly updated to use template.Template instead of request.Template, aligning with the package restructuring.

Also applies to: 26-26, 42-42, 51-51


69-76: Variable name changed for better clarity.

Renaming the variable from request to template improves readability by making it clearer what the variable represents in the context of the test.

cmd/root.go (3)

13-15: Good package imports to support the builder pattern implementation.

The necessary imports have been added to support the builder pattern implementation in the code.


40-47: Well-implemented builder pattern.

The change from passing multiple parameters to using a builder.Builder instance is a good architectural improvement. This approach centralizes the parameters, making the code more maintainable, easier to read, and simpler to extend in the future.


53-58: Good variable restructuring and header configuration.

The variable declarations have been reorganized for better readability, and the introduction of defaultHeaders centralizes header configuration. The User-Agent header is appropriately set with the application name and version.

pkg/authentication/api_test.go (3)

8-8: Import correctly updated to use the template package.

This change is consistent with the package restructuring in the codebase.


13-13: Template initialization updated to use the template package.

The template initialization statements have been correctly updated to use template.Template instead of request.Template, aligning with the package restructuring.

Also applies to: 28-28, 46-46, 55-55


74-81: Variable name changed for better clarity.

Renaming the variable from request to template improves readability by making it clearer what the variable represents in the context of the test.

pkg/builder/builder.go (6)

13-18: Good use of the builder pattern to encapsulate configuration

The Builder struct cleanly consolidates all the parameters needed for request execution (client config, template path, variables, and headers), which improves modularity and reduces parameter coupling.


20-22: Good implementation of BuildClient method

Simple and straightforward wrapper around the client creation.


24-26: Good implementation of BuildTemplate method

Clean delegation to the template creation function.


28-44: BuildRequest method has proper error handling and flow

The method follows a logical sequence: build template, interpolate variables, parse request, and combine headers. Error handling is appropriate at each step.


41-41: Good use of utility function for combining maps

Using utils.CombineMaps to merge headers from both sources is a clean approach.


46-55: BuildAuth method follows similar pattern as BuildRequest

Consistent approach with proper error handling.

cmd/run.go (5)

7-7: Appropriate import for the builder package

The import matches the new implementation pattern.


10-10: Function signature simplified with Builder pattern

Replacing multiple parameters with a single builder parameter significantly improves the function's interface.


11-32: Good refactoring of runCommandFunction

The implementation now delegates creation responsibilities to the builder, making the code more maintainable and easier to follow. Error handling remains appropriate.


27-27: Simplification of error output

The error output is now more direct and consistent.


31-32: Consistent printer initialization via builder

Using the builder to create the printer maintains consistency with the rest of the function.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (4)
pkg/builder/builder.go (4)

13-18: Consider encapsulating these fields for controlled access.
Currently, the Builder fields are publicly exposed. For a robust Builder pattern, you might want to keep them private and provide builder-like setters to maintain control over how these fields are initialized or modified.


24-26: Add safety checks for template path validity.
Consider handling an empty or invalid TemplatePath before calling template.NewTemplate to avoid ambiguities or runtime errors.


28-44: Clarify header precedence when merging.
Merging headers with utils.CombineMaps(b.Headers, r.Headers) gives the request’s headers priority over the builder’s. Verify this is the intended behavior. Otherwise, reverse the argument order to ensure the builder’s headers take precedence.


46-55: Optional enforcement of authentication.
This function naturally returns nil if the template has no auth. If your use case requires authentication to be present, consider throwing an error or providing a default no-op auth object for clarity.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b4b44fd and 0042593.

📒 Files selected for processing (2)
  • pkg/authentication/basic_test.go (9 hunks)
  • pkg/builder/builder.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • pkg/authentication/basic_test.go
🧰 Additional context used
🧬 Code Definitions (1)
pkg/builder/builder.go (7)
cmd/root.go (2)
  • ClientConfig (53-53)
  • Variables (54-54)
pkg/request/request.go (2)
  • StringOrStringList (51-51)
  • Request (21-26)
pkg/client/client.go (2)
  • Client (14-18)
  • NewClient (26-32)
pkg/template/template.go (2)
  • Template (18-18)
  • NewTemplate (22-31)
pkg/utils/map_utils.go (1)
  • CombineMaps (31-38)
pkg/authentication/authentication.go (2)
  • Auth (22-25)
  • NewAuth (31-59)
pkg/printer/printer.go (3)
  • Printer (23-27)
  • NewPrinter (29-39)
  • PrinterConfig (16-21)
🔇 Additional comments (2)
pkg/builder/builder.go (2)

20-22: Straightforward client creation.
This function cleanly instantiates a new client with the provided config.


57-60: Printer creation looks good.
The code succinctly constructs a printer instance with the correct configuration.

@eynopv eynopv merged commit 459345c into master Apr 8, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants