From afc09a35acbc68e58fa55ad6ed8b6bd855b7e432 Mon Sep 17 00:00:00 2001 From: Carson McManus Date: Sun, 1 Aug 2021 11:20:57 -0400 Subject: [PATCH] implement saving manifest and accounts --- src/accountmanager.rs | 40 +++++++++++++++++++++++++++++++++++++++- src/main.rs | 8 ++++++-- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/accountmanager.rs b/src/accountmanager.rs index 13433f3..7e6ee83 100644 --- a/src/accountmanager.rs +++ b/src/accountmanager.rs @@ -1,7 +1,7 @@ use log::*; use serde::{Deserialize, Serialize}; use std::fs::File; -use std::io::BufReader; +use std::io::{BufReader, Write}; use std::path::Path; use steamguard::SteamGuardAccount; @@ -52,4 +52,42 @@ impl Manifest { } Ok(()) } + + 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(()) + } } diff --git a/src/main.rs b/src/main.rs index c88197b..1822b10 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,6 +17,8 @@ use termion::{ #[macro_use] extern crate lazy_static; +#[macro_use] +extern crate anyhow; mod accountlinker; mod accountmanager; @@ -128,11 +130,11 @@ fn main() { let mut selected_accounts: Vec = vec![]; if matches.is_present("all") { // 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()); } } else { - for account in manifest.accounts { + for account in &manifest.accounts { if !matches.is_present("username") { selected_accounts.push(account.clone()); break; @@ -197,6 +199,8 @@ fn main() { } } } + + manifest.save(); } else { let server_time = steamapi::get_server_time(); for account in selected_accounts {