diff --git a/src/bin/detee-cli.rs b/src/bin/detee-cli.rs index 22ec6c5..c35630b 100644 --- a/src/bin/detee-cli.rs +++ b/src/bin/detee-cli.rs @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 -use clap::builder::PossibleValue; +use clap::builder::{PossibleValue, ValueParser}; use clap::{Arg, Command}; use detee_cli::general::cli_handler::{ handle_account, handle_completion, handle_operators, handle_packagers, @@ -642,6 +642,8 @@ fn clap_cmd() -> Command { .subcommand(Command::new("ssh-pubkey-path").about("define the SSH pubkey that you want to use") .arg( Arg::new("path") + .value_name("FILE") + .value_parser(ValueParser::path_buf()) .help("supply path to your public SSH key") .required(true) ) diff --git a/src/config.rs b/src/config.rs index 949f825..510110e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -187,9 +187,7 @@ impl Config { } pub fn cli_dir_path() -> Result { - let dir = std::env::var(CONFIG_OVERRIDE_PATH_ENV) - .unwrap_or_else(|_| Self::home_dir() + ("/.detee/cli")); - + let dir = Self::override_path(); if !Path::new(&dir).exists() { warn!("Could not config dir. Creating {dir}"); std::fs::create_dir_all(dir.clone())?; @@ -197,6 +195,12 @@ impl Config { Ok(dir) } + fn override_path() -> String { + let dir = std::env::var(CONFIG_OVERRIDE_PATH_ENV) + .unwrap_or_else(|_| Self::home_dir() + ("/.detee/cli")); + dir + } + fn config_path() -> Result { let config_path = Self::cli_dir_path()? + ("/cli-config.yaml"); Ok(config_path) @@ -308,7 +312,13 @@ impl Config { if self.ssh_key_path.is_empty() { return Err(Error::SshKeyNoDefined); } - Ok(self.ssh_key_path.clone()) + + if Path::new(&self.ssh_key_path).is_absolute() { + Ok(self.ssh_key_path.clone()) + } else { + let ssh_key_path = format!("{}/{}", Self::override_path(), &self.ssh_key_path); + Ok(ssh_key_path) + } } pub fn get_root_ca_path() -> Result { @@ -402,13 +412,13 @@ impl Config { log::error!("The network is not set! To configure it, run:"); eprintln!(" detee-cli account network testnet"); } else { - account_data.network = config.network; + account_data.network = config.network.clone(); } if config.ssh_key_path.is_empty() { log::error!("SSH public key path not set! To configure it, run:"); eprintln!(" detee-cli account ssh-pubkey-path /home/your_user/.ssh/id_ed25519.pub"); } else { - account_data.ssh_pubkey = config.ssh_key_path; + account_data.ssh_pubkey = config.get_ssh_pubkey().unwrap_or_default(); } match Self::get_detee_wallet() { Ok(key) => { diff --git a/src/general/cli_handler.rs b/src/general/cli_handler.rs index a53c64b..9d7d2ac 100644 --- a/src/general/cli_handler.rs +++ b/src/general/cli_handler.rs @@ -6,6 +6,7 @@ use clap::{ArgMatches, Command}; use clap_complete::{generate, Shell}; use std::error::Error; use std::io; +use std::path::PathBuf; pub fn handle_operators(matches: &ArgMatches) { match matches.subcommand() { @@ -52,7 +53,9 @@ pub fn handle_account(matches: &ArgMatches) { config::Config::init_config().sign_file(&path); } Some(("ssh-pubkey-path", path_subcommand)) => { - let path: String = path_subcommand.get_one::("path").unwrap().clone(); + let path_buff = path_subcommand.get_one::("path").unwrap().clone(); + let canonical_path = path_buff.canonicalize().unwrap_or(path_buff); + let path = canonical_path.to_str().unwrap_or_default(); config::Config::set_ssh_pubkey_path(&path); } Some(("network", path_subcommand)) => {