5 Commits

Author SHA1 Message Date
55d4ae3f6f release: version 0.2.0
Command renamings and compltion generation marks v0.2.0 of `ahab`.
2023-05-27 13:32:46 +02:00
de5e6b4d10 feat: rename commands and remove aliases in favour of completion
BREAKING CHANGE: changed commands as follows
D -> Docker
Dc -> Compose
Dj -> Django
Pg -> Postgres
2023-05-27 13:30:09 +02:00
61e7556997 feat: add clap_complete for generating completion files
`ahab generate <shell>` can now be used to generte completion files for
this tool.
2023-05-27 13:28:05 +02:00
a9a5e401dc refactor: create library with simple entrypoint in main
Create a library entrypoint 'src/lib.rs' and refactor binary entrypoint
'src/main.rs' accordingly.
2023-05-27 12:52:10 +02:00
423badfe07 fix: all django management command related subcommands
We just called `manage` inside docker instead of
`exec appserver python manage.py`. Now we make assumption that django
app is running in docker-compose in service named `appserver`.

While fixing this bug we also refactored for `shell`, `makemigrations`
and `migrate` to all call `manage` under the hood to avoid repeating code.
2023-05-24 00:21:04 +02:00
10 changed files with 61 additions and 46 deletions

12
Cargo.lock generated
View File

@@ -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"

View File

@@ -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"

View File

@@ -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,
},

View File

@@ -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,
}

View File

@@ -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,
}

View File

@@ -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,
}

View File

@@ -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
View 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)*); })
}

View File

@@ -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),
},

View File

@@ -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)
}