#[cfg(feature = "northstar")]
use crate::core::northstar::{init_northstar, update_northstar};
use std::path::PathBuf;
use clap::{Parser, Subcommand};
use log::debug;
use crate::api::model;
use directories::ProjectDirs;
use rustyline::Editor;
mod api;
mod core;
use crate::core::commands::*;
#[derive(Parser)]
#[clap(name = "Papa")]
#[clap(author = "AnAcutalEmerald <emerald_actual@proton.me>")]
#[clap(version = env!("CARGO_PKG_VERSION"))]
#[clap(about = "Command line mod manager for Northstar")]
#[clap(after_help = "Welcome back. Cockpit cooling reactivated.")]
struct Cli {
#[clap(subcommand)]
command: Commands,
#[clap(short, long)]
debug: bool,
}
#[derive(Subcommand)]
enum Commands {
#[clap(alias = "i")]
Install {
#[clap(value_name = "MOD")]
#[clap(help = "Mod name(s) to install")]
#[clap(required_unless_present = "url")]
mod_names: Vec<String>,
#[clap(short, long)]
#[clap(value_name = "URL")]
url: Option<String>,
#[clap(short, long)]
yes: bool,
#[clap(short, long)]
force: bool,
#[clap(short, long)]
global: bool,
},
#[clap(alias = "r", alias = "rm")]
Remove {
#[clap(value_name = "MOD")]
#[clap(help = "Mod name(s) to remove")]
mod_names: Vec<String>,
},
#[clap(alias = "l", alias = "ls")]
List {
#[clap(short, long)]
global: bool,
#[clap(short, long)]
all: bool,
},
#[clap(alias = "c")]
Clear {
#[clap(
help = "Force removal of all files in the cahce directory, not just downloaded packages"
)]
#[clap(long, short)]
full: bool,
},
#[clap(alias = "cfg")]
Config {
#[clap(long, short, value_name = "PATH")]
mods_dir: Option<String>,
#[clap(long, short, value_name = "CACHE")]
cache: Option<bool>,
},
#[clap(alias = "u")]
Update {
#[clap(short, long)]
yes: bool,
},
#[clap(alias = "s")]
Search {
term: Vec<String>,
},
Disable { mods: Vec<String> },
Enable { mods: Vec<String> },
#[cfg(target_os = "linux")]
#[clap(alias = "link", alias = "ln")]
Include {
mods: Vec<String>,
#[clap(long, short)]
force: bool,
},
#[cfg(target_os = "linux")]
#[clap(alias = "unlink")]
Exclude { mods: Vec<String> },
#[cfg(feature = "northstar")]
#[clap(alias("ns"))]
Northstar {
#[clap(subcommand)]
command: NstarCommands,
},
#[cfg(feature = "cluster")]
#[clap(alias("cl"))]
Cluster {
#[clap(subcommand)]
command: WsCommands,
},
#[cfg(feature = "profiles")]
Profile {
#[clap(subcommand)]
command: ProfCommands,
},
}
#[derive(Subcommand)]
enum NstarCommands {
Init { game_path: Option<PathBuf> },
Update {},
#[cfg(feature = "launcher")]
Start {},
}
#[tokio::main]
async fn main() {
let cli = Cli::parse();
if cli.debug {
std::env::set_var("RUST_LOG", "DEBUG");
}
env_logger::builder().format_timestamp(None).init();
let dirs = ProjectDirs::from("me", "greenboi", "papa").unwrap();
let rl = Editor::<()>::new();
let mut ctx = core::Ctx::new(dirs, rl).expect("Failed to create context");
let res = match cli.command {
Commands::Update { yes } => update(&mut ctx, yes).await,
Commands::Config {
mods_dir: None,
cache: None,
} => {
println!(
"Current config:\n{}",
toml::to_string_pretty(&ctx.config).unwrap()
);
Ok(())
}
Commands::Config { mods_dir, cache } => update_config(&mut ctx, mods_dir, cache),
Commands::List { global, all } => list(&ctx, global, all),
Commands::Install {
mod_names: _,
url: Some(url),
yes: _,
force: _,
global: _,
} => install_from_url(&ctx, url).await,
Commands::Install {
mod_names,
url: None,
yes,
force,
global,
} => install(&mut ctx, mod_names, yes, force, global).await,
Commands::Disable { mods } => disable(&ctx, mods),
Commands::Enable { mods } => enable(&ctx, mods),
Commands::Search { term } => search(&ctx, term).await,
Commands::Remove { mod_names } => remove(&ctx, mod_names),
Commands::Clear { full } => clear(&ctx, full),
#[cfg(feature = "northstar")]
Commands::Northstar { command } => match command {
NstarCommands::Init { game_path } => {
let game_path = if let Some(p) = game_path {
match p.canonicalize() {
Ok(p) => p,
Err(e) => {
debug!("{:#?}", e);
println!("{}", e);
return;
}
}
} else {
std::env::current_dir().unwrap()
};
init_northstar(&mut ctx, &game_path).await
}
NstarCommands::Update {} => update_northstar(&mut ctx).await,
#[cfg(feature = "launcher")]
NstarCommands::Start {} => ctx.start_northstar(&ctx),
},
#[cfg(target_os = "linux")]
Commands::Include { mods, force } => include(&ctx, mods, force),
#[cfg(target_os = "linux")]
Commands::Exclude { mods } => exclude(&ctx, mods),
#[cfg(feature = "cluster")]
Commands::Cluster { command } => cluster(&mut ctx, command),
#[cfg(feature = "profiles")]
Commands::Profile { command } => profile(&mut ctx, command),
};
if let Some(e) = res.err() {
if cli.debug {
debug!("{:#?}", e);
} else {
println!("{}", e);
}
}
}