Skip to content

Commit

Permalink
feat: implement config path as top level option
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianbormann committed Nov 30, 2024
1 parent 1961687 commit 9aa0207
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 44 deletions.
70 changes: 31 additions & 39 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,24 +18,28 @@ mod utils;
struct Args {
/// Subcommand to execute
#[command(subcommand)]
command: Option<Commands>,
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<String>,
},
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<String>,
},
}

#[tokio::main]
Expand All @@ -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: {}",
Expand All @@ -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<String> = 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!(
Expand Down
7 changes: 7 additions & 0 deletions cli/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error>> {
let config = config::get_config();
let yaci_devkit_root = resolve_home_symbol(&config.yaci_devkit.path);
Expand Down
6 changes: 1 addition & 5 deletions cli/tests/main_tests.rs
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit 9aa0207

Please sign in to comment.