Move auth key function to query module

This commit is contained in:
Kiana Sheibani 2023-09-02 01:47:52 -04:00
parent ce03cbcd2a
commit 003fde0f1e
Signed by: toki
GPG key ID: 6CB106C25E86A9F7
2 changed files with 25 additions and 24 deletions

View file

@ -1,34 +1,10 @@
#![feature(iterator_try_collect)]
use std::path::Path;
mod queries;
use queries::*;
mod datasets;
fn get_auth_key(config_dir: &Path) -> Option<String> {
use std::env::{var, VarError};
use std::fs::read_to_string;
match var("AUTH_KEY") {
Ok(key) => Some(key),
Err(VarError::NotUnicode(_)) => panic!("Invalid authorization key"),
Err(VarError::NotPresent) => {
let mut auth_file = config_dir.to_owned();
auth_file.push("auth.txt");
read_to_string(auth_file).ok().and_then(|s| {
let trimmed = s.trim();
if trimmed.is_empty() {
None
} else {
Some(trimmed.to_owned())
}
})
}
}
}
fn main() {
let mut config_dir = dirs::config_dir().unwrap();
config_dir.push("ggelo");

View file

@ -1,5 +1,6 @@
use cynic::{GraphQlResponse, QueryBuilder};
use serde::{Deserialize, Serialize};
use std::path::Path;
pub mod search_games;
pub use search_games::*;
@ -25,6 +26,30 @@ pub struct EntrantId(pub u64);
#[derive(cynic::Scalar, Debug, Clone)]
pub struct Timestamp(pub u64);
// Auth key
pub fn get_auth_key(config_dir: &Path) -> Option<String> {
use std::env::{var, VarError};
use std::fs::read_to_string;
match var("AUTH_KEY") {
Ok(key) => Some(key),
Err(VarError::NotUnicode(_)) => panic!("Invalid authorization key"),
Err(VarError::NotPresent) => {
let mut auth_file = config_dir.to_owned();
auth_file.push("auth.txt");
read_to_string(auth_file).ok().and_then(|s| {
let trimmed = s.trim();
if trimmed.is_empty() {
None
} else {
Some(trimmed.to_owned())
}
})
}
}
}
// Query machinery
pub trait QueryUnwrap<Vars>: 'static + QueryBuilder<Vars> {