A library for writing host-specific, single-binary configuration management and deployment tools
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.1 KiB

use std::borrow::{ Borrow, Cow };
use std::error::Error;
use std::fmt;
use std::path::PathBuf;
use command_runner::CommandRunner;
use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
#[derive(Debug)]
pub enum SystemdUserSessionError<E: Error> {
ExecError(E),
GenericError
}
impl<E: Error> Error for SystemdUserSessionError<E> {
fn description(&self) -> &str {
match self {
SystemdUserSessionError::ExecError(ref e) => e.description(),
SystemdUserSessionError::GenericError => "Generic error"
}
}
fn cause(&self) -> Option<&Error> {
match self {
SystemdUserSessionError::ExecError(ref e) => Some(e),
_ => None
}
}
}
impl<E: Error> fmt::Display for SystemdUserSessionError<E> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "{}", self.description())
}
}
pub struct SystemdUserSession<'a, C: 'a + CommandRunner> {
user_name: Cow<'a, str>,
command_runner: &'a C
}
impl<'a, C: CommandRunner> SystemdUserSession<'a, C> {
pub fn new(user_name: Cow<'a, str>, command_runner: &'a C) -> Self {
SystemdUserSession { user_name, command_runner }
}
}
impl<'a, C: CommandRunner> Symbol for SystemdUserSession<'a, C> {
fn target_reached(&self) -> Result<bool, Box<Error>> {
let mut path = PathBuf::from("/var/lib/systemd/linger");
path.push(self.user_name.borrow() as &str);
Ok(path.exists())
// Could also do `loginctl show-user ${self.user_name} | grep -F 'Linger=yes`
}
fn execute(&self) -> Result<(), Box<Error>> {
self.command_runner.run_successfully("loginctl", &["enable-linger", self.user_name.borrow()])
}
fn as_action<'b>(&'b self, runner: &'b SymbolRunner) -> Box<Action + 'b> {
Box::new(SymbolAction::new(runner, self))
}
fn into_action<'b>(self: Box<Self>, runner: &'b SymbolRunner) -> Box<Action + 'b> where Self: 'b {
Box::new(OwnedSymbolAction::new(runner, *self))
}
}
impl<'a, C: CommandRunner> fmt::Display for SystemdUserSession<'a, C> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(),fmt::Error>{
write!(f, "Systemd user session for {}", self.user_name)
}
}