2021-12-30 02:47:34 +00:00
|
|
|
pub mod config;
|
2021-12-30 13:37:53 +00:00
|
|
|
pub mod daemon;
|
2021-12-31 15:17:55 +00:00
|
|
|
pub mod git;
|
2021-12-30 19:17:42 +00:00
|
|
|
pub mod path;
|
2021-12-29 16:49:24 +00:00
|
|
|
|
2021-12-30 13:37:53 +00:00
|
|
|
use config::PathConfig;
|
2021-12-30 21:53:29 +00:00
|
|
|
use path::ResPathBuf;
|
2022-01-01 17:10:57 +00:00
|
|
|
use std::{error::Error, io};
|
2021-12-29 13:15:19 +00:00
|
|
|
|
2022-01-02 18:48:18 +00:00
|
|
|
type Result = std::result::Result<(), Box<dyn Error>>;
|
|
|
|
|
|
|
|
pub fn run_apply(config: PathConfig) -> Result {
|
|
|
|
let repo = git::init(&config)?;
|
|
|
|
git::apply(&config, &repo)?;
|
2021-12-30 03:38:04 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-01-02 18:48:18 +00:00
|
|
|
pub fn run_daemon(config: PathConfig, freq_secs: u64) -> Result {
|
2022-01-06 12:46:39 +00:00
|
|
|
let repo = git::init(&config)?;
|
|
|
|
daemon::launch(config, repo, freq_secs)?;
|
2021-12-30 13:37:53 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-01-02 18:48:18 +00:00
|
|
|
pub fn run_init(candidates: Vec<ResPathBuf>) -> Result {
|
2021-12-30 19:17:42 +00:00
|
|
|
debug_assert!(!candidates.is_empty(), "Empty candidates found in `init`.");
|
|
|
|
if candidates.is_empty() {
|
2022-01-01 14:24:01 +00:00
|
|
|
Err(config::Error::IOError(io::Error::new(
|
2021-12-30 19:17:42 +00:00
|
|
|
io::ErrorKind::NotFound,
|
|
|
|
"No suitable config file found.",
|
2022-01-01 01:12:21 +00:00
|
|
|
)))?;
|
2021-12-30 19:17:42 +00:00
|
|
|
}
|
2022-01-01 17:10:57 +00:00
|
|
|
let config = match config::load(&candidates) {
|
2021-12-30 13:37:53 +00:00
|
|
|
// Check if we already have a local config somewhere. If so, reprompt
|
|
|
|
// the same configuration options and override the values present in the
|
|
|
|
// current YAML file.
|
2022-01-02 18:48:18 +00:00
|
|
|
Ok(loaded) => config::write(&loaded.homesync_yml, Some(loaded.config))?,
|
2021-12-30 13:37:53 +00:00
|
|
|
// Otherwise create a new config file at the given location. We always
|
|
|
|
// assume we want to write to the first file in our priority list. If
|
|
|
|
// not, the user should specify which config they want to write using
|
|
|
|
// the `-c` flag.
|
2021-12-30 03:38:04 +00:00
|
|
|
// TODO(jrpotter): Verify I have permission to write at specified path.
|
|
|
|
// Make directories if necessary.
|
2021-12-30 13:37:53 +00:00
|
|
|
Err(config::Error::MissingConfig) if !candidates.is_empty() => {
|
2022-01-01 17:10:57 +00:00
|
|
|
config::write(&candidates[0], None)?
|
2021-12-30 13:37:53 +00:00
|
|
|
}
|
2022-01-01 01:12:21 +00:00
|
|
|
Err(e) => Err(e)?,
|
2022-01-01 17:10:57 +00:00
|
|
|
};
|
|
|
|
git::init(&config)?;
|
2022-01-02 16:14:05 +00:00
|
|
|
println!("\nFinished initialization.");
|
2022-01-01 17:10:57 +00:00
|
|
|
Ok(())
|
2021-12-29 16:49:24 +00:00
|
|
|
}
|
|
|
|
|
2022-01-02 18:48:18 +00:00
|
|
|
pub fn run_list(config: PathConfig) -> Result {
|
2022-01-01 14:24:01 +00:00
|
|
|
config::list_packages(config);
|
2021-12-30 13:37:53 +00:00
|
|
|
Ok(())
|
2021-12-29 13:15:19 +00:00
|
|
|
}
|
2022-01-06 13:54:46 +00:00
|
|
|
|
|
|
|
pub fn run_push(config: PathConfig) -> Result {
|
|
|
|
let mut repo = git::init(&config)?;
|
|
|
|
git::push(&config, &mut repo)?;
|
|
|
|
Ok(())
|
|
|
|
}
|