jensmemesclient/cli/src/main.rs

114 lines
2.6 KiB
Rust
Raw Normal View History

2020-12-20 20:52:22 +01:00
use crate::util::MemeSorting;
2020-12-05 12:08:24 +01:00
use anyhow::Result;
use reqwest::Client;
use structopt::StructOpt;
mod api;
2020-12-05 22:20:08 +01:00
mod commands;
2020-12-05 12:08:24 +01:00
mod util;
#[derive(StructOpt)]
struct Opts {
#[structopt(subcommand)]
cmd: Cmd,
}
#[derive(StructOpt)]
enum Cmd {
2020-12-06 13:58:23 +01:00
#[structopt(about = "uploads a meme")]
2020-12-05 12:08:24 +01:00
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,
},
2020-12-06 13:58:23 +01:00
#[structopt(about = "lists the available categories")]
2020-12-05 12:08:24 +01:00
Cats,
2020-12-05 22:20:08 +01:00
2020-12-06 13:58:23 +01:00
#[structopt(about = "searches for a meme")]
2020-12-21 23:06:50 +01:00
Search {
query: String,
#[structopt(long, short, help = "filter by category")]
category: Option<String>,
#[structopt(long, short, help = "filter by user")]
user: Option<String>,
},
2020-12-05 22:20:08 +01:00
2020-12-06 13:58:23 +01:00
#[structopt(about = "lists all memes")]
2020-12-18 16:58:53 +01:00
List {
#[structopt(long, short, help = "filter by category")]
category: Option<String>,
#[structopt(long, short, help = "filter by user")]
user: Option<String>,
2020-12-20 20:52:22 +01:00
#[structopt(
long,
short,
help = "how to sort the results. can be id, user, category or link"
)]
sort: Option<MemeSorting>,
2020-12-18 16:58:53 +01:00
},
2020-12-19 19:43:23 +01:00
#[structopt(about = "Lists all users")]
Users,
2020-12-05 12:08:24 +01:00
}
#[tokio::main]
async fn main() -> Result<()> {
2020-12-06 14:57:14 +01:00
env_logger::init();
2020-12-05 12:08:24 +01:00
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());
2020-12-05 22:20:08 +01:00
commands::up::run(&http, token, file, name, category, open).await?;
2020-12-05 12:08:24 +01:00
},
2020-12-19 19:43:23 +01:00
Cmd::Cats => util::api::cats(&http)
2020-12-05 12:32:01 +01:00
.await?
.iter()
.for_each(|c| println!("{}", c)),
2020-12-21 23:06:50 +01:00
Cmd::Search {
query,
user,
category,
} => commands::search::run(&http, query, user, category).await?,
2020-12-20 20:52:22 +01:00
Cmd::List {
category,
user,
sort,
} => commands::list::run(&http, category, user, sort).await?,
2020-12-19 19:43:23 +01:00
Cmd::Users => commands::users::run(&http).await?,
2020-12-05 12:08:24 +01:00
}
Ok(())
}