Merge pull request #1 from SergioGasquez/configurable-config-path

Minor formatting changes
This commit is contained in:
Arne Beer 2023-01-04 14:43:42 +01:00 committed by GitHub
commit 2400f9a13b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 19 deletions

View File

@ -146,6 +146,9 @@ Xtensa Rust toolchain will be installed under `<rustup_home>/toolchains/esp`.
Usage: espup install [OPTIONS] Usage: espup install [OPTIONS]
Options: Options:
-p, --config-path <CONFIG_PATH>
Path to where the espup configuration file will be written to
-d, --default-host <DEFAULT_HOST> -d, --default-host <DEFAULT_HOST>
Target triple of the host Target triple of the host
@ -217,6 +220,7 @@ Options:
Usage: espup uninstall [OPTIONS] Usage: espup uninstall [OPTIONS]
Options: Options:
-p, --config-path <CONFIG_PATH> Path to where the espup configuration file will be written to
-l, --log-level <LOG_LEVEL> Verbosity level of the logs [default: info] [possible values: debug, info, warn, error] -l, --log-level <LOG_LEVEL> Verbosity level of the logs [default: info] [possible values: debug, info, warn, error]
-h, --help Print help information -h, --help Print help information
-V, --version Print version information -V, --version Print version information
@ -228,6 +232,8 @@ Options:
Usage: espup update [OPTIONS] Usage: espup update [OPTIONS]
Options: Options:
-p, --config-path <CONFIG_PATH>
Path to where the espup configuration file will be written to
-d, --default-host <DEFAULT_HOST> -d, --default-host <DEFAULT_HOST>
Target triple of the host [possible values: x86_64-unknown-linux-gnu, aarch64-unknown-linux-gnu, x86_64-pc-windows-msvc, x86_64-pc-windows-gnu, x86_64-apple-darwin, aarch64-apple-darwin] Target triple of the host [possible values: x86_64-unknown-linux-gnu, aarch64-unknown-linux-gnu, x86_64-pc-windows-msvc, x86_64-pc-windows-gnu, x86_64-apple-darwin, aarch64-apple-darwin]
-l, --log-level <LOG_LEVEL> -l, --log-level <LOG_LEVEL>

View File

