rewrite clap usage

This commit is contained in:
Quaternions 2024-03-08 09:35:10 -08:00
parent b64da4511c
commit 1dff5d6856

View File

@ -14,81 +14,103 @@ const CONCURRENT_REQUESTS:usize=32;
#[command(author,version,about,long_about=None)] #[command(author,version,about,long_about=None)]
#[command(propagate_version = true)] #[command(propagate_version = true)]
struct Cli{ struct Cli{
//asset options
#[arg(short,long)]
group:Option<u64>,
#[arg(long)]
asset_id:Option<AssetID>,
//idk how to do this better
#[arg(long)]
cookie_literal:Option<String>,
#[arg(long)]
cookie_env:Option<String>,
#[arg(long)]
cookie_file:Option<PathBuf>,
//TODO: read the versions.json file instead of doing this
//TODO: write file dates instead of versions.json
#[arg(long)]
start_version:Option<u64>,
#[arg(long)]
end_version:Option<u64>,
#[arg(long)]
r#continue:bool,
//decompile options
#[arg(long)]
no_models:Option<bool>,
#[arg(long)]
no_scripts:Option<bool>,
#[arg(long)]
no_template:Option<bool>,
#[arg(long)]
style:Option<String>,
//git options
#[arg(long)]
git_committer_name:Option<String>,
#[arg(long)]
git_committer_email:Option<String>,
#[arg(short,long)]
input:Option<PathBuf>,
#[arg(short,long)]
output:Option<PathBuf>,
#[command(subcommand)] #[command(subcommand)]
command:Commands, command:Commands,
} }
#[derive(Subcommand)] #[derive(Subcommand)]
enum Commands{ enum Commands{
DownloadHistory, DownloadHistory(DownloadHistorySubcommand),
Download(AssetIDList), Download(DownloadSubcommand),
Upload, Upload(UploadSubcommand),
Compile, Compile(CompileSubcommand),
Decompile, Decompile(DecompileSubcommand),
DecompileHistoryIntoGit, DecompileHistoryIntoGit(DecompileHistoryIntoGitSubcommand),
DownloadAndDecompileHistoryIntoGit, DownloadAndDecompileHistoryIntoGit(DownloadAndDecompileHistoryIntoGitSubcommand),
} }
#[derive(Clone,Copy,Debug)] #[derive(Args)]
struct DownloadHistorySubcommand{
asset_id:AssetID,
cookie_type:CookieType,
cookie:String,
output_folder:Option<PathBuf>,
continue_from_versions:bool,
start_version:Option<u64>,
end_version:Option<u64>,
}
#[derive(Args)]
struct DownloadSubcommand{
cookie_type:CookieType,
cookie:String,
output_folder:Option<PathBuf>,
asset_id_list:Vec<AssetID>,
}
#[derive(Args)]
struct UploadSubcommand{
asset_id:AssetID,
cookie_type:CookieType,
cookie:String,
input_file:PathBuf,
group:Option<u64>,
}
#[derive(Args)]
struct CompileSubcommand{
input_folder:PathBuf,
output_file:PathBuf,
style:Option<DecompileStyle>,
template:Option<PathBuf>,
}
#[derive(Args)]
struct DecompileSubcommand{
input_file:PathBuf,
output_folder:PathBuf,
style:DecompileStyle,
write_template:bool,
write_models:bool,
write_scripts:bool,
}
#[derive(Args)]
struct DecompileHistoryIntoGitSubcommand{
input_folder:PathBuf,
//currently output folder must be the current folder due to git2 limitations
//output_folder:cli.output.unwrap(),
style:DecompileStyle,
git_committer_name:String,
git_committer_email:String,
write_template:bool,
write_models:bool,
write_scripts:bool,
}
#[derive(Args)]
struct DownloadAndDecompileHistoryIntoGitSubcommand{
asset_id:AssetID,
cookie_type:CookieType,
cookie:String,
//currently output folder must be the current folder due to git2 limitations
//output_folder:cli.output.unwrap(),
style:DecompileStyle,
git_committer_name:String,
git_committer_email:String,
write_template:bool,
write_models:bool,
write_scripts:bool,
}
#[derive(Clone,clap::ValueEnum)]
enum CookieType{
Literal,
Environment,
File,
}
#[derive(Clone,Copy,Debug,clap::ValueEnum)]
enum DecompileStyle{ enum DecompileStyle{
Rox, Rox,
Rojo, Rojo,
RoxRojo, RoxRojo,
} }
#[derive(Args)]
struct AssetIDList{
asset_ids:Vec<AssetID>
}
#[derive(Args)]
struct PathBufList{
paths:Vec<PathBuf>
}
#[derive(serde::Deserialize)] #[derive(serde::Deserialize)]
#[allow(nonstandard_style,dead_code)] #[allow(nonstandard_style,dead_code)]
struct VersionPage{ struct VersionPage{
@ -112,100 +134,78 @@ struct AssetVersion{
#[tokio::main] #[tokio::main]
async fn main()->AResult<()>{ async fn main()->AResult<()>{
let cli=Cli::parse(); let cli=Cli::parse();
let cookie_enum={
match (cli.cookie_literal,cli.cookie_env,cli.cookie_file){
(Some(literal),None,None)=>Some(Cookie::Literal(literal)),
(None,Some(env_var),None)=>Some(Cookie::Environment(env_var)),
(None,None,Some(path))=>Some(Cookie::File(path)),
(None,None,None)=>None,
_=>Err(anyhow::Error::msg("Cookie was specified multiple times."))?,
}
};
let cookie=match cookie_enum{
Some(c)=>Some(format!(".ROBLOSECURITY={}",match c{
Cookie::Literal(s)=>s,
Cookie::Environment(var)=>std::env::var(var)?,
Cookie::File(path)=>tokio::fs::read_to_string(path).await?,
})),
None=>None,
};
let decompile_style=match cli.style.as_deref(){
Some("rox")
|Some("Rox")=>Some(DecompileStyle::Rox),
Some("rojo")
|Some("Rojo")=>Some(DecompileStyle::Rojo),
Some("rox-rojo")
|Some("rojo-rox")
|Some("roxrojo")
|Some("rojorox")
|Some("RoxRojo")
|Some("RojoRox")=>Some(DecompileStyle::RoxRojo),
None=>None,
_=>Err(anyhow::Error::msg("Invalid style"))?,
};
match cli.command{ match cli.command{
Commands::DownloadHistory=>download_history(DownloadHistoryConfig{ Commands::DownloadHistory(subcommand)=>download_history(DownloadHistoryConfig{
continue_from_versions:cli.r#continue, continue_from_versions:subcommand.continue_from_versions,
end_version:cli.end_version, end_version:subcommand.end_version,
start_version:cli.start_version.unwrap_or(0), start_version:subcommand.start_version.unwrap_or(0),
output_folder:cli.output.unwrap(), output_folder:subcommand.output_folder.unwrap_or_else(||std::env::current_dir().unwrap()),
cookie:cookie.unwrap(), cookie:Cookie::from_type(subcommand.cookie_type,subcommand.cookie).await?.0,
asset_id:cli.asset_id.unwrap(), asset_id:subcommand.asset_id,
}).await, }).await,
Commands::Download(asset_id_list)=>download_list( Commands::Download(subcommand)=>{
cookie.unwrap(), let output_folder=subcommand.output_folder.unwrap_or_else(||std::env::current_dir().unwrap());
asset_id_list.asset_ids.into_iter().map(|asset_id|{ download_list(
let mut path=cli.output.clone().unwrap(); Cookie::from_type(subcommand.cookie_type,subcommand.cookie).await?.0,
path.push(asset_id.to_string()); subcommand.asset_id_list.into_iter().map(|asset_id|{
(asset_id,path) let mut path=output_folder.clone();
}).collect() path.push(asset_id.to_string());
(asset_id,path)
}).collect()
).await
},
Commands::Upload(subcommand)=>upload_list(
Cookie::from_type(subcommand.cookie_type,subcommand.cookie).await?.0,
subcommand.group,
vec![(subcommand.asset_id,subcommand.input_file)]
).await, ).await,
Commands::Upload=>upload_list(cookie.unwrap(),cli.group,vec![(cli.asset_id.unwrap(),cli.output.unwrap())]).await, Commands::Compile(subcommand)=>compile(CompileConfig{
Commands::Compile=>compile(CompileConfig{ input_folder:subcommand.input_folder,
input_folder:cli.input.unwrap(), output_file:subcommand.output_file,
output_file:cli.output.unwrap(), template:subcommand.template,
template:None, style:subcommand.style,
style:None,
}).await, }).await,
Commands::Decompile=>decompile(DecompileConfig{ Commands::Decompile(subcommand)=>decompile(DecompileConfig{
style:decompile_style.unwrap(), style:subcommand.style,
input_file:cli.input.unwrap(), input_file:subcommand.input_file,
output_folder:cli.output.unwrap(), output_folder:subcommand.output_folder,
write_template:!cli.no_template.unwrap_or(false), write_template:subcommand.write_template,
write_models:!cli.no_models.unwrap_or(false), write_models:subcommand.write_models,
write_scripts:!cli.no_scripts.unwrap_or(false), write_scripts:subcommand.write_scripts,
}).await, }).await,
Commands::DecompileHistoryIntoGit=>decompile_history_into_git(DecompileHistoryConfig{ Commands::DecompileHistoryIntoGit(subcommand)=>decompile_history_into_git(DecompileHistoryConfig{
git_committer_name:cli.git_committer_name.unwrap(), git_committer_name:subcommand.git_committer_name,
git_committer_email:cli.git_committer_email.unwrap(), git_committer_email:subcommand.git_committer_email,
input_folder:cli.input.unwrap(), input_folder:subcommand.input_folder,
output_folder:cli.output.unwrap(), output_folder:std::env::current_dir()?,
style:decompile_style.unwrap(), style:subcommand.style,
write_template:!cli.no_template.unwrap_or(false), write_template:subcommand.write_template,
write_models:!cli.no_models.unwrap_or(false), write_models:subcommand.write_models,
write_scripts:!cli.no_scripts.unwrap_or(false), write_scripts:subcommand.write_scripts,
}).await, }).await,
Commands::DownloadAndDecompileHistoryIntoGit=>download_and_decompile_history_into_git(DownloadAndDecompileHistoryConfig{ Commands::DownloadAndDecompileHistoryIntoGit(subcommand)=>download_and_decompile_history_into_git(DownloadAndDecompileHistoryConfig{
git_committer_name:cli.git_committer_name.unwrap(), git_committer_name:subcommand.git_committer_name,
git_committer_email:cli.git_committer_email.unwrap(), git_committer_email:subcommand.git_committer_email,
cookie:cookie.unwrap(), cookie:Cookie::from_type(subcommand.cookie_type,subcommand.cookie).await?.0,
asset_id:cli.asset_id.unwrap(), asset_id:subcommand.asset_id,
output_folder:cli.output.unwrap(), output_folder:std::env::current_dir()?,
style:decompile_style.unwrap(), style:subcommand.style,
write_template:!cli.no_template.unwrap_or(false), write_template:subcommand.write_template,
write_models:!cli.no_models.unwrap_or(false), write_models:subcommand.write_models,
write_scripts:!cli.no_scripts.unwrap_or(false), write_scripts:subcommand.write_scripts,
}).await, }).await,
} }
} }
enum Cookie{ struct Cookie(String);
Literal(String), impl Cookie{
Environment(String), async fn from_type(cookie_type:CookieType,cookie_string:String)->AResult<Self>{
File(PathBuf), Ok(Self(format!(".ROBLOSECURITY={}",match cookie_type{
CookieType::Literal=>cookie_string,
CookieType::Environment=>std::env::var(cookie_string)?,
CookieType::File=>tokio::fs::read_to_string(cookie_string).await?,
})))
}
} }
enum ReaderType<R:Read>{ enum ReaderType<R:Read>{