Merge pull request 'Add whoami command to peach-config' (#128) from who-am-i into main

Reviewed-on: #128
This commit is contained in:
notplants 2022-07-05 13:10:26 +00:00
commit fcaa9e29c4
7 changed files with 90 additions and 1 deletions

2
Cargo.lock generated
View File

@ -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",

View File

@ -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"

View File

@ -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<std::io::Error> for PeachConfigError {
@ -51,3 +56,15 @@ impl From<serde_json::Error> for PeachConfigError {
PeachConfigError::SerdeError { source: err }
}
}
impl From<PeachError> for PeachConfigError {
fn from(err: PeachError) -> PeachConfigError {
PeachConfigError::PeachLibError { source: err }
}
}
impl From<GolgiError> for PeachConfigError {
fn from(err: GolgiError) -> PeachConfigError {
PeachConfigError::Golgi { source: err }
}
}

View File

@ -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;
}

View File

@ -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<String, PeachConfigError> {
let mut sbot = init_sbot().await?;
let sbot_id = sbot.whoami().await?;
Ok(sbot_id)
}

View File

@ -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<string::FromUtf8Error> for PeachError {
PeachError::Utf8ToString(err)
}
}
impl From<GolgiError> for PeachError {
fn from(err: GolgiError) -> PeachError {
PeachError::Golgi(err)
}
}

View File

@ -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<Sbot, PeachError> {
// 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)
}