Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: expose --help from rails #8

Merged
merged 4 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>,
}
Copy link
Member

Choose a reason for hiding this comment

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

Can we write a test for this?


#[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(())
}
}
Loading