jensmemesclient/cli/src/main.rs

102 lines
2.3 KiB
Rust

use crate::util::MemeSorting;
use anyhow::Result;
use reqwest::Client;
use structopt::StructOpt;
mod api;
mod commands;
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(about = "lists all memes")]
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 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 http = Client::new();
match cmd {
Cmd::Up {
file,
category,
token,
name,
open,
} => {
let name = name.unwrap_or_else(|| file.clone());
commands::up::run(&http, token, file, name, category, open).await?;
},
Cmd::Cats => util::api::cats(&http)
.await?
.iter()
.for_each(|c| println!("{}", c)),
Cmd::Search { query } => commands::search::run(&http, query).await?,
Cmd::List {
category,
user,
sort,
} => commands::list::run(&http, category, user, sort).await?,
Cmd::Users => commands::users::run(&http).await?,
}
Ok(())
}