tog/profile/
mod.rs

1use serde::{Deserialize, Serialize};
2use serde_yaml::Error;
3use std::fs::File;
4use std::io::prelude::*;
5use std::path::PathBuf;
6
7#[derive(Serialize, Deserialize, Debug, Clone, Default)]
8pub struct Profile {
9    pub processes: Vec<ProcessCfg>,
10    pub monitors: Vec<MonitorCfg>,
11    pub actions: Vec<ActionCfg>,
12}
13
14impl Profile {
15    pub fn new() -> Profile {
16        Profile {
17            processes: Vec::new(),
18            monitors: Vec::new(),
19            actions: Vec::new(),
20        }
21    }
22}
23
24#[derive(Serialize, Deserialize, Debug, Clone)]
25pub struct ProcessCfg {
26    #[serde(default = "default_empty_string")]
27    pub name: String,
28    #[serde(default = "default_empty_string")]
29    pub command: String,
30    #[serde(default = "default_cwd")]
31    pub cwd: String,
32    #[serde(default = "default_false")]
33    pub silent: bool,
34    #[serde(default = "default_false")]
35    pub blocking: bool,
36}
37
38#[derive(Serialize, Deserialize, Debug, Clone)]
39pub struct MonitorCfg {
40    #[serde(default = "default_empty_string")]
41    pub process: String,
42    pub triggers: Triggers,
43    #[serde(default = "default_empty_vec_string")]
44    pub actions: Vec<String>,
45}
46
47#[derive(Serialize, Deserialize, Debug, Clone)]
48pub struct Triggers {
49    pub logs: LogTriggerCfg,
50}
51
52#[derive(Serialize, Deserialize, Debug, Clone)]
53pub struct LogTriggerCfg {
54    #[serde(default = "default_empty_string")]
55    pub includes_string: String,
56}
57
58#[derive(Serialize, Deserialize, Debug, Clone)]
59pub struct ActionCfg {
60    #[serde(default = "default_empty_string")]
61    pub name: String,
62    #[serde(default = "default_empty_string")]
63    pub command: String,
64    #[serde(default = "default_cwd")]
65    pub cwd: String,
66    #[serde(default = "default_empty_string")]
67    pub r#type: String,
68    #[serde(default = "default_empty_string")]
69    pub stdin: String,
70    #[serde(default = "default_false")]
71    pub silent: bool,
72    #[serde(default = "default_false")]
73    pub blocking: bool,
74}
75
76fn default_cwd() -> String {
77    ".".to_string()
78}
79
80fn default_empty_string() -> String {
81    "".to_string()
82}
83
84fn default_false() -> bool {
85    false
86}
87
88fn default_empty_vec_string() -> Vec<String> {
89    Vec::new()
90}
91
92pub fn get_tog_pr(pathstr: String) -> Result<Profile, Error> {
93    let mut path: PathBuf = PathBuf::from(pathstr);
94
95    if path.file_name() == None {
96        path.set_file_name("tog");
97        path.set_extension("yaml");
98    } else {
99        let path_string = path
100            .clone()
101            .into_os_string()
102            .into_string()
103            .expect("!string");
104
105        if !path_string.contains("tog.yaml") {
106            path.set_extension("tog.yaml");
107        }
108    }
109
110    let mut pr_file: File = File::open(path).expect("Unable to find or open profile file");
111
112    let mut pr_file_str = String::new();
113    pr_file
114        .read_to_string(&mut pr_file_str)
115        .expect("Unable to read profile file");
116    let profile: Result<Profile, Error> = serde_yaml::from_str(&pr_file_str);
117
118    profile
119}