Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Commit

Permalink
support configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
iuridiniz committed Oct 15, 2021
1 parent 364b179 commit 2c17c7d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/settings.default.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
slack_token = "<TOKEN>"
slack_channel_id = "<CHANNEL_ID>"
# see https://api.slack.com/reference/surfaces/formatting
open_session_message = """🕵️ ▶️▶️▶️ IP `{addr}` logged in `{hostname}` as `{user}` using `{auth_info}` at `{when}`"""
close_session_message = """🕵️ 🛑🛑🛑 IP `{addr}` logout from `{hostname}` (is was `{user}` using `{auth_info}`) at `{when}`"""
# could be "America/Sao_Paulo" or "America/Los_Angeles" or "Europe/Oslo"
timezone = "UTC"
52 changes: 52 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use config::{Config, ConfigError, File, FileFormat};
use serde::Deserialize;
use std::env;

#[derive(Debug, Deserialize)]
pub struct Settings {
pub slack_token: String,
pub slack_channel_id: String,
pub open_session_message: String,
pub close_session_message: String,
pub timezone: String,
}

impl Settings {
pub fn new(system_config_file: &str) -> Result<Self, ConfigError> {
let mut s = Config::default();

// Start off by merging in the "default" configuration file
s.merge(File::from_str(
include_str!("settings.default.toml").into(),
FileFormat::Toml,
))?;

// Add some settings from environment variables
match env::var("SLACK_TOKEN") {
Ok(token) => {
s.set("slack_token", token)?;
}
Err(_) => {}
};

match env::var("SLACK_TOKEN") {
Ok(token) => {
s.set("slack_token", token)?;
}
Err(_) => {}
};

match env::var("SLACK_CHANNEL_ID") {
Ok(channel_id) => {
s.set("slack_channel_id", channel_id)?;
}
Err(_) => {}
};

// Add in system configuration file
s.merge(File::new(system_config_file, FileFormat::Toml).required(false))?;

// You can deserialize (and thus freeze) the entire configuration as
s.try_into()
}
}

0 comments on commit 2c17c7d

Please sign in to comment.