jensmemesclient/cli/src/main.rs

122 lines
2.9 KiB
Rust

use anyhow::Result;
use libjens::{util::MemeSorting, JMClient};
use structopt::StructOpt;
mod commands;
mod table;
mod util;
#[derive(StructOpt)]
struct Opts {
#[structopt(subcommand)]
cmd: Cmd,
}
#[derive(StructOpt)]
enum Cmd {
#[structopt(about = "uploads a meme")]
Up {
file: String,
category: String,
#[structopt(
env = "JM_TOKEN",
short,
long,
hide_env_values = true,
help = "token to use in order to upload"
)]
token: String,
#[structopt(
long,
short,
help = "the name the file should have. defaults to the name of the chosen file"
)]
name: Option<String>,
#[structopt(long, short, help = "open the files in the browser")]
open: bool,
},
#[structopt(about = "lists the available categories")]
Cats,
#[structopt(about = "searches for a meme")]
Search {
query: String,
#[structopt(long, short, help = "filter by category")]
category: Option<String>,
#[structopt(long, short, help = "filter by user")]
user: Option<String>,
#[structopt(long, help = "print the URL of the frst result")]
firsturl: bool,
#[structopt(
long,
help = "print the first search result's data to stdout",
conflicts_with = "firsturl"
)]
cat: bool,
},
#[structopt(about = "lists all memes", alias = "l")]
List {
#[structopt(long, short, help = "filter by category")]
category: Option<String>,
#[structopt(long, short, help = "filter by user")]
user: Option<String>,
#[structopt(
long,
short,
help = "how to sort the results. can be id, user, category, timestamp or link"
)]
sort: Option<MemeSorting>,
},
#[structopt(about = "Lists all users")]
Users,
}
#[tokio::main]
async fn main() -> Result<()> {
env_logger::init();
let Opts { cmd } = Opts::from_args();
let client = JMClient::new();
match cmd {
Cmd::Up {
file,
category,
token,
name,
open,
} => {
let name = name.unwrap_or_else(|| file.clone());
commands::up::run(&client, token, file, name, category, open).await?;
},
Cmd::Cats => commands::cats::run(&client).await?,
Cmd::Search {
query,
user,
category,
firsturl,
cat,
} => commands::search::run(&client, query, user, category, firsturl, cat).await?,
Cmd::List {
category,
user,
sort,
} => commands::list::run(&client, category, user, sort).await?,
Cmd::Users => commands::users::run(&client).await?,
}
Ok(())
}