feat: finish django cli and add aliases

Implemented basic wanted functionality for django subcommands.

Added single or two letter aliases for all subsubcommands.
This commit is contained in:
2023-05-23 22:49:18 +02:00
parent 907740fc5a
commit 286e16e7ce
8 changed files with 112 additions and 19 deletions

View File

@@ -1,20 +1,73 @@
use anyhow::Result;
use std::path::PathBuf;
use crate::scripts::{create_file, safe_create_file};
pub fn make_command(app: &PathBuf, command: &str) -> Result<()> {
println!("{app:?} and {command:?}");
todo!()
use super::DockerCommand;
use anyhow::{anyhow, Result};
use std::env;
use std::fs::create_dir;
use std::path::{Path, PathBuf};
fn get_django_settings_module() -> Result<String> {
Ok(env::var("DJANGO_SETTINGS_MODULE")?)
}
pub fn make_command(app: &PathBuf, name: &str) -> Result<()> {
let app_name = app.to_string_lossy();
let app_dir = Path::new(&app);
let not_app_exists = !app_dir.is_dir();
if not_app_exists {
return Err(anyhow!("directory {app_name} does not exist"));
}
println!("found app {app_name}");
let management_dir = app_dir.join("management");
let not_management_exists = !management_dir.exists();
if not_management_exists {
create_dir(&management_dir)?;
create_file(management_dir.join("__init__.py"))?;
println!("created module {app_name}.management")
};
let commands_dir = management_dir.join("commands");
let not_commands_exists = !commands_dir.exists();
if not_commands_exists {
create_dir(&commands_dir)?;
create_file(commands_dir.join("__init__.py"))?;
println!("created module {app_name}.management.commands")
};
safe_create_file(commands_dir.join(name))?;
println!("created command {app_name}.management.commands.{name}");
Ok(())
}
pub fn makemigrations() -> Result<()> {
todo!()
let dsm = get_django_settings_module()?;
let command = format!("manage makemigrations --settings={dsm}");
DockerCommand::docker_compose().args(&command).spawn_wait()
}
pub fn manage(command: &[String]) -> Result<()> {
println!("{command:?}");
todo!()
pub fn manage(rest: &[String]) -> Result<()> {
let dsm = get_django_settings_module()?;
let joined = rest.join(" ");
let command = format!("manage {joined} --settings={dsm}");
DockerCommand::docker_compose().args(&command).spawn_wait()
}
pub fn migrate() -> Result<()> {
todo!()
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()
}
pub fn shell() -> Result<()> {
todo!()
let dsm = get_django_settings_module()?;
let command = format!("manage shell --settings={dsm}");
DockerCommand::docker_compose().args(&command).spawn_wait()
}