2025-02-17 17:41:21 -07:00
|
|
|
mod common;
|
2025-02-15 22:11:08 -07:00
|
|
|
mod config;
|
2025-03-02 16:36:04 -07:00
|
|
|
mod refractr;
|
2025-02-15 22:11:08 -07:00
|
|
|
|
|
|
|
use clap::Parser;
|
|
|
|
use std::path::PathBuf;
|
2025-03-02 14:39:12 -07:00
|
|
|
use std::process;
|
2025-03-09 12:58:03 -06:00
|
|
|
use std::env;
|
2025-03-02 15:47:21 -07:00
|
|
|
use users;
|
2025-03-05 20:45:35 -07:00
|
|
|
use crate::refractr::Refractr;
|
2025-02-15 22:11:08 -07:00
|
|
|
|
|
|
|
#[derive(Parser)]
|
2025-03-09 12:16:51 -06:00
|
|
|
#[command(name = "refractr")]
|
2025-03-09 12:26:48 -06:00
|
|
|
#[command(version = option_env!("CARGO_PKG_VERSION"))]
|
2025-03-09 12:16:51 -06:00
|
|
|
#[command(about = "An automated pull/push utility for mirroring Git repositories")]
|
2025-02-15 22:11:08 -07:00
|
|
|
#[command(long_about = None)]
|
|
|
|
struct Args {
|
2025-03-02 15:03:49 -07:00
|
|
|
#[arg(short, long, help = "Specify a config file", default_value = get_config_default())]
|
2025-02-17 17:41:21 -07:00
|
|
|
config: Vec<PathBuf>,
|
2025-02-15 22:11:08 -07:00
|
|
|
|
|
|
|
#[arg(short, long, help = "Specify the level of verbosity", action = clap::ArgAction::Count)]
|
|
|
|
verbose: u8,
|
|
|
|
|
|
|
|
#[arg(short = 'e', long, help = "Output a full, commented config file and exit")]
|
|
|
|
create: bool,
|
|
|
|
}
|
2025-02-11 23:32:28 -07:00
|
|
|
|
2025-03-02 15:03:49 -07:00
|
|
|
fn get_config_default() -> &'static str {
|
|
|
|
if cfg!(windows) {
|
|
|
|
"C:\\ProgramData\\refractr\\config.toml"
|
|
|
|
} else {
|
|
|
|
"/etc/refractr/config.toml"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-09 12:16:51 -06:00
|
|
|
fn main() -> Result<(), String> {
|
2025-02-15 22:11:08 -07:00
|
|
|
let args = Args::parse();
|
2025-03-05 20:45:35 -07:00
|
|
|
let refractr = Refractr {
|
2025-03-09 12:58:03 -06:00
|
|
|
docker: match option_env!("REFRACTR_DOCKER") {
|
|
|
|
Some(v) => match v {
|
|
|
|
"true" => true,
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
None => false
|
|
|
|
},
|
2025-03-02 14:39:12 -07:00
|
|
|
pid: process::id(),
|
2025-03-09 12:58:03 -06:00
|
|
|
unix: cfg!(unix),
|
|
|
|
verbose: args.verbose
|
2025-03-02 11:47:29 -07:00
|
|
|
};
|
2025-02-17 17:41:21 -07:00
|
|
|
|
2025-03-02 14:39:12 -07:00
|
|
|
common::verbose(refractr.verbose, 1, format!("Level {} verbosity enabled", refractr.verbose.to_string()));
|
2025-03-09 12:16:51 -06:00
|
|
|
common::verbose(refractr.verbose, 3, format!("Process ID: {}", refractr.pid));
|
2025-03-09 12:58:03 -06:00
|
|
|
common::verbose(refractr.verbose, 3, format!("Running in Docker: {}", refractr.docker));
|
2025-03-02 11:47:29 -07:00
|
|
|
common::verbose(refractr.verbose, 2, format!("Checking for create flag"));
|
2025-02-17 17:41:21 -07:00
|
|
|
if args.create {
|
2025-03-02 14:39:12 -07:00
|
|
|
common::verbose(refractr.verbose, 3, format!("Printing sample config"));
|
2025-02-17 17:41:21 -07:00
|
|
|
let example = include_str!("example/config.toml");
|
|
|
|
println!("{}", example);
|
2025-03-02 18:33:33 -07:00
|
|
|
Ok(())
|
2025-02-17 17:41:21 -07:00
|
|
|
} else {
|
2025-03-02 15:47:21 -07:00
|
|
|
// warn to avoid root/admin
|
|
|
|
if refractr.unix {
|
|
|
|
if users::get_current_uid() == 0 {
|
2025-03-09 12:16:51 -06:00
|
|
|
common::warning(format!("this program should not ran as root"));
|
2025-03-02 15:47:21 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// TODO: print message for Windows
|
|
|
|
}
|
|
|
|
|
2025-03-09 12:16:51 -06:00
|
|
|
let cfgs = match config::read_config(args.config, &refractr) {
|
|
|
|
Ok(cfgs) => cfgs,
|
|
|
|
Err(e) => freak_out!(e)
|
|
|
|
};
|
2025-03-02 11:47:29 -07:00
|
|
|
if refractr.verbose >= 2 {
|
2025-03-02 14:39:12 -07:00
|
|
|
// no need to loop over configs if verbose is not at the correct level
|
2025-03-02 18:33:33 -07:00
|
|
|
for i in &cfgs {
|
2025-03-02 11:47:29 -07:00
|
|
|
common::verbose(refractr.verbose, 2, format!("{}", i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-02 14:39:12 -07:00
|
|
|
common::verbose(refractr.verbose, 1, format!("Config file(s) read successfully"));
|
2025-03-05 20:45:35 -07:00
|
|
|
refractr.run(cfgs)
|
2025-02-17 17:41:21 -07:00
|
|
|
}
|
2025-03-02 15:47:21 -07:00
|
|
|
|
2025-02-11 23:32:28 -07:00
|
|
|
}
|