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..5235a1c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,8 +3,8 @@ // use std::process::Command; mod docker_client; mod rails_new; -use rails_new::Cli; -use std::io::Write; +use rails_new::{Cli, Commands}; +use std::{io::Write, process::Command}; use clap::Parser; @@ -40,10 +40,20 @@ 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"); + let mut command: Command; + + match &cli.command { + Some(Commands::RailsHelp {}) => { + 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 $@ + command = DockerClient::run_image(&ruby_version, &rails_version, cli.args) + } + } + + let status = command.status().expect("Failed to execute process"); assert!(status.success()); } diff --git a/src/rails_new.rs b/src/rails_new.rs index a7469f1..3faac87 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)] @@ -35,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(()) + } }