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:
parent
83c3b05d30
commit
4762c991a3
|
@ -4,10 +4,11 @@ version = 3
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ahab"
|
name = "ahab"
|
||||||
version = "0.1.0"
|
version = "0.0.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"clap",
|
"clap",
|
||||||
|
"dotenvy",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -125,6 +126,12 @@ version = "1.0.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
|
checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "dotenvy"
|
||||||
|
version = "0.15.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "errno"
|
name = "errno"
|
||||||
version = "0.3.1"
|
version = "0.3.1"
|
||||||
|
|
|
@ -13,4 +13,4 @@ repository = "https://github.com/janezicmatej/ahab.git"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { version = "4.3.0", features = ["derive"] }
|
clap = { version = "4.3.0", features = ["derive"] }
|
||||||
anyhow = "1.0.71"
|
anyhow = "1.0.71"
|
||||||
|
dotenvy = "0.15.7"
|
||||||
|
|
|
@ -5,6 +5,9 @@ use anyhow::Result;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
|
// always load dotenv on start
|
||||||
|
dotenvy::dotenv().ok();
|
||||||
|
|
||||||
let args = cli::Ahab::parse();
|
let args = cli::Ahab::parse();
|
||||||
|
|
||||||
match args.command {
|
match args.command {
|
||||||
|
|
|
@ -1,21 +1,34 @@
|
||||||
|
use super::DockerCommand;
|
||||||
|
|
||||||
|
// simple commands
|
||||||
pub fn build() {
|
pub fn build() {
|
||||||
todo!()
|
DockerCommand::docker_compose().args("build").spawn_wait();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn down() {
|
pub fn down() {
|
||||||
todo!()
|
DockerCommand::docker_compose().args("down").spawn_wait();
|
||||||
}
|
|
||||||
pub fn rebuild() {
|
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
pub fn restart() {
|
|
||||||
todo!()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn start() {
|
pub fn start() {
|
||||||
todo!()
|
DockerCommand::docker_compose().args("start").spawn_wait();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn stop() {
|
pub fn stop() {
|
||||||
todo!()
|
DockerCommand::docker_compose().args("stop").spawn_wait();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn up() {
|
pub fn up() {
|
||||||
todo!()
|
DockerCommand::docker_compose().args("up -d").spawn_wait();
|
||||||
|
}
|
||||||
|
|
||||||
|
// shortcuts
|
||||||
|
pub fn rebuild() {
|
||||||
|
stop();
|
||||||
|
build();
|
||||||
|
start();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn restart() {
|
||||||
|
stop();
|
||||||
|
start();
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,3 +2,44 @@ pub mod django;
|
||||||
pub mod docker;
|
pub mod docker;
|
||||||
pub mod docker_compose;
|
pub mod docker_compose;
|
||||||
pub mod postgres;
|
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(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue