From 73eb39d5bb31380627de901f92c35420695c4255 Mon Sep 17 00:00:00 2001 From: Jan Makara Date: Wed, 27 Mar 2024 02:33:41 +0100 Subject: [PATCH 1/4] feature: expose `--help` from rails --- src/docker_client.rs | 14 ++++++++++++++ src/main.rs | 27 ++++++++++++++++++++------- src/rails_new.rs | 13 +++++++++++-- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/docker_client.rs b/src/docker_client.rs index 0ed7f6e..5f61945 100644 --- a/src/docker_client.rs +++ b/src/docker_client.rs @@ -50,4 +50,18 @@ impl DockerClient { command } + + pub fn get_help(ruby_version: &str, rails_version: &str) -> Command { + let mut command = Command::new("docker"); + + command + .arg("run") + .arg("--rm") + .arg(format!("rails-new-{}-{}", ruby_version, rails_version)) + .arg("rails") + .arg("new") + .arg("--help"); + + command + } } diff --git a/src/main.rs b/src/main.rs index e018574..57fe5f2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ // use std::process::Command; mod docker_client; mod rails_new; -use rails_new::Cli; +use rails_new::{Cli, Commands}; use std::io::Write; use clap::Parser; @@ -40,10 +40,23 @@ fn main() { assert!(status.success()); - // Run the image with docker run -v $(pwd):/$(pwd) -w $(pwd) rails-new-$RUBY_VERSION-$RAILS_VERSION rails new $@ - let status = DockerClient::run_image(&ruby_version, &rails_version, cli.args) - .status() - .expect("Failed to execute process"); - - assert!(status.success()); + match &cli.command { + Some(Commands::RailsHelp {}) => { + let mut child = DockerClient::get_help(&ruby_version, &rails_version) + .spawn() + .expect("Failed to execute process"); + let status = child.wait().expect("failed to wait on child"); + + assert!(status.success()); + } + + None => { + // Run the image with docker run -v $(pwd):/$(pwd) -w $(pwd) rails-new-$RUBY_VERSION-$RAILS_VERSION rails new $@ + let status = DockerClient::run_image(&ruby_version, &rails_version, cli.args) + .status() + .expect("Failed to execute process"); + + assert!(status.success()); + } + } } diff --git a/src/rails_new.rs b/src/rails_new.rs index a7469f1..a98d690 100644 --- a/src/rails_new.rs +++ b/src/rails_new.rs @@ -1,7 +1,7 @@ -use clap::Parser; +use clap::{Parser, Subcommand}; #[derive(Parser)] -#[command(version, about, long_about = None)] +#[command(version, about, long_about = None, subcommand_negates_reqs = true)] pub struct Cli { #[clap(trailing_var_arg = true, required = true)] /// arguments passed to `rails new` @@ -10,6 +10,15 @@ pub struct Cli { pub ruby_version: String, #[clap(long, short = 'r', default_value = "7.1.3")] pub rails_version: String, + + #[command(subcommand)] + pub command: Option, +} + +#[derive(Subcommand)] +pub enum Commands { + /// Prints `rails new --help` + RailsHelp {}, } #[cfg(test)] From f8345c1a17b63781e29e3bccbc74091fc186e922 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Thu, 28 Mar 2024 17:33:50 +0000 Subject: [PATCH 2/4] Add test for default values of options and the help subcommand --- src/rails_new.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/rails_new.rs b/src/rails_new.rs index a98d690..3faac87 100644 --- a/src/rails_new.rs +++ b/src/rails_new.rs @@ -44,4 +44,33 @@ mod tests { Ok(()) } + + #[test] + fn default_values() -> Result<(), Box> { + use clap::CommandFactory; + + let m = Cli::command().get_matches_from(vec!["rails-new", "my_app"]); + + let ruby_version = m.get_one::("ruby_version").unwrap(); + let rails_version = m.get_one::("rails_version").unwrap(); + + assert_eq!(ruby_version, "3.2.3"); + assert_eq!(rails_version, "7.1.3"); + + Ok(()) + } + + #[test] + fn rails_help() -> Result<(), Box> { + use clap::CommandFactory; + + let m = Cli::command().get_matches_from(vec!["rails-new", "rails-help"]); + + match m.subcommand_name() { + Some("rails-help") => {} + _ => panic!("Expected subcommand 'rails-help'"), + } + + Ok(()) + } } From a3261dc56297bf87c8a7301ad75c38aec7c08c55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Thu, 28 Mar 2024 17:35:32 +0000 Subject: [PATCH 3/4] Avoid subprocess spawning in the help command --- src/main.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 57fe5f2..cfae5b9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,10 +42,9 @@ fn main() { match &cli.command { Some(Commands::RailsHelp {}) => { - let mut child = DockerClient::get_help(&ruby_version, &rails_version) - .spawn() + let status = DockerClient::get_help(&ruby_version, &rails_version) + .status() .expect("Failed to execute process"); - let status = child.wait().expect("failed to wait on child"); assert!(status.success()); } From 6b9fdddd8d64f9f35c4ffc37f992ab74192100e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Thu, 28 Mar 2024 17:44:33 +0000 Subject: [PATCH 4/4] Remove duplication --- src/main.rs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index cfae5b9..5235a1c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ mod docker_client; mod rails_new; use rails_new::{Cli, Commands}; -use std::io::Write; +use std::{io::Write, process::Command}; use clap::Parser; @@ -40,22 +40,20 @@ fn main() { assert!(status.success()); + let mut command: Command; + match &cli.command { Some(Commands::RailsHelp {}) => { - let status = DockerClient::get_help(&ruby_version, &rails_version) - .status() - .expect("Failed to execute process"); - - assert!(status.success()); + command = DockerClient::get_help(&ruby_version, &rails_version) } None => { // Run the image with docker run -v $(pwd):/$(pwd) -w $(pwd) rails-new-$RUBY_VERSION-$RAILS_VERSION rails new $@ - let status = DockerClient::run_image(&ruby_version, &rails_version, cli.args) - .status() - .expect("Failed to execute process"); - - assert!(status.success()); + command = DockerClient::run_image(&ruby_version, &rails_version, cli.args) } } + + let status = command.status().expect("Failed to execute process"); + + assert!(status.success()); }