1use crate::{is_false, is_zero};
2use derive_setters::*;
3use warpgate_api::{ExecCommandInput, VirtualPath, api_enum, api_struct};
4
5api_struct!(
6 #[derive(Setters)]
7 #[serde(default)]
8 pub struct ExecCommand {
9 #[serde(skip_serializing_if = "is_false")]
13 #[setters(bool)]
14 pub allow_failure: bool,
15
16 #[serde(skip_serializing_if = "Option::is_none")]
20 #[setters(into, strip_option)]
21 pub cache: Option<String>,
22
23 #[setters(skip)]
25 pub command: ExecCommandInput,
26
27 #[serde(skip_serializing_if = "Vec::is_empty")]
30 pub inputs: Vec<CacheInput>,
31
32 #[serde(skip_serializing_if = "Option::is_none")]
35 #[setters(into, strip_option)]
36 pub label: Option<String>,
37
38 #[serde(skip_serializing_if = "is_false")]
40 #[setters(bool)]
41 pub parallel: bool,
42
43 #[serde(skip_serializing_if = "is_zero")]
46 pub retry_count: u8,
47 }
48);
49
50impl ExecCommand {
51 pub fn new(command: ExecCommandInput) -> Self {
53 Self {
54 allow_failure: false,
55 cache: None,
56 command,
57 inputs: vec![],
58 label: None,
59 parallel: false,
60 retry_count: 0,
61 }
62 }
63
64 pub fn get_label(&self) -> String {
66 self.label.clone().unwrap_or_else(|| {
67 format!("{} {}", self.command.command, self.command.args.join(" "))
68 .trim()
69 .into()
70 })
71 }
72}
73
74impl From<ExecCommandInput> for ExecCommand {
75 fn from(input: ExecCommandInput) -> Self {
76 Self::new(input)
77 }
78}
79
80api_enum!(
81 #[serde(tag = "type", content = "value", rename_all = "kebab-case")]
83 pub enum CacheInput {
84 EnvVar(String),
86
87 FileHash(VirtualPath),
89
90 FileSize(VirtualPath),
92
93 FileTimestamp(VirtualPath),
95 }
96);