From 7e94135839f638927006ee1e8c18c217b0e48cb4 Mon Sep 17 00:00:00 2001 From: notplants Date: Mon, 4 Jul 2022 14:35:17 +0200 Subject: [PATCH] Whoami --- Cargo.lock | 1 + peach-config/Cargo.toml | 1 + peach-config/src/error.rs | 17 +++++++++++++++++ peach-config/src/main.rs | 20 ++++++++++++++++---- peach-config/src/status.rs | 11 ++++------- peach-lib/src/error.rs | 6 ++++++ peach-lib/src/sbot.rs | 2 +- 7 files changed, 46 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0bdf75d..bfaf051 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2202,6 +2202,7 @@ dependencies = [ name = "peach-config" version = "0.1.25" dependencies = [ + "async-std", "clap", "env_logger 0.6.2", "golgi", diff --git a/peach-config/Cargo.toml b/peach-config/Cargo.toml index 87a178d..ee34f61 100644 --- a/peach-config/Cargo.toml +++ b/peach-config/Cargo.toml @@ -38,3 +38,4 @@ lazy_static = "1.4.0" peach-lib = { path = "../peach-lib" } rpassword = "5.0" golgi = { git = "https://git.coopcloud.tech/golgi-ssb/golgi.git" } +async-std = "1.10.0" diff --git a/peach-config/src/error.rs b/peach-config/src/error.rs index 7f2a159..4b3ef41 100644 --- a/peach-config/src/error.rs +++ b/peach-config/src/error.rs @@ -1,4 +1,5 @@ #![allow(clippy::nonstandard_macro_braces)] +use golgi::error::GolgiError; use peach_lib::error::PeachError; pub use snafu::ResultExt; use snafu::Snafu; @@ -35,6 +36,10 @@ pub enum PeachConfigError { ChangePasswordError { source: PeachError }, #[snafu(display("Entered passwords did not match. Please try again."))] InvalidPassword, + #[snafu(display("Error in peach lib: {}", source))] + PeachLibError { source: PeachError }, + #[snafu(display("Error in golgi: {}", source))] + Golgi { source: GolgiError }, } impl From for PeachConfigError { @@ -51,3 +56,15 @@ impl From for PeachConfigError { PeachConfigError::SerdeError { source: err } } } + +impl From for PeachConfigError { + fn from(err: PeachError) -> PeachConfigError { + PeachConfigError::PeachLibError { source: err } + } +} + +impl From for PeachConfigError { + fn from(err: GolgiError) -> PeachConfigError { + PeachConfigError::Golgi { source: err } + } +} diff --git a/peach-config/src/main.rs b/peach-config/src/main.rs index 1db4e62..be860f5 100644 --- a/peach-config/src/main.rs +++ b/peach-config/src/main.rs @@ -6,6 +6,7 @@ mod set_permissions; mod setup_networking; mod setup_peach; mod setup_peach_deb; +mod status; mod update; mod utils; @@ -106,7 +107,7 @@ arg_enum! { } } -fn main() { +async fn run() { // initialize the logger env_logger::init(); @@ -159,12 +160,23 @@ fn main() { ) } }, - PeachConfig::WhoAmI => match status::who_am_i() { - Ok(sbot_id) => {sbot_id} + PeachConfig::WhoAmI => match status::whoami().await { + Ok(sbot_id) => { + println!("{:?}", sbot_id); + {} + } Err(err) => { - error!("1") + error!("sbot whoami encountered an error: {}", err) } }, } } } + +// Enable an async main function and execute the `run()` function, +// catching any errors and printing them to `stderr` before exiting the +// process. +#[async_std::main] +async fn main() { + run().await; +} diff --git a/peach-config/src/status.rs b/peach-config/src/status.rs index 5e707a3..891d24d 100644 --- a/peach-config/src/status.rs +++ b/peach-config/src/status.rs @@ -1,12 +1,9 @@ use crate::error::PeachConfigError; -use crate::ChangePasswordOpts; -use peach_lib::password_utils::set_new_password; use peach_lib::sbot::init_sbot; -use golgi::Sbot; - /// Utility function to check if sbot is running via the whoami method -pub fn whoami() -> Result { - let sbot = init_sbot()?; - sbot.whoami() +pub async fn whoami() -> Result { + let mut sbot = init_sbot().await?; + let sbot_id = sbot.whoami().await?; + Ok(sbot_id) } diff --git a/peach-lib/src/error.rs b/peach-lib/src/error.rs index da30cd0..68a98c4 100644 --- a/peach-lib/src/error.rs +++ b/peach-lib/src/error.rs @@ -3,6 +3,7 @@ //! Error handling for various aspects of the PeachCloud system, including the network, OLED, stats and dyndns JSON-RPC clients, as well as the configuration manager, sbot client and password utilities. use std::{io, str, string}; +use golgi::GolgiError; /// This type represents all possible errors that can occur when interacting with the PeachCloud library. #[derive(Debug)] @@ -102,6 +103,9 @@ pub enum PeachError { /// The file path for the write attempt. path: String, }, + + /// Represents a Golgi error + Golgi(GolgiError), } impl std::error::Error for PeachError { @@ -130,6 +134,7 @@ impl std::error::Error for PeachError { PeachError::Utf8ToStr(_) => None, PeachError::Utf8ToString(_) => None, PeachError::Write { ref source, .. } => Some(source), + PeachError::Golgi(_) => None, } } } @@ -187,6 +192,7 @@ impl std::fmt::Display for PeachError { PeachError::Write { ref path, .. } => { write!(f, "Write error: {}", path) } + PeachError::Golgi(ref err) => err.fmt(f), } } } diff --git a/peach-lib/src/sbot.rs b/peach-lib/src/sbot.rs index 78ce794..916d0d1 100644 --- a/peach-lib/src/sbot.rs +++ b/peach-lib/src/sbot.rs @@ -3,7 +3,7 @@ use std::{fs, fs::File, io, io::Write, path::PathBuf, process::Command, str}; use golgi::{ - api::friends::RelationshipQuery, blobs, messages::SsbMessageValue, sbot::Keystore, Sbot, + sbot::Keystore, Sbot, }; use log::debug;