remove option type wrappers and implement defaults for SbotConfig

This commit is contained in:
glyph 2022-02-02 14:13:02 +02:00
parent 46926bf468
commit 90a90096f4
1 changed files with 48 additions and 16 deletions

View File

@ -21,8 +21,8 @@ pub struct SbotStatus {
pub downtime: Option<String>, pub downtime: Option<String>,
} }
impl SbotStatus { /// Default builder for `SbotStatus`.
/// Default builder for `SbotStatus`. impl Default for SbotStatus {
fn default() -> Self { fn default() -> Self {
Self { Self {
state: None, state: None,
@ -32,7 +32,9 @@ impl SbotStatus {
downtime: None, downtime: None,
} }
} }
}
impl SbotStatus {
/// Retrieve statistics for the go-sbot systemd process by querying `systemctl`. /// Retrieve statistics for the go-sbot systemd process by querying `systemctl`.
pub fn read() -> Result<Self, PeachError> { pub fn read() -> Result<Self, PeachError> {
let mut status = SbotStatus::default(); let mut status = SbotStatus::default();
@ -103,40 +105,64 @@ impl SbotStatus {
} }
} }
/// go-sbot configuration parameters.
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
#[serde(default)]
pub struct SbotConfig { pub struct SbotConfig {
// TODO: maybe define as a Path type? // TODO: maybe define as a Path type?
/// Directory path for the log and indexes. /// Directory path for the log and indexes.
repo: Option<String>, pub repo: String,
/// Directory path for writing debug output. /// Directory path for writing debug output.
debugdir: Option<String>, pub debugdir: String,
/// Secret-handshake app-key (aka. network key). /// Secret-handshake app-key (aka. network key).
shscap: Option<String>, pub shscap: String,
/// HMAC hash used to sign messages. /// HMAC hash used to sign messages.
hmac: Option<String>, pub hmac: String,
/// Replication hops (1: friends, 2: friends of friends). /// Replication hops (1: friends, 2: friends of friends).
hops: Option<u8>, pub hops: u8,
/// Address to listen on. /// Address to listen on.
lis: Option<String>, pub lis: String,
/// Address to listen on for WebSocket connections. /// Address to listen on for WebSocket connections.
wslis: Option<String>, pub wslis: String,
/// Address to for metrics and pprof HTTP server. /// Address to for metrics and pprof HTTP server.
debuglis: Option<String>, pub debuglis: String,
/// Enable sending local UDP broadcasts. /// Enable sending local UDP broadcasts.
localadv: Option<bool>, pub localadv: bool,
/// Enable listening for UDP broadcasts and connecting. /// Enable listening for UDP broadcasts and connecting.
localdiscov: Option<bool>, pub localdiscov: bool,
/// Enable syncing by using epidemic-broadcast-trees (EBT). /// Enable syncing by using epidemic-broadcast-trees (EBT).
#[serde(rename = "enable-ebt")] #[serde(rename(serialize = "enable_ebt", deserialize = "enable-ebt"))]
enable_ebt: Option<bool>, pub enable_ebt: bool,
/// Bypass graph auth and fetch remote's feed (useful for pubs that are restoring their data /// Bypass graph auth and fetch remote's feed (useful for pubs that are restoring their data
/// from peer; user beware - caveats about). /// from peer; user beware - caveats about).
promisc: Option<bool>, pub promisc: bool,
/// Disable the UNIX socket RPC interface. /// Disable the UNIX socket RPC interface.
nounixsock: Option<bool>, 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 { impl SbotConfig {
/// Read the go-sbot `config.toml` file from file and deserialize into `SbotConfig`.
pub fn read() -> Result<Self, PeachError> { pub fn read() -> Result<Self, PeachError> {
// determine path of user's home directory // determine path of user's home directory
let mut config_path = dirs::home_dir().ok_or(PeachError::HomeDir)?; let mut config_path = dirs::home_dir().ok_or(PeachError::HomeDir)?;
@ -149,7 +175,10 @@ impl SbotConfig {
Ok(config) Ok(config)
} }
/// Write the given `SbotConfig` to the go-sbot `config.toml` file.
pub fn write(config: SbotConfig) -> Result<(), PeachError> { 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 // convert the provided `SbotConfig` instance to a string
let config_string = toml::to_string(&config)?; let config_string = toml::to_string(&config)?;
@ -160,6 +189,9 @@ impl SbotConfig {
// open config file for writing // open config file for writing
let mut file = File::create(config_path)?; let mut file = File::create(config_path)?;
// write the repo comment to file
write!(file, "{}", repo_comment)?;
// write the config string to file // write the config string to file
write!(file, "{}", config_string)?; write!(file, "{}", config_string)?;