From f002f5cf3e79399a55ebd24181e64665fa7387d2 Mon Sep 17 00:00:00 2001 From: notplants Date: Mon, 4 Jul 2022 13:23:55 +0200 Subject: [PATCH 1/3] Working on peach-config --- Cargo.lock | 1 + peach-config/Cargo.toml | 1 + peach-config/src/main.rs | 10 ++++++++++ peach-config/src/status.rs | 12 ++++++++++++ peach-lib/src/error.rs | 6 ++++++ peach-lib/src/sbot.rs | 28 ++++++++++++++++++++++++++++ 6 files changed, 58 insertions(+) create mode 100644 peach-config/src/status.rs diff --git a/Cargo.lock b/Cargo.lock index 7699594..0bdf75d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2204,6 +2204,7 @@ version = "0.1.25" dependencies = [ "clap", "env_logger 0.6.2", + "golgi", "lazy_static", "log 0.4.17", "peach-lib", diff --git a/peach-config/Cargo.toml b/peach-config/Cargo.toml index e2f226f..87a178d 100644 --- a/peach-config/Cargo.toml +++ b/peach-config/Cargo.toml @@ -37,3 +37,4 @@ log = "0.4" lazy_static = "1.4.0" peach-lib = { path = "../peach-lib" } rpassword = "5.0" +golgi = { git = "https://git.coopcloud.tech/golgi-ssb/golgi.git" } diff --git a/peach-config/src/main.rs b/peach-config/src/main.rs index f59a474..1db4e62 100644 --- a/peach-config/src/main.rs +++ b/peach-config/src/main.rs @@ -50,6 +50,10 @@ enum PeachConfig { /// Updates file permissions on PeachCloud device #[structopt(name = "permissions")] SetPermissions, + + /// Returns sbot id if sbot is running + #[structopt(name = "whoami")] + WhoAmI, } #[derive(StructOpt, Debug)] @@ -155,6 +159,12 @@ fn main() { ) } }, + PeachConfig::WhoAmI => match status::who_am_i() { + Ok(sbot_id) => {sbot_id} + Err(err) => { + error!("1") + } + }, } } } diff --git a/peach-config/src/status.rs b/peach-config/src/status.rs new file mode 100644 index 0000000..5e707a3 --- /dev/null +++ b/peach-config/src/status.rs @@ -0,0 +1,12 @@ +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() +} diff --git a/peach-lib/src/error.rs b/peach-lib/src/error.rs index 42a4993..da30cd0 100644 --- a/peach-lib/src/error.rs +++ b/peach-lib/src/error.rs @@ -256,3 +256,9 @@ impl From for PeachError { PeachError::Utf8ToString(err) } } + +impl From for PeachError { + fn from(err: GolgiError) -> PeachError { + PeachError::Golgi(err) + } +} \ No newline at end of file diff --git a/peach-lib/src/sbot.rs b/peach-lib/src/sbot.rs index e2ddba7..78ce794 100644 --- a/peach-lib/src/sbot.rs +++ b/peach-lib/src/sbot.rs @@ -2,6 +2,11 @@ use std::{fs, fs::File, io, io::Write, path::PathBuf, process::Command, str}; +use golgi::{ + api::friends::RelationshipQuery, blobs, messages::SsbMessageValue, sbot::Keystore, Sbot, +}; +use log::debug; + use crate::config_manager; use serde::{Deserialize, Serialize}; @@ -240,3 +245,26 @@ impl SbotConfig { Ok(()) } } + +/// Initialise an sbot client +pub async fn init_sbot() -> Result { + + // read sbot config from config.toml + let sbot_config = SbotConfig::read().ok(); + + debug!("Initialising an sbot client with configuration parameters"); + // initialise sbot connection with ip:port and shscap from config file + let key_path = format!( + "{}/secret", + config_manager::get_config_value("GO_SBOT_DATADIR")? + ); + let sbot_client = match sbot_config { + // TODO: panics if we pass `Some(conf.shscap)` as second arg + Some(conf) => { + let ip_port = conf.lis.clone(); + Sbot::init(Keystore::CustomGoSbot(key_path), Some(ip_port), None).await? + } + None => Sbot::init(Keystore::CustomGoSbot(key_path), None, None).await?, + }; + Ok(sbot_client) +} From 7e94135839f638927006ee1e8c18c217b0e48cb4 Mon Sep 17 00:00:00 2001 From: notplants Date: Mon, 4 Jul 2022 14:35:17 +0200 Subject: [PATCH 2/3] 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; From 1258a3697d4bbb96de5810d8f99020698933f5b4 Mon Sep 17 00:00:00 2001 From: notplants Date: Mon, 4 Jul 2022 14:54:00 +0200 Subject: [PATCH 3/3] Cargo fmt --- peach-lib/src/error.rs | 4 ++-- peach-lib/src/sbot.rs | 5 +---- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/peach-lib/src/error.rs b/peach-lib/src/error.rs index 68a98c4..9c55c1d 100644 --- a/peach-lib/src/error.rs +++ b/peach-lib/src/error.rs @@ -2,8 +2,8 @@ //! 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; +use std::{io, str, string}; /// This type represents all possible errors that can occur when interacting with the PeachCloud library. #[derive(Debug)] @@ -267,4 +267,4 @@ impl From for PeachError { fn from(err: GolgiError) -> PeachError { PeachError::Golgi(err) } -} \ No newline at end of file +} diff --git a/peach-lib/src/sbot.rs b/peach-lib/src/sbot.rs index 916d0d1..730df22 100644 --- a/peach-lib/src/sbot.rs +++ b/peach-lib/src/sbot.rs @@ -2,9 +2,7 @@ use std::{fs, fs::File, io, io::Write, path::PathBuf, process::Command, str}; -use golgi::{ - sbot::Keystore, Sbot, -}; +use golgi::{sbot::Keystore, Sbot}; use log::debug; use crate::config_manager; @@ -248,7 +246,6 @@ impl SbotConfig { /// Initialise an sbot client pub async fn init_sbot() -> Result { - // read sbot config from config.toml let sbot_config = SbotConfig::read().ok();