Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
feat: Update cli usage
Browse files Browse the repository at this point in the history
Signed-off-by: Dephilia <me@dephilia.moe>
  • Loading branch information
Dephilia committed Nov 26, 2022
1 parent 99f680f commit 30d83b8
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 86 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "plurkcli"
version = "0.1.1"
version = "0.1.2"
authors = ["Dephilia <me@dephilia.moe>"]
edition = "2021"

Expand Down
36 changes: 11 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,19 @@ Build it, and run.
You'll need `key.toml` to configure Plurk oauth key.

```
Usage: plurk [OPTIONS]
Usage: plurk [OPTIONS] [COMMAND]
Options:
-k, --key-file <KEY_FILE>
[default: "$XDG_CONFIG_DIR/plurk-cli/key.toml"]
-g, --gen-key
--consumer-key <CONSUMER_KEY>
--consumer-secret <CONSUMER_SECRET>
--token-key <TOKEN_KEY>
--token-secret <TOKEN_SECRET>
Commands:
gen-key
comet
me
timeline
help Print this message or the help of the given subcommand(s)
-c, --comet
-m, --me
-t, --timeline
-v, --verbose
-h, --help
Print help information
-V, --version
Print version information
Options:
-k, --key-file <KEY_FILE> [default: "/Users/dephilia/Library/Application Support/plurk-cli/key.toml"]
-h, --help Print help information
-V, --version Print version information
```

The example for `key.toml` is:
Expand Down
7 changes: 5 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ struct ObjGetPlurks {
plurk_users: Option<HashMap<u64, PlurkUser>>,
}

pub async fn print_timeline(plurk: Plurk, verbose: bool) -> Result<(), PlurkError> {
pub async fn print_timeline(plurk: Plurk, verbose: bool, limit: u64) -> Result<(), PlurkError> {
let now = chrono::offset::Utc::now();
let time = now - chrono::Duration::days(1);
let time = time.to_rfc3339_opts(chrono::SecondsFormat::Secs, true);
let resp = plurk
.request_query("/APP/Polling/getPlurks", &[("offset", &time)])
.request_query(
"/APP/Polling/getPlurks",
&[("offset", &time), ("limit", &limit.to_string())],
)
.await?;
let body = resp
.json::<ObjGetPlurks>()
Expand Down
119 changes: 61 additions & 58 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,85 +9,88 @@ mod plurk;
mod utils;

use app::*;
use clap::CommandFactory;
use clap::Parser;
use clap::{CommandFactory, Parser, Subcommand};
use error::PlurkError;
use plurk::Plurk;
use tokio;

#[derive(Parser, Debug)]
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Args {
#[command(propagate_version = true)]
struct Cli {
#[command(subcommand)]
command: Option<Commands>,

#[arg(short, long, default_value_t = get_key_file_string())]
key_file: String,
}

#[arg(short, long, default_value_t = false)]
gen_key: bool,

#[arg(long)]
consumer_key: Option<String>,
#[arg(long)]
consumer_secret: Option<String>,
#[arg(long)]
token_key: Option<String>,
#[arg(long)]
token_secret: Option<String>,

#[arg(short, long, default_value_t = false)]
comet: bool,

#[arg(short, long, default_value_t = false)]
me: bool,

#[arg(short, long, default_value_t = false)]
timeline: bool,

#[arg(short, long, default_value_t = false, requires = "timeline")]
verbose: bool,
#[derive(Subcommand)]
enum Commands {
GenKey {
consumer_key: String,
consumer_secret: String,
token_key: Option<String>,
token_secret: Option<String>,
},
Comet,

Me,

Timeline {
#[arg(short, long)]
verbose: bool,
#[arg(short, long, default_value_t = 20)]
limit: u64,
},
}

#[tokio::main]
async fn main() -> Result<(), PlurkError> {
let args = Args::parse();

if args.gen_key {
if let (Some(ck), Some(cs)) = (args.consumer_key, args.consumer_secret) {
gen_key_file(ck, cs, args.token_key, args.token_secret)?;
} else {
println!("Missing argument: --consumer-key, --consumer-secret");
}
return Ok(());
}

if !get_key_file()?.as_path().exists() {
println!("Config file not exist.");
let cli = Cli::parse();

/* Gen Key need to put here */
if let Some(Commands::GenKey {
consumer_key,
consumer_secret,
token_key,
token_secret,
}) = &cli.command
{
gen_key_file(
consumer_key.clone(),
consumer_secret.clone(),
token_key.clone(),
token_secret.clone(),
)?;
return Ok(());
}

let mut plurk = Plurk::from_toml(&args.key_file)?;
let mut plurk = Plurk::from_toml(&cli.key_file)?;

if !plurk.has_token() {
plurk.acquire_plurk_key().await?;
plurk.to_toml(&args.key_file)?;
plurk.to_toml(&cli.key_file)?;
}

if args.me {
print_me(plurk.clone()).await?;
return Ok(());
}

if args.comet {
poll_comet(plurk.clone()).await?;
return Ok(());
}

if args.timeline {
print_timeline(plurk.clone(), args.verbose).await?;
return Ok(());
match &cli.command {
Some(Commands::GenKey { .. }) => {
// Bypass here
}
Some(Commands::Me) => {
print_me(plurk.clone()).await?;
}
Some(Commands::Comet) => {
poll_comet(plurk.clone()).await?;
}
Some(Commands::Timeline { verbose, limit }) => {
print_timeline(plurk.clone(), verbose.clone(), limit.clone()).await?;
}
None => {
let mut cmd = Cli::command();
cmd.print_help().unwrap();
}
}

let mut cmd = Args::command();
cmd.print_help().unwrap();

Ok(())
}

0 comments on commit 30d83b8

Please sign in to comment.