Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
55d4ae3f6f
|
|||
|
de5e6b4d10
|
|||
|
61e7556997
|
|||
|
a9a5e401dc
|
|||
|
423badfe07
|
12
Cargo.lock
generated
12
Cargo.lock
generated
@@ -4,10 +4,11 @@ version = 3
|
||||
|
||||
[[package]]
|
||||
name = "ahab"
|
||||
version = "0.1.0"
|
||||
version = "0.2.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"clap",
|
||||
"clap_complete",
|
||||
"dotenvy",
|
||||
]
|
||||
|
||||
@@ -102,6 +103,15 @@ dependencies = [
|
||||
"strsim",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_complete"
|
||||
version = "4.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a04ddfaacc3bc9e6ea67d024575fafc2a813027cf374b8f24f7bc233c6b6be12"
|
||||
dependencies = [
|
||||
"clap",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_derive"
|
||||
version = "4.3.0"
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
name = "ahab"
|
||||
description = "docker cli wrapper"
|
||||
readme = "README.md"
|
||||
version = "0.1.0"
|
||||
version = "0.2.0"
|
||||
edition = "2021"
|
||||
license = "MIT"
|
||||
authors = ["Matej Janežič <janezic.mj@gmail.com>"]
|
||||
@@ -12,5 +12,6 @@ repository = "https://github.com/janezicmatej/ahab.git"
|
||||
|
||||
[dependencies]
|
||||
clap = { version = "4.3.0", features = ["derive"] }
|
||||
clap_complete = "4.3.0"
|
||||
anyhow = "1.0.71"
|
||||
dotenvy = "0.15.7"
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use super::{Django, Docker, DockerCompose, Postgres};
|
||||
use clap::{Parser, Subcommand};
|
||||
use clap_complete::Shell;
|
||||
|
||||
/// A program for interacting with various dockerized applications.
|
||||
#[derive(Parser, Debug)]
|
||||
@@ -11,26 +12,32 @@ pub struct Ahab {
|
||||
|
||||
#[derive(Debug, Subcommand)]
|
||||
pub enum Commands {
|
||||
/// Generate completion files
|
||||
Completion {
|
||||
#[arg(value_enum)]
|
||||
shell: Shell,
|
||||
},
|
||||
|
||||
/// Docker related subcommands
|
||||
D {
|
||||
Docker {
|
||||
#[command(subcommand)]
|
||||
command: Docker,
|
||||
},
|
||||
|
||||
/// Docker compose related subcommands
|
||||
Dc {
|
||||
Compose {
|
||||
#[command(subcommand)]
|
||||
command: DockerCompose,
|
||||
},
|
||||
|
||||
/// Docker compose related subcommands
|
||||
Dj {
|
||||
Django {
|
||||
#[command(subcommand)]
|
||||
command: Django,
|
||||
},
|
||||
|
||||
/// Postgres related subcommands
|
||||
Pg {
|
||||
Postgres {
|
||||
#[command(subcommand)]
|
||||
command: Postgres,
|
||||
},
|
||||
|
||||
@@ -7,7 +7,6 @@ use clap::Parser;
|
||||
#[derive(Parser, Debug)]
|
||||
pub enum Django {
|
||||
/// Prepare empty management command 'command' in app 'app'.
|
||||
#[clap(alias("mc"))]
|
||||
MakeCommand {
|
||||
#[arg(value_enum)]
|
||||
app: PathBuf,
|
||||
@@ -16,24 +15,20 @@ pub enum Django {
|
||||
},
|
||||
|
||||
/// Run Django's manage.py makemigrations.
|
||||
#[clap(alias("mm"))]
|
||||
Makemigrations,
|
||||
|
||||
/// Pass arguments to Django's manage.py.
|
||||
#[clap(alias("m"))]
|
||||
Manage {
|
||||
#[arg(value_enum)]
|
||||
rest: Vec<String>,
|
||||
},
|
||||
|
||||
/// Run Django's manage.py migrate.
|
||||
#[clap(alias("mg"))]
|
||||
Migrate {
|
||||
#[arg(value_enum)]
|
||||
rest: Vec<String>,
|
||||
},
|
||||
|
||||
/// Run Django's manage.py shell.
|
||||
#[clap(alias("s"))]
|
||||
Shell,
|
||||
}
|
||||
|
||||
@@ -3,6 +3,5 @@ use clap::Parser;
|
||||
#[derive(Parser, Debug)]
|
||||
pub enum Docker {
|
||||
/// Stop all containers via `docker stop $(docker ps -q)`
|
||||
#[clap(alias("sa"))]
|
||||
StopAll,
|
||||
}
|
||||
|
||||
@@ -6,30 +6,23 @@ use clap::Subcommand;
|
||||
#[derive(Subcommand, Debug)]
|
||||
pub enum DockerCompose {
|
||||
/// Build containers.
|
||||
#[clap(alias("b"))]
|
||||
Build,
|
||||
|
||||
/// Down containers.
|
||||
#[clap(alias("d"))]
|
||||
Down,
|
||||
|
||||
/// Stop, build and start containers.
|
||||
#[clap(alias("rb"))]
|
||||
Rebuild,
|
||||
|
||||
/// Stop and start containers.
|
||||
#[clap(alias("rs"))]
|
||||
Restart,
|
||||
|
||||
/// Start containers.
|
||||
#[clap(alias("st"))]
|
||||
Start,
|
||||
|
||||
/// Stop containers.
|
||||
#[clap(alias("s"))]
|
||||
Stop,
|
||||
|
||||
/// Up containers.
|
||||
#[clap(alias("u"))]
|
||||
Up,
|
||||
}
|
||||
|
||||
@@ -5,14 +5,12 @@ use clap::Subcommand;
|
||||
#[derive(Subcommand, Debug)]
|
||||
pub enum Postgres {
|
||||
/// Import dump via pg_restore
|
||||
#[clap(alias("i"))]
|
||||
Import {
|
||||
#[arg(value_enum)]
|
||||
path: PathBuf,
|
||||
},
|
||||
|
||||
/// Dump via pg_dump with format=c
|
||||
#[clap(alias("d"))]
|
||||
Dump {
|
||||
#[arg(value_enum)]
|
||||
path: PathBuf,
|
||||
|
||||
10
src/lib.rs
Normal file
10
src/lib.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
#![allow(unused)]
|
||||
|
||||
pub mod cli;
|
||||
pub mod scripts;
|
||||
|
||||
// NOTE: stolen from https://docs.rs/debug_print/latest/debug_print/
|
||||
#[macro_export]
|
||||
macro_rules! debug_println {
|
||||
($($arg:tt)*) => (if ::std::cfg!(debug_assertions) { ::std::println!($($arg)*); })
|
||||
}
|
||||
25
src/main.rs
25
src/main.rs
@@ -1,11 +1,7 @@
|
||||
#![allow(unused)]
|
||||
|
||||
mod cli;
|
||||
mod scripts;
|
||||
use ahab::{cli, scripts};
|
||||
|
||||
use anyhow::Result;
|
||||
use clap::Parser;
|
||||
use std::env;
|
||||
use clap::{CommandFactory, Parser};
|
||||
|
||||
fn main() -> Result<()> {
|
||||
// always load dotenv on start
|
||||
@@ -17,10 +13,19 @@ fn main() -> Result<()> {
|
||||
let args = cli::Ahab::parse();
|
||||
|
||||
match args.command {
|
||||
cli::Commands::D { command } => match command {
|
||||
cli::Commands::Completion { shell } => {
|
||||
clap_complete::generate(
|
||||
shell,
|
||||
&mut cli::Ahab::command(),
|
||||
"ahab",
|
||||
&mut std::io::stdout(),
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
cli::Commands::Docker { command } => match command {
|
||||
cli::Docker::StopAll => scripts::docker::stop_all(),
|
||||
},
|
||||
cli::Commands::Dc { command } => match command {
|
||||
cli::Commands::Compose { command } => match command {
|
||||
cli::DockerCompose::Build => scripts::docker_compose::build(),
|
||||
cli::DockerCompose::Down => scripts::docker_compose::down(),
|
||||
cli::DockerCompose::Rebuild => scripts::docker_compose::rebuild(),
|
||||
@@ -29,14 +34,14 @@ fn main() -> Result<()> {
|
||||
cli::DockerCompose::Stop => scripts::docker_compose::stop(),
|
||||
cli::DockerCompose::Up => scripts::docker_compose::up(),
|
||||
},
|
||||
cli::Commands::Dj { command } => match command {
|
||||
cli::Commands::Django { command } => match command {
|
||||
cli::Django::MakeCommand { app, name } => scripts::django::make_command(&app, &name),
|
||||
cli::Django::Makemigrations => scripts::django::makemigrations(),
|
||||
cli::Django::Manage { rest } => scripts::django::manage(&rest),
|
||||
cli::Django::Migrate { rest } => scripts::django::migrate(&rest),
|
||||
cli::Django::Shell => scripts::django::shell(),
|
||||
},
|
||||
cli::Commands::Pg { command } => match command {
|
||||
cli::Commands::Postgres { command } => match command {
|
||||
cli::Postgres::Import { path } => scripts::postgres::import(&path),
|
||||
cli::Postgres::Dump { path } => scripts::postgres::dump(&path),
|
||||
},
|
||||
|
||||
@@ -46,28 +46,25 @@ pub fn make_command(app: &PathBuf, name: &str) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn makemigrations() -> Result<()> {
|
||||
let dsm = get_django_settings_module()?;
|
||||
let command = format!("manage makemigrations --settings={dsm}");
|
||||
DockerCommand::docker_compose().args(&command).spawn_wait()
|
||||
}
|
||||
|
||||
pub fn manage(rest: &[String]) -> Result<()> {
|
||||
let dsm = get_django_settings_module()?;
|
||||
let joined = rest.join(" ");
|
||||
let command = format!("manage {joined} --settings={dsm}");
|
||||
let command = format!("exec appserver python manage.py {joined} --settings={dsm}");
|
||||
DockerCommand::docker_compose().args(&command).spawn_wait()
|
||||
}
|
||||
|
||||
pub fn migrate(rest: &[String]) -> Result<()> {
|
||||
let dsm = get_django_settings_module()?;
|
||||
let joined = rest.join(" ");
|
||||
let command = format!("manage migrate {joined} --settings={dsm}");
|
||||
DockerCommand::docker_compose().args(&command).spawn_wait()
|
||||
// shortcuts
|
||||
pub fn makemigrations() -> Result<()> {
|
||||
manage(&["makemigrations".to_string()])
|
||||
}
|
||||
|
||||
pub fn shell() -> Result<()> {
|
||||
let dsm = get_django_settings_module()?;
|
||||
let command = format!("manage shell --settings={dsm}");
|
||||
DockerCommand::docker_compose().args(&command).spawn_wait()
|
||||
manage(&["shell".to_string()])
|
||||
}
|
||||
|
||||
pub fn migrate(rest: &[String]) -> Result<()> {
|
||||
let mut full_rest = vec!["migrate".to_string()];
|
||||
full_rest.extend_from_slice(rest);
|
||||
|
||||
manage(&full_rest)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user