diff --git a/Cargo.lock b/Cargo.lock index 7699594..bfaf051 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2202,8 +2202,10 @@ dependencies = [ name = "peach-config" version = "0.1.25" dependencies = [ + "async-std", "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..ee34f61 100644 --- a/peach-config/Cargo.toml +++ b/peach-config/Cargo.toml @@ -37,3 +37,5 @@ 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" } +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 f59a474..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; @@ -50,6 +51,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)] @@ -102,7 +107,7 @@ arg_enum! { } } -fn main() { +async fn run() { // initialize the logger env_logger::init(); @@ -155,6 +160,23 @@ fn main() { ) } }, + PeachConfig::WhoAmI => match status::whoami().await { + Ok(sbot_id) => { + println!("{:?}", sbot_id); + {} + } + Err(err) => { + 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 new file mode 100644 index 0000000..891d24d --- /dev/null +++ b/peach-config/src/status.rs @@ -0,0 +1,9 @@ +use crate::error::PeachConfigError; +use peach_lib::sbot::init_sbot; + +/// Utility function to check if sbot is running via the whoami method +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 42a4993..9c55c1d 100644 --- a/peach-lib/src/error.rs +++ b/peach-lib/src/error.rs @@ -2,6 +2,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 golgi::GolgiError; use std::{io, str, string}; /// This type represents all possible errors that can occur when interacting with the PeachCloud library. @@ -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), } } } @@ -256,3 +262,9 @@ impl From for PeachError { PeachError::Utf8ToString(err) } } + +impl From for PeachError { + fn from(err: GolgiError) -> PeachError { + PeachError::Golgi(err) + } +} diff --git a/peach-lib/src/sbot.rs b/peach-lib/src/sbot.rs index e2ddba7..730df22 100644 --- a/peach-lib/src/sbot.rs +++ b/peach-lib/src/sbot.rs @@ -2,6 +2,9 @@ use std::{fs, fs::File, io, io::Write, path::PathBuf, process::Command, str}; +use golgi::{sbot::Keystore, Sbot}; +use log::debug; + use crate::config_manager; use serde::{Deserialize, Serialize}; @@ -240,3 +243,25 @@ 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) +}