refactor: give every command we run a type of its own
This commit is contained in:
69
src/cmd/docker.rs
Normal file
69
src/cmd/docker.rs
Normal file
@@ -0,0 +1,69 @@
|
||||
use super::{Argv, Cmd};
|
||||
|
||||
// docker exec, around a command that runs inside the container
|
||||
pub struct Exec {
|
||||
container: String,
|
||||
interactive: bool,
|
||||
inner: Argv,
|
||||
}
|
||||
|
||||
impl Exec {
|
||||
pub fn wrapping(container: &str, inner: Argv) -> Self {
|
||||
Self {
|
||||
container: container.to_string(),
|
||||
interactive: false,
|
||||
inner,
|
||||
}
|
||||
}
|
||||
|
||||
// keep stdin open, for a command that is fed a dump
|
||||
pub fn interactive(mut self) -> Self {
|
||||
self.interactive = true;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Cmd for Exec {
|
||||
fn argv(&self) -> Argv {
|
||||
let mut argv = Argv::new("docker").arg("exec");
|
||||
|
||||
if self.interactive {
|
||||
argv = argv.arg("--interactive");
|
||||
}
|
||||
|
||||
argv.arg(&self.container).args(self.inner.words())
|
||||
}
|
||||
}
|
||||
|
||||
// docker cp, in either direction
|
||||
pub struct Cp {
|
||||
from: String,
|
||||
to: String,
|
||||
}
|
||||
|
||||
impl Cp {
|
||||
pub fn into_container(local: &str, container: &str, remote: &str) -> Self {
|
||||
Self {
|
||||
from: local.to_string(),
|
||||
to: format!("{container}:{remote}"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn out_of_container(container: &str, remote: &str, local: &str) -> Self {
|
||||
Self {
|
||||
from: format!("{container}:{remote}"),
|
||||
to: local.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Cmd for Cp {
|
||||
fn argv(&self) -> Argv {
|
||||
// -L has no long form; it copies what a symlink points at
|
||||
Argv::new("docker")
|
||||
.arg("cp")
|
||||
.arg("-L")
|
||||
.arg(&self.from)
|
||||
.arg(&self.to)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user