refactor: give every command we run a type of its own

This commit is contained in:
2026-09-08 11:54:21 +00:00
parent 3d06f7dcb0
commit d7626d9f5e
16 changed files with 968 additions and 324 deletions

69
src/cmd/docker.rs Normal file
View 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)
}
}