refractr/src/main.rs

70 lines
1.9 KiB
Rust
Raw Normal View History

mod common;
2025-02-15 22:11:08 -07:00
mod config;
use clap::Parser;
use std::path::PathBuf;
2025-03-02 14:39:12 -07:00
use std::process;
use users;
2025-02-15 22:11:08 -07:00
#[derive(Parser)]
#[command(name = "refractor")]
#[command(version = "0.1.0")]
#[command(about = "An automated push/pull/clone utility for mirroring Git repositories")]
#[command(long_about = None)]
struct Args {
#[arg(short, long, help = "Specify a config file", default_value = get_config_default())]
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
fn get_config_default() -> &'static str {
if cfg!(windows) {
"C:\\ProgramData\\refractr\\config.toml"
} else {
"/etc/refractr/config.toml"
}
}
fn main() -> std::io::Result<()> {
2025-02-15 22:11:08 -07:00
let args = Args::parse();
2025-03-02 11:47:29 -07:00
let refractr = common::Refractr {
2025-03-02 14:39:12 -07:00
verbose: args.verbose,
pid: process::id(),
unix: cfg!(unix)
2025-03-02 11:47:29 -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-02 11:47:29 -07:00
common::verbose(refractr.verbose, 2, format!("Checking for create flag"));
if args.create {
2025-03-02 14:39:12 -07:00
common::verbose(refractr.verbose, 3, format!("Printing sample config"));
let example = include_str!("example/config.toml");
println!("{}", example);
} else {
// warn to avoid root/admin
if refractr.unix {
if users::get_current_uid() == 0 {
eprintln!("refractr: warning: this program should not ran as root")
}
} else {
// TODO: print message for Windows
}
2025-03-02 11:47:29 -07:00
let cfgs = config::read_config(args.config, &refractr);
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 11:47:29 -07:00
for i in cfgs {
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"));
}
Ok(())
2025-02-11 23:32:28 -07:00
}