Closed zzaakiirr closed 1 month ago
[!IMPORTANT]
Review skipped
Auto reviews are disabled on base/target branches other than the default branch.
Please check the settings in the CodeRabbit UI or the
.coderabbit.yaml
file in this repository. To trigger a single review, invoke the@coderabbitai review
command.You can disable this status message by setting the
reviews.review_status
tofalse
in the CodeRabbit configuration file.
The updates introduce significant enhancements to the cpflow
CLI by adding a new generate
command for Terraform configuration, improving command discovery and handling of subcommands, and structuring subcommands more effectively. These changes streamline the user experience, making it easier to manage infrastructure as code and facilitating better organization within the command-line application. Testing enhancements ensure that the new functionalities work as intended.
File | Change Summary |
---|---|
docs/commands.md |
Added generate command documentation for generating Terraform configuration files from controlplane.yml . |
lib/command/base.rb |
Enhanced all_commands to support recursive file discovery and proper namespace handling for commands. |
lib/command/base_sub_command.rb |
Introduced BaseSubCommand class for structured definition of subcommands, including methods for command banners and subcommand prefixes. |
lib/command/terraform/generate.rb |
Created Generate class within Command::Terraform for generating Terraform configurations; includes command metadata and placeholders for future implementation. |
lib/cpflow.rb |
Added subcommand_names and klass_for methods to dynamically handle subcommands and improve command processing. |
spec/cpflow_spec.rb |
Added tests to verify correct handling of subcommands and their help output in the CLI. |
spec/support/command_helpers.rb |
Enhanced run_cpflow_command to manage the global $PROGRAM_NAME ; introduced package_name method for better code organization. |
sequenceDiagram
participant User
participant CLI
participant Command
participant Terraform
User->>CLI: cpflow terraform generate
CLI->>Command: Initialize Generate Command
Command->>Terraform: Generate configuration files
Terraform-->>Command: Return generated files
Command-->>CLI: Display success message
CLI-->>User: Show confirmation of files generated
🐰 In the warren where changes bloom,
New commands bring joy, dispelling gloom.
With subcommands now in their place,
Infrastructure flows with grace.
A hop, a skip, we generate with glee,
Oh, what a wondrous CLI, come see! 🎉
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?
@coderabbitai review
Just a partial review so far, but I almost wonder if it wouldn't be easier to just use : like we do for ps:start, ps:stop, etc. Like terraform:generate. Are subcommands preferrable?
I missed that 🥲
I still prefer subcommands because user can get all terraform
commands by invoking cpflow terraform --help
. The problem with :
approach is that user needs to invoke cpflow help
for each command in this namespace
Another issue with this approach is class names which must include subcommand namespace in class names (class Ps<CommandName>
for example), declaring separate module is better (IMHO)
I won't push on subcommand approach @rafaelgomesxyz @borela WDYT?
@zzaakiirr I am in favor of your design as it is more common in UNIX for CLIs that support subcommands.
@justin808 @rafaelgomesxyz @dzirtusss @zzaakiirr I would prefer to change the loading logic to just reopen the CLI class for each command and declare them like Thor docs says, e.g.:
# command_1.rb
class CLI < Thor
desc "usage", "desc"
long_desc "long desc"
def command_1
end
end
# command_2.rb
class CLI < Thor
desc "usage", "desc"
long_desc "long desc"
def command_2
end
end
...
By reopening the class this way, you can still share state, methods etc... and still follow Thor's design.
@borela We have a bunch of custom logic that benefits from the current loading logic, as you can see in https://github.com/shakacode/control-plane-flow/blob/main/lib/cpflow.rb#L168.
What are the benefits of switching to your proposed logic?
Thor doesn't really have a design for defining commands in separate files, so I'm not sure what you mean by that.
I think we should keep it the way it is.
@rafaelgomesxyz, it looks like you are re-implementing Thor's features; Which features that custom logic implement that Thor already doesn't support? Validations? That could be extracted to a method that accepts a value and pattern to validate against.
@borela We're not though, we just have a layer on top of Thor to make things easier. It's not just validations, but also handling of deprecated commands and special args / options cases, as well as setting some fallback values here and there.
Regardless, if we were to reopen the class in every command file, we'd have to repeat all of this logic for each file. Sure, we could move some logic to common helpers, but we'd still have repetition, which is why this way seems better to me. I don't see any benefits in switching to the way you proposed.
@rafaelgomesxyz The code for each command would be linear and less magical. A little bit of repetition is fine if the end result is simpler to read.
In an example scenario, command x is having a validation issue. I will go to the command x file and see how validation is being performed (either directly or by calling an accessory function) inside the command's method.
That does not happen with the current design, it is not immediately obvious where simple stuff like validations are happening or that those options are being validated. In contrast, other projects using thor just do simple ||= for fallback values and validate the option on the command itself, which again can be extracted to a method and called by the command.
Let's see what Sergey and Justin think, but I still think we should keep it as is.
I think once you understand the constants defined at the top of the command classes and how options are defined in Base
, it becomes pretty straightforward to figure out how something is being validated.
Another thing I like about the current approach is that we have a somewhat centralized place for all Thor stuff, which allows us to have a better separation of concerns, and detaches our app from Thor. It would be a lot easier to adapt the code if we wanted to replace Thor with something else - I know Justin was considering moving to an npm
package so we have both the CPLN CLI and our CLI in the same place, and although that wouldn't help too much in this case, because we'd still need to convert from Ruby to JavaScript, I think it would definitely make things easier.
We also have some configs set on the class level based on the commands, which benefits from the current approach:
These cannot be set individually, so we'd still need a way to go through all command classes looking for matches.
@rafaelgomesxyz Looking at the source, just build image has ACCEPTS_EXTRA_OPTIONS set to true, and it looks like it only does this because it forwards them to docker.
Why not declare the docker options supported? The error message will be better and when somebody does cpflow build-image --help
they will see them too.
@rafaelgomesxyz if zakir pr is following the current pattern and working we can merge this pr and I can move my suggestions to an issue.
What does this PR do?
This PR adds possibility to add subcommands to
cpflow
cli tool and addscpflow terraform generate
command which does nothing (for now)Note
CommandHelpers#run_cpflow_command
so thatcpflow help
showscpflow <command_name>
i/orspec <command_name>
Command::Terraform::Generate
will be implemented in feature PRsScreenshots
Summary by CodeRabbit
New Features
generate
command to automate Terraform configuration file creation in the CLI.Bug Fixes
Tests
Chores