2025-03-16 21:05:33 -06:00
|
|
|
/*
|
|
|
|
* Copyright 2025 Bryson Steck <me@brysonsteck.xyz>
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
2025-03-02 11:47:29 -07:00
|
|
|
use crate::common;
|
2025-03-05 20:45:35 -07:00
|
|
|
use crate::refractr::Refractr;
|
2025-03-09 13:26:28 -06:00
|
|
|
|
2025-03-02 11:47:29 -07:00
|
|
|
use core::fmt;
|
2025-03-09 13:26:28 -06:00
|
|
|
use serde_derive::Deserialize;
|
2025-03-02 14:39:12 -07:00
|
|
|
use std::env;
|
2025-03-09 12:16:51 -06:00
|
|
|
use std::fs::{self, File, Metadata};
|
2025-03-09 13:26:28 -06:00
|
|
|
use std::io::Read;
|
|
|
|
use std::path::PathBuf;
|
2025-02-15 22:11:08 -07:00
|
|
|
use toml;
|
|
|
|
|
2025-03-02 11:47:29 -07:00
|
|
|
pub struct ConfigFile {
|
|
|
|
pub path: String,
|
|
|
|
pub file: Metadata,
|
2025-03-20 22:01:50 -06:00
|
|
|
pub config: Config,
|
2025-03-02 11:47:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for ConfigFile {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2025-03-08 20:48:28 -07:00
|
|
|
let mut branches_list = String::from("[");
|
|
|
|
for i in 0..self.config.branches.len() {
|
|
|
|
branches_list = format!("{}{}", branches_list, &self.config.branches[i]);
|
|
|
|
if i < self.config.branches.len() - 1 {
|
|
|
|
branches_list.push_str(", ");
|
2025-03-02 11:47:29 -07:00
|
|
|
}
|
2025-03-08 20:48:28 -07:00
|
|
|
}
|
|
|
|
branches_list.push(']');
|
2025-03-02 11:47:29 -07:00
|
|
|
|
|
|
|
let mut to_list = String::from("[");
|
|
|
|
for i in 0..self.config.to.len() {
|
|
|
|
to_list.push_str(&self.config.to[i]);
|
|
|
|
if i < self.config.to.len() - 1 {
|
|
|
|
to_list.push_str(", ");
|
|
|
|
}
|
2025-03-20 22:01:50 -06:00
|
|
|
}
|
2025-03-02 11:47:29 -07:00
|
|
|
to_list.push(']');
|
|
|
|
|
2025-03-02 14:39:12 -07:00
|
|
|
let work_dir_path = match &self.config.work_dir {
|
|
|
|
None => {
|
|
|
|
if cfg!(windows) {
|
2025-03-20 22:01:50 -06:00
|
|
|
format!(
|
|
|
|
"Using default \"{}\\refractr\"",
|
|
|
|
match env::var("TEMP") {
|
|
|
|
Ok(val) => val,
|
|
|
|
Err(_) => format!("This shouldn't happen!"),
|
|
|
|
}
|
|
|
|
)
|
2025-03-02 14:39:12 -07:00
|
|
|
} else {
|
|
|
|
format!("Using default: /tmp/refractr")
|
|
|
|
}
|
|
|
|
},
|
2025-03-20 22:01:50 -06:00
|
|
|
Some(path) => format!("{}", path),
|
2025-03-02 14:39:12 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
let schedule_interval = match self.config.schedule.interval {
|
|
|
|
None => {
|
|
|
|
if !self.config.schedule.enabled {
|
|
|
|
String::from("")
|
|
|
|
} else {
|
|
|
|
String::from("This shouldn't happen!\n")
|
|
|
|
}
|
|
|
|
},
|
2025-03-20 22:01:50 -06:00
|
|
|
Some(int) => format!(
|
|
|
|
"\n Scheduled interval in seconds: {}",
|
|
|
|
int.to_string()
|
|
|
|
),
|
2025-03-02 14:39:12 -07:00
|
|
|
};
|
|
|
|
|
2025-03-20 22:01:50 -06:00
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"Config file: \"{}\"\n \
|
2025-03-02 14:39:12 -07:00
|
|
|
Is a file: {}\n \
|
|
|
|
Read only: {}\n \
|
|
|
|
Configuration:\n \
|
|
|
|
Git repo to clone: {}\n \
|
|
|
|
Git repos to push clone to: {}\n \
|
|
|
|
Branches of clone to push: {}\n \
|
|
|
|
Working directory: {}\n \
|
|
|
|
SSH key for pushing clone: {}\n \
|
|
|
|
Schedule enabled: {}\
|
2025-03-20 22:01:50 -06:00
|
|
|
{}",
|
|
|
|
self.path,
|
|
|
|
self.file.is_file(),
|
|
|
|
self.file.permissions().readonly(),
|
|
|
|
self.config.from,
|
|
|
|
to_list,
|
|
|
|
branches_list,
|
|
|
|
work_dir_path,
|
|
|
|
self.config.git.ssh_identity_file,
|
|
|
|
self.config.schedule.enabled,
|
|
|
|
schedule_interval
|
|
|
|
)
|
2025-03-02 11:47:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-20 22:01:50 -06:00
|
|
|
#[derive(PartialEq, Deserialize)]
|
2025-03-02 14:39:12 -07:00
|
|
|
pub struct Config {
|
2025-03-02 18:33:33 -07:00
|
|
|
pub from: String,
|
|
|
|
pub to: Vec<String>,
|
2025-03-08 20:48:28 -07:00
|
|
|
pub branches: Vec<String>,
|
2025-03-02 18:33:33 -07:00
|
|
|
pub work_dir: Option<String>,
|
|
|
|
pub git: Git,
|
2025-03-20 22:01:50 -06:00
|
|
|
pub schedule: Schedule,
|
2025-02-15 22:11:08 -07:00
|
|
|
}
|
|
|
|
|
2025-03-20 22:01:50 -06:00
|
|
|
#[derive(PartialEq, Deserialize)]
|
2025-03-03 21:10:31 -07:00
|
|
|
pub struct Git {
|
|
|
|
pub ssh_identity_file: String,
|
2025-02-15 22:11:08 -07:00
|
|
|
}
|
|
|
|
|
2025-03-20 22:01:50 -06:00
|
|
|
#[derive(PartialEq, Deserialize)]
|
2025-03-03 21:10:31 -07:00
|
|
|
pub struct Schedule {
|
|
|
|
pub enabled: bool,
|
2025-03-04 23:30:17 -07:00
|
|
|
pub interval: Option<i32>,
|
2025-02-15 22:11:08 -07:00
|
|
|
}
|
|
|
|
|
2025-03-09 12:16:51 -06:00
|
|
|
pub fn read_config(paths: Vec<PathBuf>, refractr: &Refractr) -> Result<Vec<ConfigFile>, String> {
|
2025-03-02 11:47:29 -07:00
|
|
|
let mut config_files: Vec<ConfigFile> = vec![];
|
2025-02-17 17:41:21 -07:00
|
|
|
for path in paths {
|
2025-03-20 22:01:50 -06:00
|
|
|
common::verbose(
|
|
|
|
refractr.verbose,
|
|
|
|
1,
|
|
|
|
format!(
|
|
|
|
"Reading config file: \"{}\"",
|
|
|
|
String::from(path.to_string_lossy())
|
|
|
|
),
|
|
|
|
);
|
2025-02-17 17:41:21 -07:00
|
|
|
let mut data = String::new();
|
|
|
|
let mut file = match File::open(path.as_path()) {
|
2025-03-20 22:01:50 -06:00
|
|
|
Err(e) => {
|
|
|
|
return Err(format!(
|
|
|
|
"unable to open {}: {}",
|
|
|
|
path.as_path().display(),
|
|
|
|
e
|
|
|
|
))
|
|
|
|
},
|
|
|
|
Ok(file) => file,
|
2025-02-17 17:41:21 -07:00
|
|
|
};
|
2025-02-15 22:11:08 -07:00
|
|
|
|
2025-02-17 17:41:21 -07:00
|
|
|
if let Err(e) = file.read_to_string(&mut data) {
|
2025-03-20 22:01:50 -06:00
|
|
|
return Err(format!(
|
|
|
|
"unable to read {}: {}",
|
|
|
|
path.as_path().display(),
|
|
|
|
e
|
|
|
|
));
|
2025-02-17 17:41:21 -07:00
|
|
|
}
|
|
|
|
|
2025-03-16 14:48:31 -06:00
|
|
|
let config: Config = match toml::from_str(&data) {
|
|
|
|
Ok(c) => c,
|
2025-03-20 22:01:50 -06:00
|
|
|
Err(e) => {
|
|
|
|
return Err(format!(
|
|
|
|
"issues parsing toml file {}: {}",
|
|
|
|
path.as_path().display(),
|
|
|
|
e
|
|
|
|
))
|
|
|
|
},
|
2025-03-16 14:48:31 -06:00
|
|
|
};
|
|
|
|
|
2025-03-02 11:47:29 -07:00
|
|
|
let config_file = ConfigFile {
|
2025-03-02 15:03:49 -07:00
|
|
|
path: match fs::canonicalize(&path) {
|
2025-03-20 22:01:50 -06:00
|
|
|
Err(_) => {
|
|
|
|
return Err(format!(
|
|
|
|
"cannot get absolute path of config file: {}",
|
|
|
|
path.as_path().display()
|
|
|
|
))
|
|
|
|
},
|
|
|
|
Ok(abs) => abs.to_string_lossy().to_string(),
|
2025-03-02 15:03:49 -07:00
|
|
|
},
|
2025-03-02 11:47:29 -07:00
|
|
|
file: match fs::metadata(&path) {
|
2025-03-20 22:01:50 -06:00
|
|
|
Err(_) => {
|
|
|
|
return Err(format!(
|
|
|
|
"cannot obtain metadata for config file: {}",
|
|
|
|
path.as_path().display()
|
|
|
|
))
|
|
|
|
},
|
|
|
|
Ok(metadata) => metadata,
|
2025-03-02 11:47:29 -07:00
|
|
|
},
|
2025-03-16 14:48:31 -06:00
|
|
|
config: match verify_config(&config) {
|
2025-03-20 22:01:50 -06:00
|
|
|
Err(e) => {
|
|
|
|
return Err(format!(
|
|
|
|
"invalid config {}: {}",
|
|
|
|
path.as_path().display(),
|
|
|
|
e
|
|
|
|
))
|
|
|
|
},
|
|
|
|
Ok(_) => config,
|
|
|
|
},
|
2025-03-02 11:47:29 -07:00
|
|
|
};
|
2025-03-02 14:39:12 -07:00
|
|
|
|
2025-03-02 15:03:49 -07:00
|
|
|
let mut dup = false;
|
|
|
|
for i in &config_files {
|
|
|
|
if i.path == config_file.path {
|
2025-03-20 22:01:50 -06:00
|
|
|
common::warning(format!(
|
|
|
|
"skipping config file \"{}\" as it was already read",
|
|
|
|
path.as_path().display()
|
|
|
|
));
|
2025-03-02 15:03:49 -07:00
|
|
|
dup = true;
|
|
|
|
break;
|
2025-03-02 15:47:21 -07:00
|
|
|
} else if i.config == config_file.config {
|
2025-03-20 22:01:50 -06:00
|
|
|
common::warning(format!(
|
|
|
|
"config files \"{}\" and \"{}\" appear to have the same config",
|
|
|
|
i.path, config_file.path
|
|
|
|
));
|
2025-03-02 15:03:49 -07:00
|
|
|
}
|
|
|
|
}
|
2025-03-20 22:01:50 -06:00
|
|
|
|
2025-03-02 15:03:49 -07:00
|
|
|
if !dup {
|
|
|
|
config_files.push(config_file);
|
|
|
|
}
|
2025-02-15 22:11:08 -07:00
|
|
|
}
|
|
|
|
|
2025-03-09 12:16:51 -06:00
|
|
|
return Ok(config_files);
|
2025-02-17 17:41:21 -07:00
|
|
|
}
|
|
|
|
|
2025-03-16 14:48:31 -06:00
|
|
|
fn verify_config(config: &Config) -> Result<(), String> {
|
2025-03-02 11:47:29 -07:00
|
|
|
if config.schedule.enabled {
|
2025-03-16 14:48:31 -06:00
|
|
|
match config.schedule.interval {
|
|
|
|
Some(i) => {
|
|
|
|
if i < 60 {
|
2025-03-20 22:01:50 -06:00
|
|
|
return Err(format!("schedule is enabled, but less than 60"));
|
2025-03-16 14:48:31 -06:00
|
|
|
}
|
|
|
|
},
|
2025-03-20 22:01:50 -06:00
|
|
|
None => return Err(format!("schedule is enabled, but no interval was defined")),
|
2025-03-16 14:48:31 -06:00
|
|
|
}
|
2025-03-02 11:47:29 -07:00
|
|
|
}
|
|
|
|
|
2025-03-16 14:48:31 -06:00
|
|
|
match &config.work_dir {
|
2025-03-02 14:39:12 -07:00
|
|
|
Some(path) => format!("{}", path),
|
|
|
|
None => {
|
|
|
|
if cfg!(windows) {
|
|
|
|
match env::var("TEMP") {
|
|
|
|
Ok(val) => val,
|
2025-03-20 22:01:50 -06:00
|
|
|
Err(_) => return Err(format!("cannot determine the default temp dir")),
|
2025-03-02 14:39:12 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
format!("/tmp/refractr")
|
|
|
|
}
|
|
|
|
},
|
2025-03-16 14:48:31 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
if !&config.from.starts_with("ssh://")
|
|
|
|
&& !&config.from.starts_with("https://")
|
2025-03-20 22:01:50 -06:00
|
|
|
&& !&config.from.starts_with("http://")
|
|
|
|
{
|
|
|
|
return Err(format!("'from' value does not use a supported protocol"));
|
2025-03-16 14:48:31 -06:00
|
|
|
}
|
2025-03-02 14:39:12 -07:00
|
|
|
|
2025-03-16 14:48:31 -06:00
|
|
|
Ok(())
|
2025-03-02 11:47:29 -07:00
|
|
|
}
|