feat: prepulate command in make-command

Fixed bug where created command file didn't end with `.py` and
prepopulate created file with minimal example of django management command.
This commit is contained in:
Matej Janezic 2023-06-02 17:12:57 +02:00
parent 5d45eccfef
commit e3306c494d
Signed by: janezicmatej
GPG Key ID: 4298E230ED37B2C0
1 changed files with 12 additions and 1 deletions

View File

@ -4,6 +4,7 @@ use super::DockerCommand;
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use std::env; use std::env;
use std::fs::create_dir; use std::fs::create_dir;
use std::io::Write;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
fn get_django_settings_module() -> Result<String> { fn get_django_settings_module() -> Result<String> {
@ -13,6 +14,14 @@ fn get_django_settings_module() -> Result<String> {
Ok(dsm) Ok(dsm)
} }
const DEBUG_TEMPLATE: &str = r#"from django.core.management.base import BaseCommand
class Command(BaseCommand):
def handle(self, *args, **options):
pass
"#;
pub fn make_command(app: &PathBuf, name: &str) -> Result<()> { pub fn make_command(app: &PathBuf, name: &str) -> Result<()> {
let app_name = app.to_string_lossy(); let app_name = app.to_string_lossy();
let app_dir = Path::new(&app); let app_dir = Path::new(&app);
@ -44,7 +53,9 @@ pub fn make_command(app: &PathBuf, name: &str) -> Result<()> {
println!("created module {app_name}.management.commands") println!("created module {app_name}.management.commands")
}; };
safe_create_file(commands_dir.join(name))?; let mut file = safe_create_file(commands_dir.join(format!("{name}.py")))?;
file.write_all(DEBUG_TEMPLATE.as_bytes())?;
println!("created command {app_name}.management.commands.{name}"); println!("created command {app_name}.management.commands.{name}");
Ok(()) Ok(())
} }