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