From 7eba80c9a71f53c8ad3d1e80b2472b9ec4723037 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Wed, 7 Oct 2020 02:37:22 +0200 Subject: [PATCH] Move --print to a subcommand 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. --- src/command.rs | 1 + src/command/main.rs | 57 ++++------------------------------------ src/command/show.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 52 deletions(-) create mode 100644 src/command/show.rs diff --git a/src/command.rs b/src/command.rs index 71d0659..96b5301 100644 --- a/src/command.rs +++ b/src/command.rs @@ -6,4 +6,5 @@ pub mod known; pub mod list; pub mod main; pub mod run; +pub mod show; pub mod uninstall; diff --git a/src/command/main.rs b/src/command/main.rs index 9ef0824..c93401f 100644 --- a/src/command/main.rs +++ b/src/command/main.rs @@ -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, @@ -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(); @@ -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(()); } @@ -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()?)?; @@ -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 diff --git a/src/command/show.rs b/src/command/show.rs new file mode 100644 index 0000000..11453cb --- /dev/null +++ b/src/command/show.rs @@ -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(()) +}