diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1a7d453 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +FROM rust:latest + +WORKDIR /usr/src/refractr +COPY . . + +RUN cargo install --path . + +CMD ["refractr"] \ No newline at end of file diff --git a/src/common.rs b/src/common.rs new file mode 100644 index 0000000..65d52d4 --- /dev/null +++ b/src/common.rs @@ -0,0 +1,11 @@ + +pub fn verbose(level: u8, msg_lvl: u8, msg: String) { + if level < msg_lvl { return }; + let mut prefix = String::new(); + for i in 0..msg_lvl { + prefix += "="; + } + + prefix += "> "; + eprintln!("{}{}", prefix, msg); +} \ No newline at end of file diff --git a/src/config.rs b/src/config.rs index 577c7f3..8baf665 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,11 +3,10 @@ use std::path::PathBuf; use std::fs::File; use toml; use serde_derive::Deserialize; -use std::process; #[derive(Deserialize)] pub struct Config { - from: Vec, + from: String, to: Vec, branches: Vec, git: Git, @@ -25,17 +24,25 @@ struct Schedule { duration: i32, } -pub fn read_config(path: PathBuf) -> Config { - let mut data = String::new(); - let mut file = match File::open(path.as_path()) { - Err(e) => panic!("unable to open {}: {}", path.as_path().display(), e), - Ok(file) => file - }; +pub fn read_config(paths: Vec) -> Vec { + let mut configs: Vec = vec![]; + for path in paths { + let mut data = String::new(); + let mut file = match File::open(path.as_path()) { + Err(e) => panic!("unable to open {}: {}", path.as_path().display(), e), + Ok(file) => file + }; - if let Err(e) = file.read_to_string(&mut data) { - panic!("unable to read {}: {}", path.as_path().display(), e) + if let Err(e) = file.read_to_string(&mut data) { + panic!("unable to read {}: {}", path.as_path().display(), e) + } + + configs.push(verify_config(toml::from_str(&data).unwrap())); } - let config: Config = toml::from_str(&data).unwrap(); + return configs; +} + +fn verify_config(config: Config) -> Config { return config; } \ No newline at end of file diff --git a/src/example/config.toml b/src/example/config.toml new file mode 100644 index 0000000..720feea --- /dev/null +++ b/src/example/config.toml @@ -0,0 +1,35 @@ +# *********************************** +# Example configuration for refractr +# *********************************** + +[config] +# The "from" field is a string of the original/main repository you want to pull +# This field is REQUIRED +#from = "https://git.brysonsteck.xyz/brysonsteck/refractr" + +# The "to" field is a list of strings of the repositories you want to push the repo +# from the "from" field to. These repositories must exist on the remote server +# It's recommended that you use SSH to avoid HTTPS authentication +# This field is REQUIRED +#to = ["git@codeberg.org:brysonsteck/refractr.git", "git@github.com:brysonsteck/refractr.git"] + +# The "branches" field is a list of branches you want to mirror from the original +# repository. +# This field is OPTIONAL, will default to ["master"] if omitted +#branches = ["master"] + +[git] +# The "ssh_identity_file" is your private SSH key that you will use to push updates +# from the original repository. +# This field is REQUIRED if you are using SSH to push, otherwise OPTIONAL +#ssh_identity_file = "/path/to/.ssh/id_rsa" + +[schedule] +# The "enabled" field turns on the schedule feature of refractr. +# This field is REQUIRED. +#enabled = false + +# The "duration" field is the amount of seconds refractor will wait before +# pulling updates from the original repository if the schedule feature is enabled. +# This field is REQUIRED if "enabled" is set to true, otherwise OPTIONAL +#duration = 300 diff --git a/src/main.rs b/src/main.rs index 1651a75..16f708c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +mod common; mod config; use clap::Parser; @@ -10,7 +11,7 @@ use std::path::PathBuf; #[command(long_about = None)] struct Args { #[arg(short, long, help = "Specify a config file", default_value = "/etc/refractr/config.toml")] - config: PathBuf, + config: Vec, #[arg(short, long, help = "Specify the level of verbosity", action = clap::ArgAction::Count)] verbose: u8, @@ -21,5 +22,16 @@ struct Args { fn main() { let args = Args::parse(); - let cfg = config::read_config(args.config); + if args.verbose >= 1 { + common::verbose(args.verbose, 1, format!("Level {} verbosity enabled", args.verbose.to_string())); + } + + common::verbose(args.verbose, 2, format!("Checking for create flag")); + if args.create { + common::verbose(args.verbose, 2, format!("Printing sample config")); + let example = include_str!("example/config.toml"); + println!("{}", example); + } else { + let cfg = config::read_config(args.config); + } }