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

Commit

Permalink
✨ Implement the giveaway CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenein committed Jun 11, 2023
1 parent 3a16cc2 commit 4cb6d5b
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 7 deletions.
11 changes: 9 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ chrono-humanize = "0.2.2"
clap = { version = "4.3.0", features = ["derive", "env", "cargo"] }
cookie = "0.17.0"
derive_more = { version = "0.99.17", default-features = false, features = ["from"] }
fastrand = "2.0.0"
indexmap = "1.9.3"
itertools = "0.10.5"
maud = { version = "0.25.0", features = ["axum"] }
Expand Down
22 changes: 20 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod export;
pub mod giveaway;

use std::{net::SocketAddr, path::PathBuf};

Expand All @@ -23,7 +24,10 @@ pub enum Command {
Web(WebArgs),

/// Export all the votes in JSONL format.
ExportVotes(ExportVotes),
ExportVotes(ExportVotesArgs),

/// Pick an account for a giveaway.
Giveaway(GiveawayArgs),
}

#[derive(Args)]
Expand Down Expand Up @@ -80,7 +84,21 @@ impl DbArgs {
}

#[derive(Args)]
pub struct ExportVotes {
pub struct ExportVotesArgs {
#[clap(flatten)]
pub db: DbArgs,
}

#[derive(Args)]
pub struct GiveawayArgs {
#[clap(flatten)]
pub db: DbArgs,

/// Account IDs to exclude, comma-separated.
#[clap(long, value_parser, num_args = 0.., value_delimiter = ',')]
pub exclude_ids: Vec<u32>,

/// Trace all candidate IDs.
#[clap(long)]
pub trace_candidates: bool,
}
4 changes: 2 additions & 2 deletions src/cli/export.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use serde_json::json;

use crate::{cli::ExportVotes, prelude::*};
use crate::{cli::ExportVotesArgs, prelude::*};

pub async fn export_votes(args: ExportVotes) -> Result {
pub fn export_votes(args: &ExportVotesArgs) -> Result {
let manager = args.db.open()?.vote_manager()?;
for result in manager.iter_all() {
let (account_id, tank_id, vote) = result?;
Expand Down
38 changes: 38 additions & 0 deletions src/cli/giveaway.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use std::collections::HashSet;

use tracing::info;

use crate::{cli::GiveawayArgs, prelude::*};

pub fn run(args: GiveawayArgs) -> Result {
let manager = args.db.open()?;

info!("⏳ Reading votes…");
let mut account_ids = manager
.vote_manager()?
.iter_all()
.map(|result| {
let (account_id, ..) = result?;
Ok(account_id)
})
.collect::<Result<HashSet<u32>>>()?;

info!(n_accounts = account_ids.len(), "✅ Votes processed");

for account_id in args.exclude_ids {
info!(account_id, "🗑️ Removing excluded account");
account_ids.remove(&account_id);
}
info!(n_accounts = account_ids.len(), "✅ Ready to pick");

if args.trace_candidates {
for account_id in &account_ids {
info!(account_id, "🤞 Candidate");
}
}

let winner_id = fastrand::choice(&account_ids);
info!(?winner_id, "🎉 Picked a winner!");

Ok(())
}
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ async fn main() -> Result {

match args.command {
Command::Web(args) => trace(web::run(args).await),
Command::ExportVotes(args) => trace(cli::export::export_votes(args).await),
Command::ExportVotes(args) => trace(cli::export::export_votes(&args)),
Command::Giveaway(args) => trace(cli::giveaway::run(args)),
}
}

0 comments on commit 4cb6d5b

Please sign in to comment.