Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions libshpool/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,12 @@ pub struct Config {
/// See https://man7.org/linux/man-pages/man8/pam_motd.8.html
/// for more info.
pub motd_args: Option<Vec<String>>,

/// A list of default values for variables. Setting values
/// in this list is equivilant to running `shpool var set` for
/// each (var, value) tuple every time the shpool daemon starts
/// up.
pub var_default: Option<Vec<VarSetting>>,
}

impl Config {
Expand Down Expand Up @@ -327,6 +333,7 @@ impl Config {
prompt_prefix: self.prompt_prefix.or(another.prompt_prefix),
motd: self.motd.or(another.motd),
motd_args: self.motd_args.or(another.motd_args),
var_default: self.var_default.or(another.var_default),
}
}
}
Expand Down Expand Up @@ -407,6 +414,14 @@ pub enum MotdDisplayMode {
Dump,
}

#[derive(Deserialize, Debug, Clone)]
pub struct VarSetting {
/// The variable name.
pub var: String,
/// The variable value.
pub value: String,
}

#[cfg(test)]
mod test {
use super::*;
Expand Down Expand Up @@ -436,6 +451,11 @@ mod test {
r#"
session_restore_engine = "vterm"
"#,
r#"
[[var_default]]
var = "foo"
value = "bar"
"#,
];

for case in cases.into_iter() {
Expand Down
13 changes: 12 additions & 1 deletion libshpool/src/daemon/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@ impl Server {
}
});

let vars = Mutex::new(
config
.get()
.var_default
.clone()
.unwrap_or(vec![])
.into_iter()
.map(|v| (v.var, v.value))
.collect(),
);

let daily_messenger = Arc::new(show_motd::DailyMessenger::new(config.clone())?);
Ok(Arc::new(Server {
config,
Expand All @@ -115,7 +126,7 @@ impl Server {
hooks,
daily_messenger,
log_level_handle,
vars: HashMap::new().into(),
vars,
}))
}

Expand Down
13 changes: 13 additions & 0 deletions shpool/tests/data/var_default.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
norc = true
noecho = true
shell = "/bin/bash"
session_restore_mode = "simple"
prompt_prefix = ""

[env]
PS1 = "prompt> "
TERM = ""

[[var_default]]
var = "default_foo"
value = "default_bar"
8 changes: 5 additions & 3 deletions shpool/tests/support/line_matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ where
pub fn scan_until_re(&mut self, re: &str) -> anyhow::Result<()> {
let compiled_re = Regex::new(re)?;
let start = time::Instant::now();
let mut line = String::new();
loop {
let mut line = String::new();
match self.out.read_line(&mut line) {
Ok(0) => {
return Err(anyhow!("LineMatcher: EOF"));
Expand Down Expand Up @@ -70,6 +70,7 @@ where
return Ok(());
} else {
eprintln!(" no match");
line.clear();
}
}
}
Expand All @@ -83,8 +84,8 @@ where

pub fn capture_re(&mut self, re: &str) -> anyhow::Result<Vec<Option<String>>> {
let start = time::Instant::now();
let mut line = String::new();
loop {
let mut line = String::new();
match self.out.read_line(&mut line) {
Ok(0) => {
return Err(anyhow!("LineMatcher: EOF"));
Expand Down Expand Up @@ -133,8 +134,8 @@ where
/// assertions fail (the never match regex).
pub fn drain(&mut self) -> anyhow::Result<()> {
let start = time::Instant::now();
let mut line = String::new();
loop {
let mut line = String::new();
match self.out.read_line(&mut line) {
Ok(0) => {
return Ok(());
Expand Down Expand Up @@ -165,6 +166,7 @@ where
}

self.check_persistant_assertions(&line)?;
line.clear();
}
}

Expand Down
18 changes: 18 additions & 0 deletions shpool/tests/var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,21 @@ fn no_daemon() -> anyhow::Result<()> {

Ok(())
}

#[test]
#[timeout(30000)]
fn default_vars() -> anyhow::Result<()> {
let mut daemon_proc = support::daemon::Proc::new(
"var_default.toml",
DaemonArgs { listen_events: false, ..DaemonArgs::default() },
)
.context("starting daemon proc")?;

let out = daemon_proc.var_get("default_foo")?;
assert!(out.status.success(), "var get proc did not exit successfully");

let stdout = String::from_utf8_lossy(&out.stdout[..]);
assert_eq!(stdout.trim(), "default_bar");

Ok(())
}
Loading