From 90a90096f4a04df5b46022fbaeed9850f1404999 Mon Sep 17 00:00:00 2001 From: glyph Date: Wed, 2 Feb 2022 14:13:02 +0200 Subject: [PATCH] remove option type wrappers and implement defaults for SbotConfig --- peach-lib/src/sbot.rs | 64 ++++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 16 deletions(-) diff --git a/peach-lib/src/sbot.rs b/peach-lib/src/sbot.rs index e9b9081..b784017 100644 --- a/peach-lib/src/sbot.rs +++ b/peach-lib/src/sbot.rs @@ -21,8 +21,8 @@ pub struct SbotStatus { pub downtime: Option, } -impl SbotStatus { - /// Default builder for `SbotStatus`. +/// Default builder for `SbotStatus`. +impl Default for SbotStatus { fn default() -> Self { Self { state: None, @@ -32,7 +32,9 @@ impl SbotStatus { downtime: None, } } +} +impl SbotStatus { /// Retrieve statistics for the go-sbot systemd process by querying `systemctl`. pub fn read() -> Result { let mut status = SbotStatus::default(); @@ -103,40 +105,64 @@ impl SbotStatus { } } +/// go-sbot configuration parameters. #[derive(Debug, Serialize, Deserialize)] +#[serde(default)] pub struct SbotConfig { // TODO: maybe define as a Path type? /// Directory path for the log and indexes. - repo: Option, + pub repo: String, /// Directory path for writing debug output. - debugdir: Option, + pub debugdir: String, /// Secret-handshake app-key (aka. network key). - shscap: Option, + pub shscap: String, /// HMAC hash used to sign messages. - hmac: Option, + pub hmac: String, /// Replication hops (1: friends, 2: friends of friends). - hops: Option, + pub hops: u8, /// Address to listen on. - lis: Option, + pub lis: String, /// Address to listen on for WebSocket connections. - wslis: Option, + pub wslis: String, /// Address to for metrics and pprof HTTP server. - debuglis: Option, + pub debuglis: String, /// Enable sending local UDP broadcasts. - localadv: Option, + pub localadv: bool, /// Enable listening for UDP broadcasts and connecting. - localdiscov: Option, + pub localdiscov: bool, /// Enable syncing by using epidemic-broadcast-trees (EBT). - #[serde(rename = "enable-ebt")] - enable_ebt: Option, + #[serde(rename(serialize = "enable_ebt", deserialize = "enable-ebt"))] + pub enable_ebt: bool, /// Bypass graph auth and fetch remote's feed (useful for pubs that are restoring their data /// from peer; user beware - caveats about). - promisc: Option, + pub promisc: bool, /// Disable the UNIX socket RPC interface. - nounixsock: Option, + pub nounixsock: bool, +} + +/// Default configuration values for go-sbot. +impl Default for SbotConfig { + fn default() -> Self { + Self { + repo: ".ssb-go".to_string(), + debugdir: "".to_string(), + shscap: "1KHLiKZvAvjbY1ziZEHMXawbCEIM6qwjCDm3VYRan/s=".to_string(), + hmac: "".to_string(), + hops: 1, + lis: ":8008".to_string(), + wslis: ":8989".to_string(), + debuglis: "localhost:6078".to_string(), + localadv: false, + localdiscov: false, + enable_ebt: false, + promisc: false, + nounixsock: false, + } + } } impl SbotConfig { + /// Read the go-sbot `config.toml` file from file and deserialize into `SbotConfig`. pub fn read() -> Result { // determine path of user's home directory let mut config_path = dirs::home_dir().ok_or(PeachError::HomeDir)?; @@ -149,7 +175,10 @@ impl SbotConfig { Ok(config) } + /// Write the given `SbotConfig` to the go-sbot `config.toml` file. pub fn write(config: SbotConfig) -> Result<(), PeachError> { + let repo_comment = "# For details about go-sbot configuration, please visit the repo: https://github.com/cryptoscope/ssb\n".to_string(); + // convert the provided `SbotConfig` instance to a string let config_string = toml::to_string(&config)?; @@ -160,6 +189,9 @@ impl SbotConfig { // open config file for writing let mut file = File::create(config_path)?; + // write the repo comment to file + write!(file, "{}", repo_comment)?; + // write the config string to file write!(file, "{}", config_string)?;