hesinde-sync/app/src/main.rs

42 lines
1.0 KiB
Rust

use std::{
fs::File,
io::{self, BufRead, Read},
};
use anyhow::Result;
use clap::Parser;
use hesinde_sync::cli::Config;
#[tokio::main]
async fn main() -> Result<()> {
if std::env::var_os("RUST_LOG").is_none() {
std::env::set_var("RUST_LOG", "info");
}
tracing_subscriber::fmt::init();
let args = Config::parse();
let db_url = read_db_url(&args.db_connection)?;
hesinde_sync::run(&args, &db_url).await
}
/// Read db url from file or stdin.
fn read_db_url(db_arg: &str) -> Result<String> {
if db_arg == "-" {
let stdin = io::stdin();
let mut buffer = String::new();
stdin.lock().read_to_string(&mut buffer)?;
let db_url = buffer.trim();
Ok(db_url.to_string())
} else {
let file = File::open(db_arg)?;
let mut reader = io::BufReader::new(file);
let mut first_line = String::new();
reader.read_line(&mut first_line)?;
let first_line = first_line.trim();
Ok(first_line.to_string())
}
}