Merge pull request #73 from dyc3/save-sessions

Save accounts and manifest
This commit is contained in:
Carson McManus 2021-08-01 11:24:52 -04:00 committed by GitHub
commit 31cc0f884e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 41 deletions

View file

@ -1,8 +1,7 @@
use log::*; use log::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::error::Error;
use std::fs::File; use std::fs::File;
use std::io::BufReader; use std::io::{BufReader, Write};
use std::path::Path; use std::path::Path;
use steamguard::SteamGuardAccount; use steamguard::SteamGuardAccount;
@ -33,49 +32,62 @@ pub struct ManifestEntry {
} }
impl Manifest { impl Manifest {
pub fn load(path: &Path) -> Result<Manifest, Box<dyn Error>> { pub fn load(path: &Path) -> anyhow::Result<Manifest> {
debug!("loading manifest: {:?}", &path); debug!("loading manifest: {:?}", &path);
match File::open(path) { let file = File::open(path)?;
Ok(file) => {
let reader = BufReader::new(file); let reader = BufReader::new(file);
match serde_json::from_reader(reader) { let mut manifest: Manifest = serde_json::from_reader(reader)?;
Ok(m) => {
let mut manifest: Manifest = m;
manifest.folder = String::from(path.parent().unwrap().to_str().unwrap()); manifest.folder = String::from(path.parent().unwrap().to_str().unwrap());
return Ok(manifest); return Ok(manifest);
} }
Err(e) => {
return Err(Box::new(e));
}
}
}
Err(e) => {
return Err(Box::new(e));
}
}
}
pub fn load_accounts(&mut self) { pub fn load_accounts(&mut self) -> anyhow::Result<()> {
for entry in &self.entries { for entry in &self.entries {
let path = Path::new(&self.folder).join(&entry.filename); let path = Path::new(&self.folder).join(&entry.filename);
debug!("loading account: {:?}", path); debug!("loading account: {:?}", path);
match File::open(path) { let file = File::open(path)?;
Ok(f) => { let reader = BufReader::new(file);
let reader = BufReader::new(f); let account: SteamGuardAccount = serde_json::from_reader(reader)?;
match serde_json::from_reader(reader) {
Ok(a) => {
let account: SteamGuardAccount = a;
self.accounts.push(account); self.accounts.push(account);
} }
Err(e) => { Ok(())
error!("invalid json: {}", e)
}
}
}
Err(e) => {
error!("unable to open account: {}", e)
} }
pub fn add_account(&mut self, account: &SteamGuardAccount) {
debug!("adding account to manifest: {}", account.account_name);
let steamid = account.session.clone().unwrap().steam_id;
self.accounts.push(account.clone());
self.entries.push(ManifestEntry {
filename: format!("{}.maFile", account.account_name),
steam_id: steamid,
encryption_iv: None,
encryption_salt: None,
});
} }
pub fn save(&self) -> anyhow::Result<()> {
ensure!(
self.entries.len() == self.accounts.len(),
"Manifest entries don't match accounts."
);
for (entry, account) in self.entries.iter().zip(&self.accounts) {
debug!("saving {}", entry.filename);
let serialized = serde_json::to_string(&account)?;
ensure!(
serialized.len() > 2,
"Something extra weird happened and the account was serialized into nothing."
);
let path = Path::new(&self.folder).join(&entry.filename);
let mut file = File::create(path)?;
file.write_all(serialized.as_bytes())?;
file.sync_data()?;
} }
debug!("saving manifest");
let manifest_serialized = serde_json::to_string(&self)?;
let path = Path::new(&self.folder).join("manifest.json");
let mut file = File::create(path)?;
file.write_all(manifest_serialized.as_bytes())?;
file.sync_data()?;
Ok(())
} }
} }

View file

@ -17,6 +17,8 @@ use termion::{
#[macro_use] #[macro_use]
extern crate lazy_static; extern crate lazy_static;
#[macro_use]
extern crate anyhow;
mod accountlinker; mod accountlinker;
mod accountmanager; mod accountmanager;
@ -128,11 +130,11 @@ fn main() {
let mut selected_accounts: Vec<SteamGuardAccount> = vec![]; let mut selected_accounts: Vec<SteamGuardAccount> = vec![];
if matches.is_present("all") { if matches.is_present("all") {
// manifest.accounts.iter().map(|a| selected_accounts.push(a.b)); // manifest.accounts.iter().map(|a| selected_accounts.push(a.b));
for account in manifest.accounts { for account in &manifest.accounts {
selected_accounts.push(account.clone()); selected_accounts.push(account.clone());
} }
} else { } else {
for account in manifest.accounts { for account in &manifest.accounts {
if !matches.is_present("username") { if !matches.is_present("username") {
selected_accounts.push(account.clone()); selected_accounts.push(account.clone());
break; break;
@ -197,6 +199,8 @@ fn main() {
} }
} }
} }
manifest.save();
} else { } else {
let server_time = steamapi::get_server_time(); let server_time = steamapi::get_server_time();
for account in selected_accounts { for account in selected_accounts {