feat: finish docker compose cli

Implemented basic wanted functionality for docker compose subcommands.
We currently assume that docker-compose.yaml file is located in
local/docker/ folder.
This commit is contained in:
Matej Janezic 2023-05-21 23:41:15 +02:00
parent 83c3b05d30
commit 4762c991a3
Signed by: janezicmatej
GPG Key ID: 4298E230ED37B2C0
5 changed files with 77 additions and 13 deletions

9
Cargo.lock generated
View File

@ -4,10 +4,11 @@ version = 3
[[package]]
name = "ahab"
version = "0.1.0"
version = "0.0.1"
dependencies = [
"anyhow",
"clap",
"dotenvy",
]
[[package]]
@ -125,6 +126,12 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
[[package]]
name = "dotenvy"
version = "0.15.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b"
[[package]]
name = "errno"
version = "0.3.1"

View File

@ -13,4 +13,4 @@ repository = "https://github.com/janezicmatej/ahab.git"
[dependencies]
clap = { version = "4.3.0", features = ["derive"] }
anyhow = "1.0.71"
dotenvy = "0.15.7"

View File

@ -5,6 +5,9 @@ use anyhow::Result;
use clap::Parser;
fn main() -> Result<()> {
// always load dotenv on start
dotenvy::dotenv().ok();
let args = cli::Ahab::parse();
match args.command {

View File

@ -1,21 +1,34 @@
use super::DockerCommand;
// simple commands
pub fn build() {
todo!()
DockerCommand::docker_compose().args("build").spawn_wait();
}
pub fn down() {
todo!()
}
pub fn rebuild() {
todo!()
}
pub fn restart() {
todo!()
DockerCommand::docker_compose().args("down").spawn_wait();
}
pub fn start() {
todo!()
DockerCommand::docker_compose().args("start").spawn_wait();
}
pub fn stop() {
todo!()
DockerCommand::docker_compose().args("stop").spawn_wait();
}
pub fn up() {
todo!()
DockerCommand::docker_compose().args("up -d").spawn_wait();
}
// shortcuts
pub fn rebuild() {
stop();
build();
start();
}
pub fn restart() {
stop();
start();
}

View File

@ -2,3 +2,44 @@ pub mod django;
pub mod docker;
pub mod docker_compose;
pub mod postgres;
use std::{ffi::OsStr, process::Command};
use anyhow::{Context, Result};
struct DockerCommand {
command: Command,
}
impl DockerCommand {
fn new<T>(program: T) -> Self
where
T: AsRef<OsStr>,
{
DockerCommand {
command: Command::new(program),
}
}
fn docker() -> Self {
Self::new("docker")
}
fn docker_compose() -> Self {
Self::new("docker").args("compose -f docker/local/docker-compose.yaml")
}
fn args(mut self, args: &str) -> Self {
self.command.args(args.split_whitespace());
self
}
fn spawn_wait(mut self) -> Result<()> {
self.command
.spawn()
.context("failed spawning command")?
.wait()
.context("failed while waiting")?;
Ok(())
}
}