@ -11,11 +11,6 @@ use std::{
path::PathBuf, path::PathBuf,
}; };
pub struct ConfigFile {
pub path: PathBuf,
pub config: Config,
}
/// Deserialized contents of a configuration file /// Deserialized contents of a configuration file
#[derive(Debug, Deserialize, Serialize, Default, Clone)] #[derive(Debug, Deserialize, Serialize, Default, Clone)]
pub struct Config { pub struct Config {
@ -37,10 +32,15 @@ pub struct Config {
pub xtensa_rust: Option<XtensaRust>, pub xtensa_rust: Option<XtensaRust>,
} }
pub struct ConfigFile {
pub path: PathBuf,
pub config: Config,
}
impl ConfigFile { impl ConfigFile {
/// Construcs a new config file with the given path and config
pub fn new(config_path: &Option<PathBuf>, config: Config) -> Result<Self, Error> { pub fn new(config_path: &Option<PathBuf>, config: Config) -> Result<Self, Error> {
let config_path = config_path.clone().unwrap_or(Self::get_config_path()?); let config_path = config_path.clone().unwrap_or(Self::get_config_path()?);
Ok(ConfigFile { Ok(ConfigFile {
path: config_path, path: config_path,
config, config,
@ -50,7 +50,6 @@ impl ConfigFile {
/// Load the config from config file /// Load the config from config file
pub fn load(config_path: &Option<PathBuf>) -> Result<Self, Error> { pub fn load(config_path: &Option<PathBuf>) -> Result<Self, Error> {
let config_path = config_path.clone().unwrap_or(Self::get_config_path()?); let config_path = config_path.clone().unwrap_or(Self::get_config_path()?);
let config: Config = if let Ok(data) = read(&config_path) { let config: Config = if let Ok(data) = read(&config_path) {
toml::from_slice(&data).map_err(|_| Error::FailedToDeserialize)? toml::from_slice(&data).map_err(|_| Error::FailedToDeserialize)?
} else { } else {
@ -58,8 +57,7 @@ impl ConfigFile {
config_path.to_string_lossy().into_owned(), config_path.to_string_lossy().into_owned(),
)); ));
}; };
Self::new(&Some(config_path), config)
ConfigFile::new(&Some(config_path), config)
} }
/// Save the config to file /// Save the config to file

View File

@ -57,6 +57,9 @@ pub enum SubCommand {
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
pub struct InstallOpts { pub struct InstallOpts {
/// Path to where the espup configuration file will be written to.
#[arg(short = 'p', long)]
pub config_path: Option<PathBuf>,
/// Target triple of the host. /// Target triple of the host.
#[arg(short = 'd', long, required = false, value_parser = ["x86_64-unknown-linux-gnu", "aarch64-unknown-linux-gnu", "x86_64-pc-windows-msvc", "x86_64-pc-windows-gnu" , "x86_64-apple-darwin" , "aarch64-apple-darwin"])] #[arg(short = 'd', long, required = false, value_parser = ["x86_64-unknown-linux-gnu", "aarch64-unknown-linux-gnu", "x86_64-pc-windows-msvc", "x86_64-pc-windows-gnu" , "x86_64-apple-darwin" , "aarch64-apple-darwin"])]
pub default_host: Option<String>, pub default_host: Option<String>,
@ -80,9 +83,6 @@ pub struct InstallOpts {
/// Relative or full path for the export file that will be generated. If no path is provided, the file will be generated under home directory (https://docs.rs/dirs/latest/dirs/fn.home_dir.html). /// Relative or full path for the export file that will be generated. If no path is provided, the file will be generated under home directory (https://docs.rs/dirs/latest/dirs/fn.home_dir.html).
#[arg(short = 'f', long)] #[arg(short = 'f', long)]
pub export_file: Option<PathBuf>, pub export_file: Option<PathBuf>,
/// The path to which the espup configuration file will be written to.
#[arg(short = 'p', long)]
pub config_path: Option<PathBuf>,
/// Comma or space list of extra crates to install. /// Comma or space list of extra crates to install.
#[arg(short = 'c', long, required = false, value_parser = Crate::parse_crates)] #[arg(short = 'c', long, required = false, value_parser = Crate::parse_crates)]
pub extra_crates: Option<HashSet<Crate>>, pub extra_crates: Option<HashSet<Crate>>,
@ -111,6 +111,9 @@ pub struct InstallOpts {
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
pub struct UpdateOpts { pub struct UpdateOpts {
/// Path to where the espup configuration file will be written to.
#[arg(short = 'p', long)]
pub config_path: Option<PathBuf>,
/// Target triple of the host. /// Target triple of the host.
#[arg(short = 'd', long, required = false, value_parser = ["x86_64-unknown-linux-gnu", "aarch64-unknown-linux-gnu", "x86_64-pc-windows-msvc", "x86_64-pc-windows-gnu" , "x86_64-apple-darwin" , "aarch64-apple-darwin"])] #[arg(short = 'd', long, required = false, value_parser = ["x86_64-unknown-linux-gnu", "aarch64-unknown-linux-gnu", "x86_64-pc-windows-msvc", "x86_64-pc-windows-gnu" , "x86_64-apple-darwin" , "aarch64-apple-darwin"])]
pub default_host: Option<String>, pub default_host: Option<String>,
@ -120,19 +123,16 @@ pub struct UpdateOpts {
/// Xtensa Rust toolchain version. /// Xtensa Rust toolchain version.
#[arg(short = 'v', long, value_parser = XtensaRust::parse_version)] #[arg(short = 'v', long, value_parser = XtensaRust::parse_version)]
pub toolchain_version: Option<String>, pub toolchain_version: Option<String>,
/// The path at which the espup configuration file can be found.
#[arg(short = 'p', long)]
pub config_path: Option<PathBuf>,
} }
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
pub struct UninstallOpts { pub struct UninstallOpts {
/// Path to where the espup configuration file will be written to.
#[arg(short = 'p', long)]
pub config_path: Option<PathBuf>,
/// Verbosity level of the logs. /// Verbosity level of the logs.
#[arg(short = 'l', long, default_value = "info", value_parser = ["debug", "info", "warn", "error"])] #[arg(short = 'l', long, default_value = "info", value_parser = ["debug", "info", "warn", "error"])]
pub log_level: String, pub log_level: String,
/// The path at which the espup configuration file can be found.
#[arg(short = 'p', long)]
pub config_path: Option<PathBuf>,
} }
/// Installs the Rust for ESP chips environment /// Installs the Rust for ESP chips environment
@ -278,7 +278,7 @@ async fn install(args: InstallOpts) -> Result<()> {
}; };
let config_file = ConfigFile::new(&args.config_path, config)?; let config_file = ConfigFile::new(&args.config_path, config)?;
info!( info!(
"{} Saving configuration file at {:?}", "{} Storing configuration file at '{:?}'",
emoji::WRENCH, emoji::WRENCH,
config_file.path config_file.path
); );