Skip to content

Commit

Permalink
Initial voting setup program
Browse files Browse the repository at this point in the history
  • Loading branch information
jmshrv committed Mar 21, 2024
1 parent 8473e77 commit 2dec536
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
14 changes: 14 additions & 0 deletions voting-setup/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Generated by Cargo
# will have compiled files and executables
debug/
target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
12 changes: 12 additions & 0 deletions voting-setup/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "voting-setup"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "4.5.3", features = ["derive"] }
libsums = { git = "https://github.com/HackSocNotts/libsums.git", version = "0.5.0" }
mongodb = "2.8.2"
tokio = { version = "1.36.0", features = ["full"] }
78 changes: 78 additions & 0 deletions voting-setup/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use clap::Parser;
use libsums::client::SumsClient;
use mongodb::{
bson::{doc, Document},
options::ClientOptions,
Client,
};

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
/// The MongoDB username
mongo_user: String,

/// The MongoDB password
mongo_pass: String,

/// The MongoDB URL. Note that this isn't a full URL (e.g., mongodb://...), but is just the
/// address name.
mongo_url: String,

/// The Webdriver address for libsums
webdriver_address: String,

/// The browser being used for libsums (probably chrome)
browser_name: String,

/// The username to login to SUMS with. Must be a valid HackSoc commitee member.
sums_username: String,

/// The password for the SUMS user.
sums_password: String,
}

#[tokio::main]
async fn main() {
let args = Args::parse();

let client_options = ClientOptions::parse(format!(
"mongodb://{}:{}@{}/hacksoc?authSource=admin&retryWrites=true&w=majority",
args.mongo_user, args.mongo_pass, args.mongo_url,
))
.await
.expect("Faileed to parse client options!");

let mongodb_client =
Client::with_options(client_options).expect("Failed to create MongoDB client!");

let members_collection = mongodb_client
.database("Hacksoc")
.collection::<Document>("members");

let sums_client = SumsClient::new(213, "http://localhost:4444", "chrome")
.await
.expect("Failed to create SUMS client!");

sums_client
.authenticate(args.sums_username, args.sums_password)
.await
.expect("Failed to authenticate with SUMS!");

let members = sums_client.members().await.expect("Failed to get members!");
let member_bson = members
.iter()
.map(|member| doc! { "ID": &member.student_id });

members_collection
.drop(None)
.await
.expect("Failed to drop members collection!");

members_collection
.insert_many(member_bson, None)
.await
.expect("Failed to insert members!");

println!("Successfully inserted {} members", members.len());
}

0 comments on commit 2dec536

Please sign in to comment.