kanit

Toy init system
Log | Files | Refs | README | LICENSE

flags.rs (3622B)


      1 use std::fmt::Formatter;
      2 use std::str::FromStr;
      3 use std::{error, fmt};
      4 
      5 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
      6 pub enum RestartPolicy {
      7     Never,
      8     Always,
      9     OnSuccess,
     10     OnFailure,
     11 }
     12 
     13 #[derive(Debug, PartialEq, Eq)]
     14 pub struct ParseRestartModeError;
     15 
     16 impl fmt::Display for ParseRestartModeError {
     17     fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
     18         write!(
     19             f,
     20             "expected `never`, `always`, `on-success`, or `on-failure`"
     21         )
     22     }
     23 }
     24 
     25 impl error::Error for ParseRestartModeError {}
     26 
     27 impl FromStr for RestartPolicy {
     28     type Err = ParseRestartModeError;
     29 
     30     fn from_str(s: &str) -> Result<Self, Self::Err> {
     31         match s {
     32             "never" => Ok(Self::Never),
     33             "always" => Ok(Self::Always),
     34             "on-success" => Ok(Self::OnSuccess),
     35             "on-failure" => Ok(Self::OnFailure),
     36             _ => Err(ParseRestartModeError),
     37         }
     38     }
     39 }
     40 
     41 impl fmt::Display for RestartPolicy {
     42     fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
     43         match self {
     44             Self::Never => write!(f, "never"),
     45             Self::Always => write!(f, "always"),
     46             Self::OnSuccess => write!(f, "on-success"),
     47             Self::OnFailure => write!(f, "on-failure"),
     48         }
     49     }
     50 }
     51 
     52 #[cfg(feature = "cli")]
     53 xflags::xflags! {
     54     src "./src/flags.rs"
     55 
     56     cmd supervisor {
     57         /// Delay used before restarting an exited process.
     58         optional -d, --restart-delay delay: u64
     59         /// Amount of times to restart a process before giving up.
     60         optional -a, --restart-attempts attempts: u64
     61         /// Policy to use before restarting, either `never`, `always`, `on-success`, or `on-failure`.
     62         optional -P, --restart-policy policy: RestartPolicy
     63         /// Set the working directory.
     64         optional -p, --pwd pwd: String
     65         /// Set the root directory.
     66         optional -r, --root root: String
     67         /// Set an environment variable (NAME=VAR).
     68         repeated -e, --env pair: String
     69         /// Set the process group.
     70         optional -g, --group gid: String
     71         /// Set the process user.
     72         optional -u, --user uid: String
     73         /// Redirect stdout to path.
     74         optional --stdout path: String
     75         /// Redirect stderr to path.
     76         optional --stderr path: String
     77         /// Cgroup to create.
     78         optional --cgroup cgroup: String
     79         /// Command to execute.
     80         required exec: String
     81         /// Arguments passed to the command.
     82         repeated args: String
     83     }
     84 }
     85 
     86 #[derive(Clone, Default)]
     87 // TODO; paths should be paths
     88 // impl needs to be marked `#[cfg(feature = "cli")]`
     89 // generated start
     90 // The following code is generated by `xflags` macro.
     91 // Run `env UPDATE_XFLAGS=1 cargo build` to regenerate.
     92 #[derive(Debug)]
     93 pub struct Supervisor {
     94     pub exec: String,
     95     pub args: Vec<String>,
     96 
     97     pub restart_delay: Option<u64>,
     98     pub restart_attempts: Option<u64>,
     99     pub restart_policy: Option<RestartPolicy>,
    100     pub cgroup: Option<String>,
    101     pub pwd: Option<String>,
    102     pub root: Option<String>,
    103     pub env: Vec<String>,
    104     pub group: Option<String>,
    105     pub user: Option<String>,
    106     pub stdout: Option<String>,
    107     pub stderr: Option<String>,
    108 }
    109 
    110 #[cfg(feature = "cli")]
    111 impl Supervisor {
    112     #[allow(dead_code)]
    113     pub fn from_env_or_exit() -> Self {
    114         Self::from_env_or_exit_()
    115     }
    116 
    117     #[allow(dead_code)]
    118     pub fn from_env() -> xflags::Result<Self> {
    119         Self::from_env_()
    120     }
    121 
    122     #[allow(dead_code)]
    123     pub fn from_vec(args: Vec<std::ffi::OsString>) -> xflags::Result<Self> {
    124         Self::from_vec_(args)
    125     }
    126 }
    127 // generated end