mirror of
https://github.com/Dangoware/confetti-box.git
synced 2025-06-22 22:53:02 -05:00
Restructured code to appease clippy
This commit is contained in:
parent
508f5c1dc9
commit
b1cebb1901
1 changed files with 72 additions and 75 deletions
147
imu/src/main.rs
147
imu/src/main.rs
|
@ -88,15 +88,15 @@ async fn main() -> Result<()> {
|
||||||
Commands::Upload { files, duration } => {
|
Commands::Upload { files, duration } => {
|
||||||
let Some(url) = config.url.clone() else {
|
let Some(url) = config.url.clone() else {
|
||||||
exit_error(
|
exit_error(
|
||||||
format!("URL is empty"),
|
"URL is empty",
|
||||||
Some(format!("Please set it using the {} command", "set".truecolor(246,199,219).bold())),
|
Some(&format!("Please set it using the {} command", "set".truecolor(246,199,219).bold())),
|
||||||
None,
|
None,
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
get_info_if_expired(&mut config).await?;
|
get_info_if_expired(&mut config).await?;
|
||||||
|
|
||||||
let duration = match parse_time_string(&duration) {
|
let duration = match parse_time_string(duration) {
|
||||||
Ok(d) => d,
|
Ok(d) => d,
|
||||||
Err(e) => return Err(anyhow!("Invalid duration: {e}")),
|
Err(e) => return Err(anyhow!("Invalid duration: {e}")),
|
||||||
};
|
};
|
||||||
|
@ -111,8 +111,8 @@ async fn main() -> Result<()> {
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
exit_error(
|
exit_error(
|
||||||
format!("Duration not allowed."),
|
"Duration not allowed.",
|
||||||
Some(format!("Please choose from:")),
|
Some("Please choose from:"),
|
||||||
Some(pretty_durations)
|
Some(pretty_durations)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -120,7 +120,7 @@ async fn main() -> Result<()> {
|
||||||
println!("Uploading...");
|
println!("Uploading...");
|
||||||
for path in files {
|
for path in files {
|
||||||
if !path.try_exists().is_ok_and(|t| t) {
|
if !path.try_exists().is_ok_and(|t| t) {
|
||||||
print_error_line(format!("The file {:#?} does not exist", path.truecolor(234, 129, 100)));
|
print_error_line(&format!("The file {:#?} does not exist", path.truecolor(234, 129, 100)));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -153,8 +153,8 @@ async fn main() -> Result<()> {
|
||||||
Commands::Download { mmids, out_directory } => {
|
Commands::Download { mmids, out_directory } => {
|
||||||
let Some(url) = config.url else {
|
let Some(url) = config.url else {
|
||||||
exit_error(
|
exit_error(
|
||||||
format!("URL is empty"),
|
"URL is empty",
|
||||||
Some(format!("Please set it using the {} command", "set".truecolor(246,199,219).bold())),
|
Some(&format!("Please set it using the {} command", "set".truecolor(246,199,219).bold())),
|
||||||
None,
|
None,
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@ -165,14 +165,14 @@ async fn main() -> Result<()> {
|
||||||
let ddir = &config.download_directory;
|
let ddir = &config.download_directory;
|
||||||
if ddir.as_os_str().is_empty() {
|
if ddir.as_os_str().is_empty() {
|
||||||
exit_error(
|
exit_error(
|
||||||
"Default download directory is empty".into(),
|
"Default download directory is empty",
|
||||||
Some(format!("Please set it using the {} command", "set".truecolor(246,199,219).bold())),
|
Some(&format!("Please set it using the {} command", "set".truecolor(246,199,219).bold())),
|
||||||
None,
|
None,
|
||||||
);
|
);
|
||||||
} else if !ddir.exists() {
|
} else if !ddir.exists() {
|
||||||
exit_error(
|
exit_error(
|
||||||
format!("Default download directory {} does not exist", ddir.display()),
|
&format!("Default download directory {} does not exist", ddir.display()),
|
||||||
Some(format!("Please set it using the {} command", "set".truecolor(246,199,219).bold())),
|
Some(&format!("Please set it using the {} command", "set".truecolor(246,199,219).bold())),
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
|
@ -185,12 +185,12 @@ async fn main() -> Result<()> {
|
||||||
if mmid.contains(format!("{url}/f/").as_str()) {
|
if mmid.contains(format!("{url}/f/").as_str()) {
|
||||||
let mmid = mmid.replace(format!("{url}/f/").as_str(), "");
|
let mmid = mmid.replace(format!("{url}/f/").as_str(), "");
|
||||||
if mmid.len() != 8 {
|
if mmid.len() != 8 {
|
||||||
exit_error("{mmid} is not a valid MMID".into(), Some("MMID must be 8 characters long".into()), None)
|
exit_error("{mmid} is not a valid MMID", Some("MMID must be 8 characters long"), None)
|
||||||
} else {
|
} else {
|
||||||
mmid
|
mmid
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
exit_error("{mmid} is not a valid MMID".into(), Some("MMID must be 8 characters long".into()), None)
|
exit_error("{mmid} is not a valid MMID", Some("MMID must be 8 characters long"), None)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
unimplemented!();
|
unimplemented!();
|
||||||
|
@ -211,7 +211,7 @@ async fn main() -> Result<()> {
|
||||||
.await {
|
.await {
|
||||||
file
|
file
|
||||||
} else {
|
} else {
|
||||||
exit_error(format!("File with MMID {mmid} was not found"), None, None)
|
exit_error("File with MMID {mmid} was not found", None, None)
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut file_res = if let Some(login) = &config.login {
|
let mut file_res = if let Some(login) = &config.login {
|
||||||
|
@ -249,7 +249,7 @@ async fn main() -> Result<()> {
|
||||||
chunk_size = next.len() as u64;
|
chunk_size = next.len() as u64;
|
||||||
first = false
|
first = false
|
||||||
}
|
}
|
||||||
out_file.write(&next).await.unwrap();
|
out_file.write_all(&next).await.unwrap();
|
||||||
|
|
||||||
progress_bar.set_position(f64::trunc(((i as f64 * chunk_size as f64) / file_size as f64) * 200.0) as u64);
|
progress_bar.set_position(f64::trunc(((i as f64 * chunk_size as f64) / file_size as f64) * 200.0) as u64);
|
||||||
}
|
}
|
||||||
|
@ -266,15 +266,15 @@ async fn main() -> Result<()> {
|
||||||
} => {
|
} => {
|
||||||
if username.is_none() && password.is_none() && url.is_none() && dl_dir.is_none() {
|
if username.is_none() && password.is_none() && url.is_none() && dl_dir.is_none() {
|
||||||
exit_error(
|
exit_error(
|
||||||
format!("Please provide an option to set"),
|
"Please provide an option to set",
|
||||||
Some(format!("Allowed options:")),
|
Some("Allowed options:"),
|
||||||
Some(vec!["--username".into(), "--password".into(), "--url".into(), "--dl-dir".into()]),
|
Some(vec!["--username".to_string(), "--password".to_string(), "--url".to_string(), "--dl-dir".to_string()]),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(u) = username {
|
if let Some(u) = username {
|
||||||
if u.is_empty() {
|
if u.is_empty() {
|
||||||
exit_error(format!("Username cannot be blank!"), None, None);
|
exit_error("Username cannot be blank!", None, None);
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(l) = config.login.as_mut() {
|
if let Some(l) = config.login.as_mut() {
|
||||||
|
@ -291,7 +291,7 @@ async fn main() -> Result<()> {
|
||||||
}
|
}
|
||||||
if let Some(p) = password {
|
if let Some(p) = password {
|
||||||
if p.is_empty() {
|
if p.is_empty() {
|
||||||
exit_error(format!("Password cannot be blank"), None, None);
|
exit_error("Password cannot be blank", None, None);
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(l) = config.login.as_mut() {
|
if let Some(l) = config.login.as_mut() {
|
||||||
|
@ -308,10 +308,10 @@ async fn main() -> Result<()> {
|
||||||
}
|
}
|
||||||
if let Some(url) = url {
|
if let Some(url) = url {
|
||||||
if url.is_empty() {
|
if url.is_empty() {
|
||||||
exit_error(format!("URL cannot be blank"), None, None);
|
exit_error("URL cannot be blank", None, None);
|
||||||
}
|
}
|
||||||
|
|
||||||
let url = if url.chars().last() == Some('/') {
|
let url = if url.ends_with('/') {
|
||||||
url.split_at(url.len() - 1).0
|
url.split_at(url.len() - 1).0
|
||||||
} else {
|
} else {
|
||||||
url
|
url
|
||||||
|
@ -330,23 +330,23 @@ async fn main() -> Result<()> {
|
||||||
}
|
}
|
||||||
if let Some(mut dir) = dl_dir.clone() {
|
if let Some(mut dir) = dl_dir.clone() {
|
||||||
if dir.is_empty() {
|
if dir.is_empty() {
|
||||||
exit_error(format!("Download directory cannot be blank"), None, None);
|
exit_error("Download directory cannot be blank", None, None);
|
||||||
}
|
}
|
||||||
if dir.as_str() == "default" {
|
if dir.as_str() == "default" {
|
||||||
dir = directories::UserDirs::new()
|
dir = directories::UserDirs::new()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.download_dir()
|
.download_dir()
|
||||||
.unwrap_or_else(|| exit_error("No Default directory available".into(), None, None))
|
.unwrap_or_else(|| exit_error("No Default directory available", None, None))
|
||||||
.to_string_lossy()
|
.to_string_lossy()
|
||||||
.to_string();
|
.to_string();
|
||||||
}
|
}
|
||||||
if dir.chars().last() != Some('/') {
|
if dir.ends_with('/') {
|
||||||
dir.push('/');
|
dir.push('/');
|
||||||
}
|
}
|
||||||
|
|
||||||
let _dir = PathBuf::from(dir.clone());
|
let _dir = PathBuf::from(dir.clone());
|
||||||
if !_dir.exists() {
|
if !_dir.exists() {
|
||||||
exit_error(format!("Directory {dir} does not exist"), None, None)
|
exit_error("Directory {dir} does not exist", None, None)
|
||||||
}
|
}
|
||||||
|
|
||||||
config.download_directory = _dir;
|
config.download_directory = _dir;
|
||||||
|
@ -357,7 +357,7 @@ async fn main() -> Result<()> {
|
||||||
Commands::Info => {
|
Commands::Info => {
|
||||||
let info = match get_info(&config).await {
|
let info = match get_info(&config).await {
|
||||||
Ok(i) => i,
|
Ok(i) => i,
|
||||||
Err(e) => exit_error(format!("Failed to get server information!"), Some(e.to_string()), None),
|
Err(e) => exit_error("Failed to get server information!", Some(e.to_string().as_str()), None),
|
||||||
};
|
};
|
||||||
config.info = Some(info);
|
config.info = Some(info);
|
||||||
config.save().unwrap();
|
config.save().unwrap();
|
||||||
|
@ -478,13 +478,13 @@ async fn upload_file<P: AsRef<Path>>(
|
||||||
|
|
||||||
async fn get_info_if_expired(config: &mut Config) -> Result<()> {
|
async fn get_info_if_expired(config: &mut Config) -> Result<()> {
|
||||||
let now = Utc::now();
|
let now = Utc::now();
|
||||||
if !config.info_fetch.is_none() && !config.info_fetch.is_some_and(|e| e <= now) {
|
if config.info_fetch.is_some() && config.info_fetch.is_none_or(|e| e > now) {
|
||||||
// Not yet ready to get a new batch of info
|
// Not yet ready to get a new batch of info
|
||||||
return Ok(())
|
return Ok(())
|
||||||
}
|
}
|
||||||
println!("{}", "Getting new server info...".truecolor(255,249,184));
|
println!("{}", "Getting new server info...".truecolor(255,249,184));
|
||||||
|
|
||||||
let info = get_info(&config).await?;
|
let info = get_info(config).await?;
|
||||||
config.info = Some(info);
|
config.info = Some(info);
|
||||||
config.info_fetch = Some(now + TimeDelta::days(2));
|
config.info_fetch = Some(now + TimeDelta::days(2));
|
||||||
config.save().unwrap();
|
config.save().unwrap();
|
||||||
|
@ -495,8 +495,8 @@ async fn get_info_if_expired(config: &mut Config) -> Result<()> {
|
||||||
async fn get_info(config: &Config) -> Result<ServerInfo> {
|
async fn get_info(config: &Config) -> Result<ServerInfo> {
|
||||||
let Some(url) = config.url.clone() else {
|
let Some(url) = config.url.clone() else {
|
||||||
exit_error(
|
exit_error(
|
||||||
format!("URL is empty"),
|
"URL is empty",
|
||||||
Some(format!("Please set it using the {} command", "set".truecolor(246,199,219).bold())),
|
Some(&format!("Please set it using the {} command", "set".truecolor(246,199,219).bold())),
|
||||||
None,
|
None,
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@ -563,21 +563,21 @@ impl Config {
|
||||||
c.save().unwrap();
|
c.save().unwrap();
|
||||||
return Ok(c);
|
return Ok(c);
|
||||||
}
|
}
|
||||||
} else {
|
} else if let Some(dir) = directories::ProjectDirs::from("", "Dangoware", "confetti_cli") {
|
||||||
if let Some(dir) = directories::ProjectDirs::from("", "Dangoware", "confetti_cli") {
|
let path = dir.config_dir();
|
||||||
let path = dir.config_dir();
|
fs::create_dir(path).or_else(|err| {
|
||||||
fs::create_dir(path).or_else(|err| {
|
if err.kind() == std::io::ErrorKind::AlreadyExists {
|
||||||
if err.kind() == std::io::ErrorKind::AlreadyExists {
|
Ok(())
|
||||||
Ok(())
|
} else {
|
||||||
} else {
|
Err(err)
|
||||||
Err(err)
|
}
|
||||||
}
|
})?;
|
||||||
})?;
|
|
||||||
|
|
||||||
let mut buf: String = String::new();
|
let mut buf: String = String::new();
|
||||||
|
|
||||||
fs::OpenOptions::new()
|
fs::OpenOptions::new()
|
||||||
.create(true)
|
.create(true)
|
||||||
|
.truncate(false)
|
||||||
.write(true)
|
.write(true)
|
||||||
.read(true)
|
.read(true)
|
||||||
.open(path.join("config.toml"))
|
.open(path.join("config.toml"))
|
||||||
|
@ -585,24 +585,23 @@ impl Config {
|
||||||
.read_to_string(&mut buf)
|
.read_to_string(&mut buf)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
if buf.is_empty() {
|
if buf.is_empty() {
|
||||||
let c = Config {
|
let c = Config {
|
||||||
url: None,
|
url: None,
|
||||||
login: None,
|
login: None,
|
||||||
info: None,
|
info: None,
|
||||||
info_fetch: None,
|
info_fetch: None,
|
||||||
download_directory: PathBuf::from(directories::UserDirs::new().unwrap().download_dir().unwrap_or(Path::new("")))
|
download_directory: PathBuf::from(directories::UserDirs::new().unwrap().download_dir().unwrap_or(Path::new("")))
|
||||||
};
|
};
|
||||||
c.save().unwrap();
|
c.save().unwrap();
|
||||||
|
|
||||||
// dbg!(path);
|
// dbg!(path);
|
||||||
return Ok(c);
|
return Ok(c);
|
||||||
} else {
|
|
||||||
buf
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
panic!("no project dir?")
|
buf
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
panic!("no project dir?")
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(toml::from_str::<Config>(c.as_str()).unwrap())
|
Ok(toml::from_str::<Config>(c.as_str()).unwrap())
|
||||||
|
@ -611,21 +610,19 @@ impl Config {
|
||||||
fn save(&self) -> Result<(), ()> {
|
fn save(&self) -> Result<(), ()> {
|
||||||
let path = if cfg!(debug_assertions) {
|
let path = if cfg!(debug_assertions) {
|
||||||
DEBUG_CONFIG.to_string()
|
DEBUG_CONFIG.to_string()
|
||||||
|
} else if let Some(dir) = directories::ProjectDirs::from("", "Dangoware", "confetti_cli") {
|
||||||
|
let path = dir.config_dir();
|
||||||
|
fs::create_dir(path).or_else(|err| {
|
||||||
|
if err.kind() == std::io::ErrorKind::AlreadyExists {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(err)
|
||||||
|
}
|
||||||
|
}).unwrap();
|
||||||
|
let x = path.join("config.toml");
|
||||||
|
x.clone().to_str().unwrap().to_string()
|
||||||
} else {
|
} else {
|
||||||
if let Some(dir) = directories::ProjectDirs::from("", "Dangoware", "confetti_cli") {
|
panic!("no project dir?")
|
||||||
let path = dir.config_dir();
|
|
||||||
fs::create_dir(path).or_else(|err| {
|
|
||||||
if err.kind() == std::io::ErrorKind::AlreadyExists {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(err)
|
|
||||||
}
|
|
||||||
}).unwrap();
|
|
||||||
let x = path.join("config.toml");
|
|
||||||
x.clone().to_str().unwrap().to_string()
|
|
||||||
} else {
|
|
||||||
panic!("no project dir?")
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
fs::OpenOptions::new().create(true).write(true).truncate(true).open(path).unwrap().write_all(toml::to_string(self).unwrap().as_bytes()).unwrap();
|
fs::OpenOptions::new().create(true).write(true).truncate(true).open(path).unwrap().write_all(toml::to_string(self).unwrap().as_bytes()).unwrap();
|
||||||
|
@ -633,7 +630,7 @@ impl Config {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn exit_error(main_message: String, fix: Option<String>, fix_values: Option<Vec<String>>) -> ! {
|
fn exit_error(main_message: &str, fix: Option<&str>, fix_values: Option<Vec<String>>) -> ! {
|
||||||
print_error_line(main_message);
|
print_error_line(main_message);
|
||||||
|
|
||||||
if let Some(f) = fix {
|
if let Some(f) = fix {
|
||||||
|
@ -654,6 +651,6 @@ fn exit_error(main_message: String, fix: Option<String>, fix_values: Option<Vec<
|
||||||
std::process::exit(1)
|
std::process::exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_error_line(message: String) {
|
fn print_error_line(message: &str) {
|
||||||
eprintln!("{}: {message}", "Error".truecolor(181,66,127).italic().underline());
|
eprintln!("{}: {message}", "Error".truecolor(181,66,127).italic().underline());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue