Skip to content

Commit

Permalink
Move --print to a subcommand
Browse files Browse the repository at this point in the history
Not sure why I made it an option in the first place. A subcommand works
better for this, and keeps the interface consistent with the other
commands.
  • Loading branch information
yorickpeterse committed Oct 7, 2020
1 parent 644d008 commit 7eba80c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 52 deletions.
1 change: 1 addition & 0 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ pub mod known;
pub mod list;
pub mod main;
pub mod run;
pub mod show;
pub mod uninstall;
57 changes: 5 additions & 52 deletions src/command/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::command::install;
use crate::command::known;
use crate::command::list;
use crate::command::run as run_cmd;
use crate::command::show;
use crate::command::uninstall;
use crate::config::{
bin_directory, cache_directory, config_directory, data_directory,
Expand All @@ -30,21 +31,13 @@ Commands:
default Set the default version
clean Clean up temporary data
implode Removes all versions and temporary data
show Prints the value of a setting.
Examples:
ivm install 0.8.0 # Install version 0.8.0
ivm uninstall 0.8.0 # Uninstall version 0.8.0
ivm run 0.8.0 foo # Run the command `foo` with version 0.8.0
You can use the -p/--print option to display what directory ivm uses for a
certain setting. The following settings are available:
data The data directory
bin The directory for symbolic links to executables
cache The directory for storing temporary data
install The directory containing all installed versions
config The directory containing configuration files";
ivm run 0.8.0 foo # Run the command `foo` with version 0.8.0";

pub fn run() -> Result<(), Error> {
let args: Vec<_> = env::args().collect();
Expand All @@ -53,16 +46,10 @@ pub fn run() -> Result<(), Error> {
options.parsing_style(ParsingStyle::StopAtFirstFree);
options.optflag("h", "help", "Shows this help message");
options.optflag("v", "version", "Prints the version number");
options.optopt(
"p",
"print",
"Prints the path to a directory used by ivm",
"KIND",
);

let matches = options.parse(&args[1..])?;

if matches.opt_present("h") {
if matches.opt_present("h") || matches.free.is_empty() {
usage!(&options, USAGE);
return Ok(());
}
Expand All @@ -72,41 +59,6 @@ pub fn run() -> Result<(), Error> {
return Ok(());
}

match matches.opt_str("p").as_ref().map(|s| s.as_str()) {
Some("data") => {
println!("{}", data_directory()?.to_string_lossy());
return Ok(());
}
Some("bin") => {
println!("{}", bin_directory()?.to_string_lossy());
return Ok(());
}
Some("cache") => {
println!("{}", cache_directory()?.to_string_lossy());
return Ok(());
}
Some("install") => {
println!("{}", install_directory()?.to_string_lossy());
return Ok(());
}
Some("config") => {
println!("{}", config_directory()?.to_string_lossy());
return Ok(());
}
Some(kind) => {
return Err(Error::generic(format!(
"{} is not a valid value for --print",
kind
)));
}
_ => {}
}

if matches.free.is_empty() {
usage!(&options, USAGE);
return Ok(());
}

// We create all necessary directories here so we don't have to do this
// every time some piece of code needs these to exist.
create_dir_all(cache_directory()?)?;
Expand All @@ -127,6 +79,7 @@ pub fn run() -> Result<(), Error> {
Some("default") => default::run(cmd_args),
Some("clean") => clean::run(cmd_args),
Some("implode") => implode::run(cmd_args),
Some("show") => show::run(cmd_args),
Some(command) => Err(Error::generic(format!(
"The command {:?} is not valid",
command
Expand Down
64 changes: 64 additions & 0 deletions src/command/show.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use crate::config::{
bin_directory, cache_directory, config_directory, data_directory,
downloads_directory, install_directory,
};
use crate::error::Error;
use getopts::Options;

const USAGE: &str = "ivm show [OPTIONS] [SETTING]
Prints the value of a setting.
Available settings:
data The data directory
bin The directory for symbolic links to executables
cache The directory for storing temporary data
install The directory containing all installed versions
config The directory containing configuration files
downloads The directory containing downloaded files";

pub fn run(arguments: &[String]) -> Result<(), Error> {
let mut options = Options::new();

options.optflag("h", "help", "Shows this help message");

let matches = options.parse(arguments)?;

if matches.opt_present("h") {
usage!(&options, USAGE);
return Ok(());
}

match matches.free.get(0).map(|s| s.as_str()) {
Some("data") => {
println!("{}", data_directory()?.to_string_lossy());
}
Some("bin") => {
println!("{}", bin_directory()?.to_string_lossy());
}
Some("cache") => {
println!("{}", cache_directory()?.to_string_lossy());
}
Some("install") => {
println!("{}", install_directory()?.to_string_lossy());
}
Some("config") => {
println!("{}", config_directory()?.to_string_lossy());
}
Some("downloads") => {
println!("{}", downloads_directory()?.to_string_lossy());
}
Some(setting) => {
return Err(Error::generic(format!(
"The setting {} doesn't exist",
setting
)));
}
_ => {
return Err(Error::generic("You must specify a setting name"));
}
}

Ok(())
}

0 comments on commit 7eba80c

Please sign in to comment.