Skip to content

Commit

Permalink
Use argument field structs (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevgo authored Oct 21, 2024
1 parent 4a1ac1d commit 5804807
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 104 deletions.
13 changes: 8 additions & 5 deletions src/cli/app_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ use crate::config::{AppName, Version};
/// a request from the user to run a particular app
#[derive(Debug, PartialEq)]
pub struct AppVersion {
pub app: AppName,
pub app_name: AppName,
pub version: Option<Version>,
}

impl AppVersion {
pub fn new<S: AsRef<str>>(token: S) -> Self {
let (app_name, version) = token.as_ref().split_once('@').unwrap_or((token.as_ref(), ""));
let version = if version.is_empty() { None } else { Some(Version::from(version)) };
AppVersion { app: app_name.into(), version }
AppVersion {
app_name: app_name.into(),
version,
}
}
}

Expand All @@ -26,7 +29,7 @@ mod tests {
let give = "[email protected]";
let have = AppVersion::new(give);
let want = AppVersion {
app: AppName::from("shellcheck"),
app_name: AppName::from("shellcheck"),
version: Some(Version::from("0.9.0")),
};
pretty::assert_eq!(have, want);
Expand All @@ -37,7 +40,7 @@ mod tests {
let give = "shellcheck";
let have = AppVersion::new(give);
let want = AppVersion {
app: AppName::from("shellcheck"),
app_name: AppName::from("shellcheck"),
version: None,
};
pretty::assert_eq!(have, want);
Expand All @@ -48,7 +51,7 @@ mod tests {
let give = "shellcheck@";
let have = AppVersion::new(give);
let want = AppVersion {
app: AppName::from("shellcheck"),
app_name: AppName::from("shellcheck"),
version: None,
};
pretty::assert_eq!(have, want);
Expand Down
98 changes: 54 additions & 44 deletions src/cli/args.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{AppVersion, Command};
use crate::cmd::run;
use crate::cmd::{self, available, run, test, update, versions};
use crate::prelude::*;

/// all arguments that can be provided via the CLI
Expand Down Expand Up @@ -93,34 +93,34 @@ pub fn parse(mut cli_args: impl Iterator<Item = String>) -> Result<Args> {
return Ok(Args { command: Command::Setup });
} else if update {
return Ok(Args {
command: Command::Update { verbose },
command: Command::Update(update::Args { verbose }),
});
}
if test {
return Ok(Args {
command: Command::Test {
app: app_version.map(|av| av.app),
command: Command::Test(test::Args {
start_at_app: app_version.map(|av| av.app_name),
verbose,
},
}),
});
}
if let Some(AppVersion { app, version }) = app_version {
if let Some(AppVersion { app_name, version }) = app_version {
if indicate_available {
Ok(Args {
command: Command::Available { app, version, verbose },
command: Command::Available(available::Args { app_name, version, verbose }),
})
} else if which {
Ok(Args {
command: Command::Which { app, version, verbose },
command: Command::Which(cmd::which::Args { app_name, version, verbose }),
})
} else if let Some(amount) = versions {
Ok(Args {
command: Command::Versions { app, amount, verbose },
command: Command::Versions(versions::Args { app_name, amount, verbose }),
})
} else {
Ok(Args {
command: Command::RunApp(run::Args {
app,
app_name,
version,
app_args,
error_on_output,
Expand Down Expand Up @@ -173,18 +173,19 @@ mod tests {
mod available {
use super::super::parse_args;
use crate::cli::{Args, Command};
use crate::cmd::available;
use crate::config::AppName;
use crate::prelude::*;

#[test]
fn with_app() {
let have = parse_args(vec!["rta", "--available", "shellcheck"]);
let want = Ok(Args {
command: Command::Available {
app: AppName::from("shellcheck"),
command: Command::Available(available::Args {
app_name: AppName::from("shellcheck"),
version: None,
verbose: false,
},
}),
});
pretty::assert_eq!(have, want);
}
Expand All @@ -193,11 +194,11 @@ mod tests {
fn with_all_options() {
let have = parse_args(vec!["rta", "--available", "--verbose", "shellcheck"]);
let want = Ok(Args {
command: Command::Available {
app: AppName::from("shellcheck"),
command: Command::Available(available::Args {
app_name: AppName::from("shellcheck"),
version: None,
verbose: true,
},
}),
});
pretty::assert_eq!(have, want);
}
Expand All @@ -222,7 +223,7 @@ mod tests {
let have = parse_args(vec!["rta", "--error-on-output", "app"]);
let want = Ok(Args {
command: Command::RunApp(run::Args {
app: AppName::from("app"),
app_name: AppName::from("app"),
version: None,
app_args: vec![],
error_on_output: true,
Expand All @@ -244,13 +245,17 @@ mod tests {
mod test {
use super::super::parse_args;
use crate::cli::{Args, Command};
use crate::cmd::test;
use crate::config::AppName;

#[test]
fn no_app_no_verbose() {
let have = parse_args(vec!["rta", "--test"]);
let want = Ok(Args {
command: Command::Test { app: None, verbose: false },
command: Command::Test(test::Args {
start_at_app: None,
verbose: false,
}),
});
pretty::assert_eq!(have, want);
}
Expand All @@ -259,7 +264,10 @@ mod tests {
fn no_app_verbose() {
let have = parse_args(vec!["rta", "--test", "--verbose"]);
let want = Ok(Args {
command: Command::Test { app: None, verbose: true },
command: Command::Test(test::Args {
start_at_app: None,
verbose: true,
}),
});
pretty::assert_eq!(have, want);
}
Expand All @@ -268,10 +276,10 @@ mod tests {
fn app_no_verbose() {
let have = parse_args(vec!["rta", "--test", "actionlint"]);
let want = Ok(Args {
command: Command::Test {
app: Some(AppName::from("actionlint")),
command: Command::Test(test::Args {
start_at_app: Some(AppName::from("actionlint")),
verbose: false,
},
}),
});
pretty::assert_eq!(have, want);
}
Expand All @@ -280,10 +288,10 @@ mod tests {
fn app_verbose() {
let have = parse_args(vec!["rta", "--test", "--verbose", "actionlint"]);
let want = Ok(Args {
command: Command::Test {
app: Some(AppName::from("actionlint")),
command: Command::Test(test::Args {
start_at_app: Some(AppName::from("actionlint")),
verbose: true,
},
}),
});
pretty::assert_eq!(have, want);
}
Expand Down Expand Up @@ -320,7 +328,7 @@ mod tests {
let have = parse_args(vec!["rta", "--verbose", "app@2"]);
let want = Ok(Args {
command: Command::RunApp(run::Args {
app: AppName::from("app"),
app_name: AppName::from("app"),
version: Some(Version::from("2")),
app_args: vec![],
error_on_output: false,
Expand All @@ -336,7 +344,7 @@ mod tests {
let have = parse_args(vec!["rta", "-v", "app@2"]);
let want = Ok(Args {
command: Command::RunApp(run::Args {
app: AppName::from("app"),
app_name: AppName::from("app"),
version: Some(Version::from("2")),
app_args: vec![],
error_on_output: false,
Expand Down Expand Up @@ -367,7 +375,7 @@ mod tests {
let have = parse_args(vec!["rta", "--optional", "app@2", "arg1"]);
let want = Ok(Args {
command: Command::RunApp(run::Args {
app: AppName::from("app"),
app_name: AppName::from("app"),
version: Some(Version::from("2")),
app_args: vec![S("arg1")],
error_on_output: false,
Expand Down Expand Up @@ -401,18 +409,19 @@ mod tests {
mod versions {
use super::parse_args;
use crate::cli::{args, Command};
use crate::cmd::versions;
use crate::config::AppName;
use args::Args;

#[test]
fn correct_usage() {
let have = parse_args(vec!["rta", "--versions", "actionlint"]);
let want = Ok(Args {
command: Command::Versions {
app: AppName::from("actionlint"),
command: Command::Versions(versions::Args {
app_name: AppName::from("actionlint"),
amount: 10,
verbose: false,
},
}),
});
pretty::assert_eq!(have, want);
}
Expand All @@ -421,11 +430,11 @@ mod tests {
fn custom_amount() {
let have = parse_args(vec!["rta", "--versions=20", "actionlint"]);
let want = Ok(Args {
command: Command::Versions {
app: AppName::from("actionlint"),
command: Command::Versions(versions::Args {
app_name: AppName::from("actionlint"),
amount: 20,
verbose: false,
},
}),
});
pretty::assert_eq!(have, want);
}
Expand All @@ -441,18 +450,19 @@ mod tests {
mod which {
use super::super::parse_args;
use crate::cli::{Args, Command};
use crate::cmd;
use crate::config::AppName;
use crate::prelude::*;

#[test]
fn with_app() {
let have = parse_args(vec!["rta", "--which", "shellcheck"]);
let want = Ok(Args {
command: Command::Which {
app: AppName::from("shellcheck"),
command: Command::Which(cmd::which::Args {
app_name: AppName::from("shellcheck"),
version: None,
verbose: false,
},
}),
});
pretty::assert_eq!(have, want);
}
Expand All @@ -461,11 +471,11 @@ mod tests {
fn with_all_options() {
let have = parse_args(vec!["rta", "--which", "--verbose", "shellcheck"]);
let want = Ok(Args {
command: Command::Which {
app: AppName::from("shellcheck"),
command: Command::Which(cmd::which::Args {
app_name: AppName::from("shellcheck"),
version: None,
verbose: true,
},
}),
});
pretty::assert_eq!(have, want);
}
Expand All @@ -492,7 +502,7 @@ mod tests {
let have = parse_args(vec!["rta", "app@2"]);
let want = Ok(Args {
command: Command::RunApp(run::Args {
app: AppName::from("app"),
app_name: AppName::from("app"),
version: Some(Version::from("2")),
app_args: vec![],
error_on_output: false,
Expand All @@ -508,7 +518,7 @@ mod tests {
let have = parse_args(vec!["rta", "app@2", "--arg1", "arg2"]);
let want = Ok(Args {
command: Command::RunApp(run::Args {
app: AppName::from("app"),
app_name: AppName::from("app"),
version: Some(Version::from("2")),
app_args: vec![S("--arg1"), S("arg2")],
error_on_output: false,
Expand All @@ -532,7 +542,7 @@ mod tests {
let have = parse_args(vec!["rta", "--verbose", "app@2", "--arg1", "arg2"]);
let want = Ok(Args {
command: Command::RunApp(run::Args {
app: AppName::from("app"),
app_name: AppName::from("app"),
version: Some(Version::from("2")),
app_args: vec![S("--arg1"), S("arg2")],
error_on_output: false,
Expand All @@ -548,7 +558,7 @@ mod tests {
let have = parse_args(vec!["rta", "app@2", "--verbose", "--version"]);
let want = Ok(Args {
command: Command::RunApp(run::Args {
app: AppName::from("app"),
app_name: AppName::from("app"),
version: Some(Version::from("2")),
app_args: vec![S("--verbose"), S("--version")],
error_on_output: false,
Expand Down
13 changes: 6 additions & 7 deletions src/cli/command.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
use crate::cmd::run;
use crate::config::{AppName, Version};
use crate::cmd::{self, available, run, test, update, versions};

/// the main commands that run-this-app can execute
#[derive(Debug, PartialEq)]
pub enum Command {
AppsLong,
AppsShort,
Available { app: AppName, version: Option<Version>, verbose: bool },
Available(available::Args),
RunApp(run::Args),
DisplayHelp,
Setup,
Test { app: Option<AppName>, verbose: bool },
Which { app: AppName, version: Option<Version>, verbose: bool },
Update { verbose: bool },
Test(test::Args),
Which(cmd::which::Args),
Update(update::Args),
Version,
Versions { app: AppName, amount: usize, verbose: bool },
Versions(versions::Args),
}
15 changes: 11 additions & 4 deletions src/cmd/available.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@ use crate::prelude::*;
use crate::{apps, logger, platform, yard};
use std::process::ExitCode;

pub fn available(app_name: &AppName, version: Option<Version>, verbose: bool) -> Result<ExitCode> {
pub fn available(args: Args) -> Result<ExitCode> {
let apps = apps::all();
let app = apps.lookup(app_name)?;
let log = logger::new(verbose);
let app = apps.lookup(&args.app_name)?;
let log = logger::new(args.verbose);
let platform = platform::detect(log)?;
let yard = yard::load_or_create(&yard::production_location()?)?;
let versions = RequestedVersions::determine(app_name, version, &apps)?;
let versions = RequestedVersions::determine(&args.app_name, args.version, &apps)?;
for version in versions {
if load_or_install(app, &version, platform, &yard, log)?.is_some() {
return Ok(ExitCode::SUCCESS);
}
}
Ok(ExitCode::FAILURE)
}

#[derive(Debug, PartialEq)]
pub struct Args {
pub app_name: AppName,
pub version: Option<Version>,
pub verbose: bool,
}
Loading

0 comments on commit 5804807

Please sign in to comment.