From d101a91a1375f68d0e97cff5a97c02e9330b0a2e Mon Sep 17 00:00:00 2001 From: Kammerlo Date: Thu, 21 Nov 2024 10:02:35 +0100 Subject: [PATCH 1/4] feat: added the option to configure the installation path of the binaries --- cli/src/config.rs | 29 +++++++++++++++++++---------- cli/src/main.rs | 26 +++++++++++++++----------- 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/cli/src/config.rs b/cli/src/config.rs index 402e92a..67e76df 100644 --- a/cli/src/config.rs +++ b/cli/src/config.rs @@ -21,27 +21,36 @@ pub struct YaciDevkit { } impl Config { - fn default() -> Self { + fn default(path: String) -> Self { Config { yaci_devkit: YaciDevkit { - path: format!("{}/yaci-devkit", get_devkit_root()), - services_path: format!("{}/services", get_devkit_root()), + path: Path::new(&path) + .join("yaci-devkit") + .to_string_lossy() + .to_string(), + services_path: Path::new(&path) + .join("services") + .to_string_lossy() + .to_string(), version: "0.9.3-beta".to_string(), }, } } - fn load() -> Self { - let config_path = format!("{}/config.json", get_devkit_root()); + fn load(path: Option) -> Self { + let config_path = Path::new(&get_devkit_root()) + .join("config.json") + .to_string_lossy() + .to_string(); if Path::new(&config_path).exists() { let file_content = fs::read_to_string(config_path).expect("Failed to read config file."); serde_json::from_str(&file_content).unwrap_or_else(|_| { eprintln!("Failed to parse config file, using default config."); - Config::default() + Config::default(path.unwrap_or(get_devkit_root())) }) } else { - let default_config = Config::default(); + let default_config = Config::default(path.unwrap_or(get_devkit_root())); log(&format!("🚀 Looks like it's your first time using the Cardano DevKit. Let's set up a config for you at: {}", config_path)); let parent_dir = Path::new(&config_path).parent().unwrap(); @@ -64,7 +73,7 @@ impl Config { } lazy_static! { - static ref CONFIG: Mutex = Mutex::new(Config::default()); + static ref CONFIG: Mutex = Mutex::new(Config::default(get_devkit_root())); } pub fn get_devkit_root() -> String { @@ -75,9 +84,9 @@ pub fn get_devkit_root() -> String { } } -pub fn init() { +pub fn init(path: Option) { let mut config = CONFIG.lock().unwrap(); - *config = Config::load(); + *config = Config::load(path); } pub fn get_config() -> Config { diff --git a/cli/src/main.rs b/cli/src/main.rs index 1556f43..7687f50 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -21,7 +21,11 @@ struct Args { #[derive(Subcommand)] enum Commands { /// Initializes a new project from a template - Init, + Init { + /// The path of where to store your project binaries + #[arg(short, long = "~/.cardano-devkit")] + path: Option, + }, /// Starts a local cardano development environment including all necessary components Start, /// Stops the local cardano development environment @@ -44,18 +48,18 @@ async fn main() { let args = Args::parse(); utils::print_header(); logger::init(args.verbose); - config::init(); - - utils::check_setup().await.unwrap_or_else(|e| { - logger::error(&format!( - "Failed to check your Yaci DevKit and services setup: {}", - e - )); - std::process::exit(1); - }); match args.command { - Commands::Init => logger::log("Init command not implemented yet"), + Commands::Init { path } => { + config::init(path); + utils::check_setup().await.unwrap_or_else(|e| { + logger::error(&format!( + "Failed to check your Yaci DevKit and services setup: {}", + e + )); + std::process::exit(1); + }); + } Commands::Start => match start::start_devkit() { Ok(_) => logger::log("Cardano DevKit started successfully"), Err(e) => { From b3018a6078fb2c2e42a6fbdeb8b4d8fc30d4ae6a Mon Sep 17 00:00:00 2001 From: Kammerlo Date: Thu, 21 Nov 2024 10:05:46 +0100 Subject: [PATCH 2/4] chore: adjusted subcommand description --- cli/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index 7687f50..4cfd22b 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -22,8 +22,8 @@ struct Args { enum Commands { /// Initializes a new project from a template Init { - /// The path of where to store your project binaries - #[arg(short, long = "~/.cardano-devkit")] + /// The path of where to store your project binaries. Default is ~/.cardano-devkit + #[arg(short, long)] path: Option, }, /// Starts a local cardano development environment including all necessary components From 9aa0207847de468679395a2f7e9e982cdafa4ea2 Mon Sep 17 00:00:00 2001 From: Fabian Bormann Date: Sat, 30 Nov 2024 20:31:06 +0100 Subject: [PATCH 3/4] feat: implement config path as top level option --- cli/src/main.rs | 70 ++++++++++++++++++----------------------- cli/src/utils.rs | 7 +++++ cli/tests/main_tests.rs | 6 +--- 3 files changed, 39 insertions(+), 44 deletions(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index ccf54b6..9b89d08 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -1,11 +1,11 @@ use std::path::Path; use std::process::Command; -use clap::CommandFactory; use clap::Parser; use clap::Subcommand; use logger::error; use logger::log; +use utils::default_config_path; mod config; mod logger; @@ -18,24 +18,28 @@ mod utils; struct Args { /// Subcommand to execute #[command(subcommand)] - command: Option, + command: Commands, /// Verbosity level (0 = quite, 1 = standard, 2 = warning, 3 = error, 4 = info, 5 = verbose) #[arg(long, default_value_t = 1)] verbose: usize, + /// Configuration file name. It should be in the root directory of the project + #[arg(short, long, default_value = default_config_path().into_os_string())] + config: String, } #[derive(Subcommand)] enum Commands { /// Initializes a new project from a template - Init { - /// The path of where to store your project binaries. Default is ~/.cardano-devkit - #[arg(short, long)] - path: Option, - }, + Init, /// Starts a local cardano development environment including all necessary components Start, /// Stops the local cardano development environment Stop, + /// Runs a yaci-cli command + Run { + /// yaci-cli arguments to run + args: Vec, + }, } #[tokio::main] @@ -51,11 +55,11 @@ async fn main() { std::process::exit(1); } - let parsed_args = Args::try_parse(); - utils::print_header(); - logger::init(parsed_args.as_ref().map(|args| args.verbose).unwrap_or(1)); - config::init(); + let parsed_args = Args::parse(); + config::init(Some(parsed_args.config)); + utils::print_header(); + logger::init(parsed_args.verbose); utils::check_setup().await.unwrap_or_else(|e| { logger::error(&format!( "Failed to check your Yaci DevKit and services setup: {}", @@ -64,43 +68,31 @@ async fn main() { std::process::exit(1); }); - match parsed_args { - Ok(args) => match args.command { - Some(Commands::Init) { path } => { - config::init(path); - utils::check_setup().await.unwrap_or_else(|e| { - logger::error(&format!( - "Failed to check your Yaci DevKit and services setup: {}", - e - )); - std::process::exit(1); - }); - } - Some(Commands::Start) => match start::start_devkit() { - Ok(_) => logger::log("Cardano DevKit started successfully"), - Err(e) => { - logger::error(&format!("Failed to start Cardano DevKit: {}", e)); - std::process::exit(1); - } - }, - Some(Commands::Stop) => logger::log("Stop command not implemented yet"), - None => { - error("Please provide a subcommand to execute."); - log(&format!( - "{}", - Args::command().render_long_help().to_string() + match parsed_args.command { + Commands::Init => { + utils::check_setup().await.unwrap_or_else(|e| { + logger::error(&format!( + "Failed to check your Yaci DevKit and services setup: {}", + e )); std::process::exit(1); + }); + } + Commands::Start => match start::start_devkit() { + Ok(_) => logger::log("Cardano DevKit started successfully"), + Err(e) => { + logger::error(&format!("Failed to start Cardano DevKit: {}", e)); + std::process::exit(1); } }, - Err(_) => { - let cli_args: Vec = std::env::args().skip(1).collect(); + Commands::Stop => logger::log("Stop command not implemented yet"), + Commands::Run { args } => { let configuration = config::get_config(); let yaci_devkit_path = Path::new(&configuration.yaci_devkit.path); let output = Command::new(yaci_devkit_path.join("yaci-cli")) .current_dir(yaci_devkit_path) - .args(cli_args) + .args(args) .output() .map_err(|yaci_cli_error| { error(&format!( diff --git a/cli/src/utils.rs b/cli/src/utils.rs index a327388..f56c2b0 100644 --- a/cli/src/utils.rs +++ b/cli/src/utils.rs @@ -148,6 +148,13 @@ pub fn resolve_home_symbol(path: &str) -> String { path.to_string() } +pub fn default_config_path() -> PathBuf { + let mut config_path = home_dir().unwrap_or_else(|| PathBuf::from("~")); + config_path.push(".cardano-devkit"); + config_path.push("config.json"); + config_path +} + pub async fn check_setup() -> Result<(), Box> { let config = config::get_config(); let yaci_devkit_root = resolve_home_symbol(&config.yaci_devkit.path); diff --git a/cli/tests/main_tests.rs b/cli/tests/main_tests.rs index 5528236..2df22c3 100644 --- a/cli/tests/main_tests.rs +++ b/cli/tests/main_tests.rs @@ -1,11 +1,7 @@ use assert_cmd::Command; -use predicates::str::contains; #[test] fn test_init_command() { let mut cmd = Command::cargo_bin("cardano-devkit").unwrap(); - cmd.arg("init") - .assert() - .success() - .stdout(contains("Init command not implemented yet")); + cmd.arg("init").assert().success(); } From e9f8f1800756aaa2ee5859772e607692d82276bf Mon Sep 17 00:00:00 2001 From: Fabian Bormann Date: Sat, 30 Nov 2024 20:34:21 +0100 Subject: [PATCH 4/4] chore: handle x86_64 and x86 arch name --- cli/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index 9b89d08..3193378 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -47,7 +47,7 @@ async fn main() { let os = std::env::consts::OS; let arch = std::env::consts::ARCH; - if !(os == "linux" && arch == "x86") && !(os == "macos" && arch == "aarch64") { + if !(os == "linux" && arch.contains("x86")) && !(os == "macos" && arch == "aarch64") { eprintln!( "Unfortunately, your operating system ({}, {}) is not currently supported. Please feel free to submit a feature request at: https://github.com/cardano-foundation/cardano-devkit/issues/new/choose", os, arch