feat: swap println for eprintln or debug_eprintln

Unified expected output to always eprintln so outputs can be piped to
other processes. Added debug_eprintln and switched all occurances of
debug_println.
This commit is contained in:
Matej Janezic 2024-10-25 09:00:56 +02:00
parent 6fcf48ed61
commit 73a0b87c31
Signed by: janezicmatej
GPG Key ID: 4298E230ED37B2C0
4 changed files with 17 additions and 17 deletions

View File

@ -9,7 +9,7 @@ use std::{
process::{Child, Command, Stdio}, process::{Child, Command, Stdio},
}; };
use crate::debug_println; use crate::debug_eprintln;
pub struct Args(Vec<String>); pub struct Args(Vec<String>);
@ -64,7 +64,7 @@ impl CommandBuilder {
} }
pub fn build(self) -> Result<Command> { pub fn build(self) -> Result<Command> {
debug_println!("-----\n{self}\n-----"); debug_eprintln!("-----\n{self}\n-----");
let (first, rest) = self.args.split_first().context("empty args")?; let (first, rest) = self.args.split_first().context("empty args")?;
let mut command = Command::new(first); let mut command = Command::new(first);

View File

@ -11,8 +11,8 @@ pub mod scripts;
// NOTE: stolen from https://docs.rs/debug_print/latest/debug_print/ // NOTE: stolen from https://docs.rs/debug_print/latest/debug_print/
#[macro_export] #[macro_export]
macro_rules! debug_println { macro_rules! debug_eprintln {
($($arg:tt)*) => (if ::std::cfg!(debug_assertions) { ::std::println!($($arg)*); }) ($($arg:tt)*) => (#[cfg(debug_assertions)] eprintln!($($arg)*));
} }
fn safe_create_file(path: PathBuf) -> Result<File, std::io::Error> { fn safe_create_file(path: PathBuf) -> Result<File, std::io::Error> {

View File

@ -10,7 +10,7 @@ use crate::{create_file, safe_create_file};
fn get_django_settings_module() -> Result<String> { fn get_django_settings_module() -> Result<String> {
let dsm = env::var("DJANGO_SETTINGS_MODULE")?; let dsm = env::var("DJANGO_SETTINGS_MODULE")?;
println!("USING: {dsm}"); eprintln!("USING: {dsm}");
Ok(dsm) Ok(dsm)
} }
@ -32,7 +32,7 @@ pub fn make_command(app: &PathBuf, name: &str) -> Result<()> {
return Err(anyhow!("directory {app_name} does not exist")); return Err(anyhow!("directory {app_name} does not exist"));
} }
println!("found app {app_name}"); eprintln!("found app {app_name}");
let management_dir = app_dir.join("management"); let management_dir = app_dir.join("management");
@ -41,7 +41,7 @@ pub fn make_command(app: &PathBuf, name: &str) -> Result<()> {
create_dir(&management_dir)?; create_dir(&management_dir)?;
create_file(management_dir.join("__init__.py"))?; create_file(management_dir.join("__init__.py"))?;
println!("created module {app_name}.management") eprintln!("created module {app_name}.management")
}; };
let commands_dir = management_dir.join("commands"); let commands_dir = management_dir.join("commands");
@ -51,20 +51,20 @@ pub fn make_command(app: &PathBuf, name: &str) -> Result<()> {
create_dir(&commands_dir)?; create_dir(&commands_dir)?;
create_file(commands_dir.join("__init__.py"))?; create_file(commands_dir.join("__init__.py"))?;
println!("created module {app_name}.management.commands") eprintln!("created module {app_name}.management.commands")
}; };
let mut file = safe_create_file(commands_dir.join(format!("{name}.py")))?; let mut file = safe_create_file(commands_dir.join(format!("{name}.py")))?;
file.write_all(DEBUG_TEMPLATE.as_bytes())?; file.write_all(DEBUG_TEMPLATE.as_bytes())?;
println!("created command {app_name}.management.commands.{name}"); eprintln!("created command {app_name}.management.commands.{name}");
Ok(()) Ok(())
} }
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!("exec appserver python manage.py {joined} --settings={dsm}"); let command = format!("run --rm appserver python manage.py {joined} --settings={dsm}");
CommandBuilder::docker_compose().args(&command).exec() CommandBuilder::docker_compose().args(&command).exec()
} }

View File

@ -8,7 +8,7 @@ use std::{
}; };
use super::docker_compose; use super::docker_compose;
use crate::{command_builder::CommandBuilder, debug_println}; use crate::{command_builder::CommandBuilder, debug_eprintln};
fn get_containers() -> Result<[String; 2]> { fn get_containers() -> Result<[String; 2]> {
// get db container // get db container
@ -40,13 +40,13 @@ pub fn import(file: &Path) -> Result<()> {
let [db_container, app_containers] = get_containers()?; let [db_container, app_containers] = get_containers()?;
let dump_file = file.to_string_lossy(); let dump_file = file.to_string_lossy();
println!("stopping all containers"); eprintln!("stopping all containers");
docker_compose::stop()?; docker_compose::stop()?;
println!("starting db container"); eprintln!("starting db container");
docker_compose::start(Some("postgresdb"))?; docker_compose::start(Some("postgresdb"))?;
println!("restoring database"); eprintln!("restoring database");
let commands = [ let commands = [
format!("cp {dump_file} {db_container}:/tmp/dbdump"), format!("cp {dump_file} {db_container}:/tmp/dbdump"),
format!("exec {db_container} dropdb -U db db"), format!("exec {db_container} dropdb -U db db"),
@ -55,7 +55,7 @@ pub fn import(file: &Path) -> Result<()> {
]; ];
for command in commands { for command in commands {
debug_println!("waiting until pg_isready"); debug_eprintln!("waiting until pg_isready");
while !CommandBuilder::docker() while !CommandBuilder::docker()
.args(&format!( .args(&format!(
"exec {db_container} pg_isready -h postgresdb -p 5432 -U db", "exec {db_container} pg_isready -h postgresdb -p 5432 -U db",
@ -71,7 +71,7 @@ pub fn import(file: &Path) -> Result<()> {
CommandBuilder::docker().args(&command).exec()?; CommandBuilder::docker().args(&command).exec()?;
} }
println!("restarting containers"); eprintln!("restarting containers");
docker_compose::stop()?; docker_compose::stop()?;
docker_compose::up()?; docker_compose::up()?;
@ -81,7 +81,7 @@ pub fn import(file: &Path) -> Result<()> {
pub fn dump(file: &PathBuf) -> Result<()> { pub fn dump(file: &PathBuf) -> Result<()> {
let [db_container, _] = get_containers()?; let [db_container, _] = get_containers()?;
println!("dumping to local file {}", file.to_string_lossy()); eprintln!("dumping to local file {}", file.to_string_lossy());
let file = File::create(file)?; let file = File::create(file)?;
let stdout = Stdio::from(file); let stdout = Stdio::from(file);