1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use compose_yml::v2 as dc;
use std::ffi::{OsStr, OsString};
use std::marker::PhantomData;
use errors::*;
use ovr::Override;
use pod::Pod;
use project::Project;
pub trait ToArgs {
fn to_args(&self) -> Vec<OsString>;
}
#[derive(Debug, Clone)]
pub struct Options {
pub detached: bool,
pub privileged: bool,
pub user: Option<String>,
pub allocate_tty: bool,
#[doc(hidden)]
pub _nonexhaustive: PhantomData<()>,
}
impl ToArgs for Options {
fn to_args(&self) -> Vec<OsString> {
let mut args: Vec<OsString> = vec![];
if self.detached {
args.push(OsStr::new("-d").to_owned());
}
if self.privileged {
args.push(OsStr::new("--privileged").to_owned());
}
if let Some(ref user) = self.user {
args.push(OsStr::new("--user").to_owned());
args.push(user.into());
}
if !self.allocate_tty {
args.push(OsStr::new("-T").to_owned());
}
args
}
}
#[test]
fn options_to_args_returns_empty_for_default_opts() {
assert_eq!(Options::default().to_args(), Vec::<OsString>::new());
}
#[test]
fn options_to_args_returns_appropriate_flags() {
let opts = Options {
detached: true,
privileged: true,
user: Some("root".to_owned()),
allocate_tty: false,
..Default::default()
};
let raw_expected = &["-d", "--privileged", "--user", "root", "-T"];
let expected: Vec<OsString> = raw_expected.iter()
.map(|s| OsStr::new(s).to_owned())
.collect();
assert_eq!(opts.to_args(), expected);
}
impl Default for Options {
fn default() -> Options {
Options {
detached: false,
privileged: false,
user: None,
allocate_tty: true,
_nonexhaustive: PhantomData,
}
}
}
#[derive(Debug)]
pub struct Target<'a> {
ovr: &'a Override,
pod: &'a Pod,
service_name: &'a str,
service: dc::Service,
}
impl<'a> Target<'a> {
pub fn new(project: &'a Project,
ovr: &'a Override,
pod_name: &'a str,
service_name: &'a str)
-> Result<Target<'a>> {
let pod = try!(project.pod(pod_name)
.ok_or_else(|| err!("Cannot find pod {}", pod_name)));
let file = try!(pod.merged_file(ovr));
let service = try!(file.services
.get(service_name)
.ok_or_else(|| err!("Cannot find service {}", service_name)));
Ok(Target {
ovr: ovr,
pod: pod,
service_name: service_name,
service: service.to_owned(),
})
}
pub fn ovr(&self) -> &Override {
self.ovr
}
pub fn pod(&self) -> &Pod {
self.pod
}
pub fn service_name(&self) -> &str {
self.service_name
}
pub fn service(&self) -> &dc::Service {
&self.service
}
}
#[derive(Debug)]
pub struct Command {
pub command: OsString,
pub args: Vec<OsString>,
}
impl Command {
pub fn new<S: AsRef<OsStr>>(command: S) -> Command {
Command {
command: command.as_ref().to_owned(),
args: vec![],
}
}
pub fn with_args<S: AsRef<OsStr>>(mut self, args: &[S]) -> Command {
self.args = args.iter().map(|a| a.as_ref().to_owned()).collect();
self
}
}
impl ToArgs for Command {
fn to_args(&self) -> Vec<OsString> {
let mut result: Vec<OsString> = vec![];
result.push(self.command.clone());
result.extend(self.args.iter().cloned());
result
}
}
#[test]
fn command_to_args_converts_to_arguments() {
assert_eq!(Command::new("foo").to_args(), vec![OsStr::new("foo")]);
assert_eq!(Command::new("foo").with_args(&["--opt"]).to_args(),
vec![OsStr::new("foo"), OsStr::new("--opt")]);
}