Skip to content

Commit

Permalink
Merge pull request #8 from onshi/feature/expose_help_from_rails
Browse files Browse the repository at this point in the history
feature: expose `--help` from rails
  • Loading branch information
rafaelfranca authored Mar 28, 2024
2 parents f3f7a79 + 6b9fddd commit 8235904
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 8 deletions.
14 changes: 14 additions & 0 deletions src/docker_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
22 changes: 16 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}
42 changes: 40 additions & 2 deletions src/rails_new.rs
Original file line number Diff line number Diff line change
@@ -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`
Expand All @@ -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<Commands>,
}

#[derive(Subcommand)]
pub enum Commands {
/// Prints `rails new --help`
RailsHelp {},
}

#[cfg(test)]
Expand All @@ -35,4 +44,33 @@ mod tests {

Ok(())
}

#[test]
fn default_values() -> Result<(), Box<dyn std::error::Error>> {
use clap::CommandFactory;

let m = Cli::command().get_matches_from(vec!["rails-new", "my_app"]);

let ruby_version = m.get_one::<String>("ruby_version").unwrap();
let rails_version = m.get_one::<String>("rails_version").unwrap();

assert_eq!(ruby_version, "3.2.3");
assert_eq!(rails_version, "7.1.3");

Ok(())
}

#[test]
fn rails_help() -> Result<(), Box<dyn std::error::Error>> {
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(())
}
}

0 comments on commit 8235904

Please sign in to comment.