use std::collections::BTreeMap;
use std::ffi::{OsStr, OsString};
use std::ops::{Deref, DerefMut};
use crate::args::ToArgs;
#[derive(Debug, Clone, Copy)]
#[allow(missing_copy_implementations)]
pub struct Empty;
impl ToArgs for Empty {
fn to_args(&self) -> Vec<OsString> {
vec![]
}
}
#[derive(Debug, Default, Clone)]
#[allow(missing_copy_implementations)]
#[non_exhaustive]
pub struct Pull {
pub quiet: bool,
}
impl ToArgs for Pull {
fn to_args(&self) -> Vec<OsString> {
let mut args = vec![];
if self.quiet {
args.push(OsString::from("--quiet"));
}
args
}
}
#[derive(Debug, Default, Clone)]
#[allow(missing_copy_implementations)]
#[non_exhaustive]
pub struct Up {
pub init: bool,
}
impl Up {
pub fn new(init: bool) -> Up {
Up { init }
}
}
impl ToArgs for Up {
fn to_args(&self) -> Vec<OsString> {
vec!["-d".into()]
}
}
#[derive(Debug, Clone)]
pub struct Process {
pub detached: bool,
pub user: Option<String>,
pub allocate_tty: bool,
#[doc(hidden)]
pub _nonexhaustive: (),
}
impl ToArgs for Process {
fn to_args(&self) -> Vec<OsString> {
let mut args: Vec<OsString> = vec![];
if self.detached {
args.push(OsStr::new("-d").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 process_options_to_args_returns_empty_for_default_opts() {
assert_eq!(Process::default().to_args(), Vec::<OsString>::new());
}
#[test]
fn process_options_to_args_returns_appropriate_flags() {
let opts = Process {
detached: true,
user: Some("root".to_owned()),
allocate_tty: false,
..Default::default()
};
let raw_expected = &["-d", "--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 Process {
fn default() -> Process {
Process {
detached: false,
user: None,
allocate_tty: true, _nonexhaustive: (),
}
}
}
#[derive(Debug, Clone, Default)]
pub struct Exec {
pub process: Process,
pub privileged: bool,
#[doc(hidden)]
pub _nonexhaustive: (),
}
impl Deref for Exec {
type Target = Process;
fn deref(&self) -> &Self::Target {
&self.process
}
}
impl DerefMut for Exec {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.process
}
}
impl ToArgs for Exec {
fn to_args(&self) -> Vec<OsString> {
let mut args = self.process.to_args();
if self.privileged {
args.push(OsStr::new("--privileged").to_owned());
}
args
}
}
#[test]
fn exec_options_to_args_returns_appropriate_flags() {
let mut opts = Exec::default();
opts.detached = true;
opts.privileged = true;
let raw_expected = &["-d", "--privileged"];
let expected: Vec<OsString> = raw_expected
.iter()
.map(|s| OsStr::new(s).to_owned())
.collect();
assert_eq!(opts.to_args(), expected);
}
#[derive(Debug, Clone, Default)]
pub struct Run {
pub process: Process,
pub environment: BTreeMap<String, String>,
pub entrypoint: Option<String>,
pub no_deps: bool,
#[doc(hidden)]
pub _nonexhaustive: (),
}
#[derive(Debug, Clone, Default)]
#[allow(missing_copy_implementations)]
pub struct Test {
pub export_test_output: bool,
}
impl Deref for Run {
type Target = Process;
fn deref(&self) -> &Self::Target {
&self.process
}
}
impl DerefMut for Run {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.process
}
}
impl ToArgs for Run {
fn to_args(&self) -> Vec<OsString> {
let mut args = self.process.to_args();
for (var, val) in &self.environment {
args.push(OsStr::new("-e").to_owned());
args.push(format!("{}={}", var, val).into());
}
if let Some(ref entrypoint) = self.entrypoint {
args.push(OsStr::new("--entrypoint").to_owned());
args.push(entrypoint.into());
}
if self.no_deps {
args.push(OsStr::new("--no-deps").to_owned());
}
args
}
}
#[test]
fn run_options_to_args_returns_appropriate_flags() {
let mut opts = Run::default();
opts.detached = true;
opts.environment.insert("FOO".to_owned(), "foo".to_owned());
opts.entrypoint = Some("/helper.sh".to_owned());
let raw_expected = &["-d", "-e", "FOO=foo", "--entrypoint", "/helper.sh"];
let expected: Vec<OsString> = raw_expected
.iter()
.map(|s| OsStr::new(s).to_owned())
.collect();
assert_eq!(opts.to_args(), expected);
}
#[derive(Debug, Clone, Default)]
#[allow(missing_copy_implementations)]
pub struct Logs {
pub follow: bool,
pub number: Option<String>,
#[doc(hidden)]
pub _nonexhaustive: (),
}
impl ToArgs for Logs {
fn to_args(&self) -> Vec<OsString> {
let mut args: Vec<OsString> = vec![];
if self.follow {
args.push(OsStr::new("-f").to_owned());
}
if let Some(ref number) = self.number {
args.push(OsStr::new(&format!("--tail={}", number)).to_owned());
}
args
}
}
#[test]
fn logs_options_to_args_returns_appropriate_flags() {
let mut opts = Logs::default();
opts.follow = true;
opts.number = Some("12".to_owned());
let raw_expected = &["-f", "--tail=12"];
let expected: Vec<OsString> = raw_expected
.iter()
.map(|s| OsStr::new(s).to_owned())
.collect();
assert_eq!(opts.to_args(), expected);
}
#[derive(Debug, Clone, Default)]
#[allow(missing_copy_implementations)]
pub struct Rm {
pub force: bool,
pub remove_volumes: bool,
#[doc(hidden)]
pub _nonexhaustive: (),
}
impl ToArgs for Rm {
fn to_args(&self) -> Vec<OsString> {
let mut args: Vec<OsString> = vec![];
if self.force {
args.push(OsStr::new("-f").to_owned());
}
if self.remove_volumes {
args.push(OsStr::new("-v").to_owned());
}
args
}
}
#[test]
fn rm_options_to_args_returns_appropriate_flags() {
let mut opts = Rm::default();
opts.force = true;
opts.remove_volumes = true;
let raw_expected = &["-f", "-v"];
let expected: Vec<OsString> = raw_expected
.iter()
.map(|s| OsStr::new(s).to_owned())
.collect();
assert_eq!(opts.to_args(), expected);
}