feat: Implemented file hierarchy initialization

- Bump version to 0.5.1
- Added Config::save method
- cli: Added init command
This commit is contained in:
Alexey 2025-12-05 17:16:40 +03:00
commit 2960b6dfc4
5 changed files with 111 additions and 25 deletions

View file

@ -1,6 +1,6 @@
use std::path::PathBuf;
use clap::{Parser,Subcommand};
use clap::{Args,Parser,Subcommand};
pub mod account;
pub mod map;
@ -23,6 +23,8 @@ pub struct Cli {
#[derive(Subcommand)]
pub enum Objects {
/// Initialize new SquadQuest in current working directory
Init(InitArgs),
/// Operations on the quests
#[command(subcommand)]
Quest(quest::QuestCommands),
@ -34,3 +36,8 @@ pub enum Objects {
Map(map::MapCommands),
}
#[derive(Args)]
pub struct InitArgs {
#[arg(long,short)]
pub path: Option<PathBuf>,
}

View file

@ -1,4 +1,4 @@
use std::path::{Path, PathBuf};
use std::{fs::DirBuilder, path::{Path, PathBuf}};
use clap::Parser;
use squad_quest_cli::cli::{Cli,Objects,account::*,map::*,quest::*};
@ -25,19 +25,17 @@ fn do_and_log(result: Result<(),Error>, log: bool, ok_text: String) {
}
}
fn main() {
let cli = Cli::parse();
let config = match cli.quiet {
false => Config::load(cli.config.clone()),
fn load_config_silent(quiet: bool, path: PathBuf) -> Config {
match quiet {
false => Config::load(path.clone()),
true => {
match Config::try_load(cli.config.clone()) {
match Config::try_load(path.clone()) {
Ok(mut config) => {
config.verbose = false;
config
},
Err(_) => {
let path = cli.config.clone().parent().unwrap_or(&Path::new(".")).to_owned();
let path = path.clone().parent().unwrap_or(&Path::new(".")).to_owned();
Config {
verbose: false,
path,
@ -46,9 +44,61 @@ fn main() {
}
}
},
};
}
}
fn main() {
let cli = Cli::parse();
let config = load_config_silent(cli.quiet, cli.config.clone());
let map_save = |map: Map, map_path: PathBuf| { map.save(map_path.parent().unwrap_or(Path::new("")).to_owned()) };
match &cli.command {
Objects::Init(args) => {
let path = match args.path.clone() {
Some(path) => path,
None => PathBuf::new(),
};
match DirBuilder::new().recursive(true).create(path.clone()) {
Ok(_) if !cli.quiet => println!("Created directory {:?}", path),
Err(error) => {
if !cli.quiet { eprintln!("Error: {error}"); }
return;
},
_ => {},
}
let config = Config {
path: path.clone(),
..Default::default()
};
do_and_log(config.save(path.clone()), !cli.quiet, format!("Created file {:?}/config.toml", path));
let mut config_path = path.clone();
config_path.push("config.toml");
let mut config = load_config_silent(true, config_path);
config.verbose = Config::default().verbose;
let map = Map::default();
let map_path = config.full_map_path();
do_and_log(map_save(map, map_path.clone()), !cli.quiet, format!("Created file {:?}/map.toml", map_path));
let quests_path = config.full_quests_path();
let accounts_path = config.full_accounts_path();
for path in [quests_path, accounts_path] {
match DirBuilder::new().recursive(true).create(path.clone()) {
Ok(_) if !cli.quiet => println!("Created directory {:?}", path),
Err(error) => {
if !cli.quiet { eprintln!("Error: {error}"); }
return;
},
_ => {},
}
}
},
Objects::Quest(commands) => {
let mut quests = config.load_quests();
let mut path = config.full_quests_path();
@ -289,7 +339,6 @@ fn main() {
}
};
let map_save = |map: Map, map_path: PathBuf| { map.save(map_path.parent().unwrap_or(Path::new("")).to_owned()) };
match commands {
MapCommands::List => {
for room in map.room {