Added resolving paths.

This commit is contained in:
Ian Wijma 2024-04-01 23:15:55 +11:00
parent 6fe987a1d0
commit ea3be48577
8 changed files with 71 additions and 2 deletions

0
examples/empty/.git-keep Normal file
View File

View File

View File

1
src/commands/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod run;

16
src/commands/run.rs Normal file
View File

@ -0,0 +1,16 @@
use clap::Args;
use crate::utils::file_resolvers::resolve_configuration_file;
#[derive(Args, Debug)]
pub struct Arguments {
#[arg(default_value = ".")]
target: String
}
pub fn run (arguments: &Arguments) -> Result<(), String> {
let target = resolve_configuration_file(&arguments.target)?;
println!("{:?}", target);
Ok(())
}

View File

@ -1,3 +1,31 @@
fn main() { use std::process::exit;
println!("Hello, world!"); use clap::{Parser, Subcommand};
use commands::run;
mod commands;
mod utils;
#[derive(Subcommand, Debug)]
enum Command {
Run(run::Arguments)
}
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None, propagate_version = true)]
struct Arguments {
#[command(subcommand)]
command: Command
}
fn main() {
let arguments = Arguments::parse();
let result = match &arguments.command {
Command::Run(arguments) => { run::run(arguments) }
};
match result {
Ok(_) => exit(0),
Err(err) => eprintln!("{}", err)
}
} }

View File

@ -0,0 +1,23 @@
use std::path::{Path, PathBuf};
use std::fs::canonicalize;
const DEFAULT_FILENAME: &str = "rask.yaml";
pub fn resolve_configuration_file(target: &String) -> Result<PathBuf, String> {
let target_path = Path::new(target);
let mut target = match canonicalize(target_path) {
Ok(target) => target,
Err(_) => return Err(format!("Target does not exists: {:?}", target))
};
if target.is_dir() {
target.push(DEFAULT_FILENAME);
}
if !target.exists() {
return Err(format!("Target does not exists: {:?}", target))
}
Ok(target)
}

1
src/utils/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod file_resolvers;