docker_api/opts/
mod.rs

1//! Options used for configuring the behavior of certain API endpoints
2mod container;
3mod exec;
4mod image;
5mod network;
6mod system;
7mod volume;
8
9#[cfg(feature = "swarm")]
10#[cfg_attr(docsrs, doc(cfg(feature = "swarm")))]
11mod node;
12#[cfg(feature = "swarm")]
13#[cfg_attr(docsrs, doc(cfg(feature = "swarm")))]
14mod plugin;
15#[cfg(feature = "swarm")]
16#[cfg_attr(docsrs, doc(cfg(feature = "swarm")))]
17mod service;
18#[cfg(feature = "swarm")]
19#[cfg_attr(docsrs, doc(cfg(feature = "swarm")))]
20mod swarm;
21
22pub use container::*;
23pub use exec::*;
24pub use image::*;
25pub use network::*;
26pub use system::*;
27pub use volume::*;
28
29#[cfg(feature = "swarm")]
30pub use node::*;
31#[cfg(feature = "swarm")]
32pub use plugin::*;
33#[cfg(feature = "swarm")]
34pub use service::*;
35#[cfg(feature = "swarm")]
36pub use swarm::*;
37
38use containers_api::{impl_opts_builder, impl_url_bool_field, impl_url_field};
39
40impl_opts_builder!(url => Logs);
41
42impl LogsOptsBuilder {
43    impl_url_bool_field!(
44        /// Keep connection after returning logs.
45        follow => "follow"
46    );
47
48    impl_url_bool_field!(
49        /// Return logs from `stdout`.
50        stdout => "stdout"
51    );
52
53    impl_url_bool_field!(
54        /// Return logs from `stderr`.
55        stderr => "stderr"
56    );
57
58    impl_url_bool_field!(
59        /// Add timestamps to every log line.
60        timestamps => "timestamps"
61    );
62
63    impl_url_field!(
64        /// Only return this number of log lines from the end of logs
65        n_lines: usize => "tail"
66    );
67
68    /// Return all log lines.
69    pub fn all(mut self) -> Self {
70        self.params.insert("tail", "all".into());
71        self
72    }
73
74    #[cfg(feature = "chrono")]
75    /// Only return logs since this time.
76    pub fn since<Tz>(mut self, timestamp: &chrono::DateTime<Tz>) -> Self
77    where
78        Tz: chrono::TimeZone,
79    {
80        self.params
81            .insert("since", timestamp.timestamp().to_string());
82        self
83    }
84
85    #[cfg(not(feature = "chrono"))]
86    /// Only return logs since this time, as a UNIX timestamp.
87    pub fn since(mut self, timestamp: i64) -> Self {
88        self.params.insert("since", timestamp.to_string());
89        self
90    }
91
92    #[cfg(feature = "chrono")]
93    /// Only return logs before this time.
94    pub fn until<Tz>(mut self, timestamp: &chrono::DateTime<Tz>) -> Self
95    where
96        Tz: chrono::TimeZone,
97    {
98        self.params
99            .insert("until", timestamp.timestamp().to_string());
100        self
101    }
102
103    #[cfg(not(feature = "chrono"))]
104    /// Only return logs before this time, as a UNIX timestamp.
105    pub fn until(mut self, timestamp: i64) -> Self {
106        self.params.insert("until", timestamp.to_string());
107        self
108    }
109}
110
111#[cfg(test)]
112mod tests {
113    use super::*;
114    #[cfg(feature = "chrono")]
115    #[test]
116    fn logs_options() {
117        let timestamp = chrono::NaiveDateTime::from_timestamp_opt(2_147_483_647, 0);
118        let since = chrono::DateTime::<chrono::Utc>::from_utc(timestamp.unwrap(), chrono::Utc);
119
120        let options = LogsOptsBuilder::default()
121            .follow(true)
122            .stdout(true)
123            .stderr(true)
124            .timestamps(true)
125            .all()
126            .since(&since)
127            .build();
128
129        let serialized = options.serialize().unwrap();
130
131        assert!(serialized.contains("follow=true"));
132        assert!(serialized.contains("stdout=true"));
133        assert!(serialized.contains("stderr=true"));
134        assert!(serialized.contains("timestamps=true"));
135        assert!(serialized.contains("tail=all"));
136        assert!(serialized.contains("since=2147483647"));
137
138        let options = LogsOptsBuilder::default().n_lines(5).until(&since).build();
139
140        let serialized = options.serialize().unwrap();
141
142        assert!(serialized.contains("tail=5"));
143        assert!(serialized.contains("until=2147483647"));
144    }
145
146    #[cfg(not(feature = "chrono"))]
147    #[test]
148    fn logs_options() {
149        let options = LogsOptsBuilder::default()
150            .follow(true)
151            .stdout(true)
152            .stderr(true)
153            .timestamps(true)
154            .all()
155            .since(2_147_483_647)
156            .until(2_147_600_000)
157            .build();
158
159        let serialized = options.serialize().unwrap();
160
161        assert!(serialized.contains("follow=true"));
162        assert!(serialized.contains("stdout=true"));
163        assert!(serialized.contains("stderr=true"));
164        assert!(serialized.contains("timestamps=true"));
165        assert!(serialized.contains("tail=all"));
166        assert!(serialized.contains("since=2147483647"));
167        assert!(serialized.contains("until=2147600000"));
168    }
169}