From 93e61a4bd6ad8b5db1083bdd21994bf73b0b90ba Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Mon, 17 Apr 2023 20:30:16 +0200 Subject: added clap cli, modified (basically) all user interface functions; changed some functions to easier string handling --- src/main.rs | 278 ++++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 225 insertions(+), 53 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 32727c7..0dfc190 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,65 +1,237 @@ -use std::{env, process}; +use clap::{Parser, Subcommand}; +use modlist::{config::Cfg, mod_add, mod_remove, db::{lists_get, config_get_current_list, lists_get_all_ids}, IDSelector, download, update, List, get_current_list, import, devdir, export, list_add, Modloader, list_version, list_remove, list_change}; -use modlist::{config::Cfg, input::{get_input, Cmd}, update, download, list, io, modification, setup}; +//TODO make default list optional + +#[derive(Parser)] +#[command(author, version, about)] +struct Cli { + #[command(subcommand)] + command: Commands, +} + +#[derive(Subcommand)] +enum Commands { + r#Mod { + #[command(subcommand)] + command: ModCommands, + }, + List { + #[command(subcommand)] + command: ListCommands + }, + Download { + /// download all lists + #[arg(short, long)] + all: bool, + + /// clean all mods before downloading them + #[arg(short, long)] + clean: bool, + + /// remove disabled versions + #[arg(short, long)] + remove: bool, + }, + Update { + /// download all lists + #[arg(short, long)] + all: bool, + + /// directly download updated mods + #[arg(short, long)] + download: bool, + + /// clean all mods before downloading them + #[arg(short, long)] + clean: bool, + + /// delete disabled versions + #[arg(short, long)] + remove: bool, + }, + Import { + #[arg(short, long)] + file: Option, + + /// directly download imported mods + #[arg(short, long)] + download: bool, + }, + Export { + /// the list you want to export + list: Option + } +} + +#[derive(Subcommand)] +enum ModCommands { + Add { + /// id of the mod/version + id: String, + + /// set id mode to version + #[arg(short, long)] + version: bool, + + /// directly download the mod + #[arg(short, long)] + download: bool, + + /// lock the version added + #[arg(/* short , */long)] + lock: bool, + + /// optional List selection, else default list will be used + #[arg(short, long)] + list: Option + }, + Remove { + /// id, name or title of the mod + id: String, + + /// optional List selection, else default list will be used + #[arg(short, long)] + list: Option + } +} + +#[derive(Subcommand)] +enum ListCommands { + Add { + /// list id + id: String, + + directory: String, + + modloader: Option, + + version: Option, + }, + Remove { + /// id, name or title of the list + id: String + }, + List, + Change { + /// id of the list to change to + id: String + }, + Version { + /// list id + id: String, + /// desired minecraft version + version: String, + + /// directly download updated mods + #[arg(long, short)] + download: bool, + + /// delete disabled versions + #[arg(short, long)] + remove: bool, + } +} #[tokio::main] async fn main() { + + let cli = Cli::parse(); + let config = Cfg::init("modlist.toml").unwrap(); - - let mut args: Vec = env::args().collect(); - args.reverse(); - args.pop(); - args.reverse(); - - if args.is_empty() { - println!("Please enter an argument"); - process::exit(1); - }; - - let input = match get_input(config.clone(), args).await { - Ok(i) => i, - Err(e) => { - println!("{}", e); - process::exit(1); - } - }; - - match input.clone().command.unwrap() { - Cmd::Mod => { - modification(config, input).await - }, - Cmd::List => { - list(config, input).await - }, - Cmd::Update => { - update(config, input).await - }, - Cmd::Download => { - download(config, input).await + println!("{:?}", config); + + //TODO setup? maybe setup on install + match cli.command { + Commands::Mod { command } => { + + match command { + #[allow(unused_variables)] + ModCommands::Add { id, version, list, download, lock } => { + let listf = match list { + Some(list) => lists_get(config.clone(), list).unwrap(), + None => lists_get(config.clone(), config_get_current_list(config.clone()).unwrap()).unwrap(), + }; + + let marked_id = match version { + true => IDSelector::VersionID(id), + false => IDSelector::ModificationID(id), + }; + + mod_add(config, vec![marked_id], listf, download, lock).await + } + ModCommands::Remove { id, list } => { + //TODO add output + //TODO add success even if no file found + let listf = match list { + Some(list) => lists_get(config.clone(), list).unwrap(), + None => lists_get(config.clone(), config_get_current_list(config.clone()).unwrap()).unwrap(), + }; + mod_remove(config, &id, listf) + } + } }, - Cmd::Io => { - io(config, input).await + Commands::List { command } => { + match command { + ListCommands::Add { id, directory, modloader, version } => { + let ml = match modloader { + Some(ml) => Modloader::from(&ml).unwrap(), + //TODO add default modloader to config + None => Modloader::Fabric, + }; + + let ver = match version { + Some(ver) => ver, + //TODO get latest version + //TODO impl config for specific version or latest or latest snap + None => "1.19.4".to_string(), + }; + + list_add(config, id, ver, ml, directory) + }, + ListCommands::Remove { id } => { + list_remove(config, id) + }, + ListCommands::List => { + todo!() + }, + ListCommands::Change { id } => { + list_change(config, id) + }, + ListCommands::Version { id, version, download, remove } => { + list_version(config, id, version, download, remove).await + } + } }, - Cmd::Version => { - show_version(); - Ok(()) + //TODO a add specific list + Commands::Update { all, download, clean, remove } => { + let mut liststack: Vec = vec![]; + if all { + let list_ids = lists_get_all_ids(config.clone()).unwrap(); + for id in list_ids { + liststack.push(lists_get(config.clone(), id).unwrap()); + } + } else { + let current = get_current_list(config.clone()).unwrap(); + println!("Update list {}:", current.id); + liststack.push(current) + } + update(config, liststack, clean, download, remove).await }, - Cmd::Setup => { - setup(config).await + //TODO add specific list + Commands::Download { all, clean, remove } => { + download(config, all, clean, remove).await }, - }.unwrap() -} + Commands::Import { file, download } => { + let filestr: String = match file { + Some(args) => args, + None => devdir(dirs::home_dir().unwrap().join("mlexport.toml").into_os_string().into_string().unwrap().as_str()), + }; -fn show_version() { - match std::env::var("DEV") { - Ok(dev) => { - let devint = dev.parse::().unwrap(); - if devint >= 1 { - println!("Modlist by FxQnLr v{} (DEV)", env!("CARGO_PKG_VERSION")); - } else { - println!("Modlist by FxQnLr v{}", env!("CARGO_PKG_VERSION")); - } + import(config, filestr, download).await }, - Err(..) => println!("Modlist by FxQnLr v{}", env!("CARGO_PKG_VERSION")), - } + Commands::Export { list } => { + export(config, list) + }, + }.unwrap(); } -- cgit v1.2.3