Commit 0c7b8f8d authored by Stavros Korokithakis's avatar Stavros Korokithakis

Initial commit, gets notified and publishes

parents
# Created by https://www.gitignore.io/api/rust
### Rust ###
# Generated by Cargo
# will have compiled files and executables
/target/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
# End of https://www.gitignore.io/api/rust
ipfs
[package]
name = "hearth"
version = "0.1.0"
authors = ["Stavros Korokithakis <hi@stavros.io>"]
[dependencies]
notify = "4.0.0"
xdg = "^2.1"
lazy_static = "0.2"
This diff is collapsed.
#[macro_use]
extern crate lazy_static;
extern crate notify;
extern crate xdg;
use notify::{Watcher, RecursiveMode, watcher};
use std::time::Duration;
use std::sync::mpsc::channel;
use std::thread;
use std::process::Command;
static HEARTH_DIR: &'static str = "/home/stavros/Hearth/";
lazy_static! {
static ref IPFS_PATH: std::path::PathBuf = {
let xdg_dirs = xdg::BaseDirectories::with_prefix("hearth")
.expect("Could not retrieve config directory path.");
xdg_dirs
.create_config_directory("ipfs")
.expect("Could not create IPFS config directory.")
};
}
fn add_files_to_ipfs() -> String {
let output = Command::new("./ipfs")
.env("IPFS_PATH", &*IPFS_PATH)
.args(&["add", "-r", "-Q", &HEARTH_DIR])
.output()
.expect("Could not add directory.");
String::from_utf8(output.stdout).unwrap()
}
fn notify_ipfs(event: notify::DebouncedEvent) {
println!("{:?}", event);
println!("{}", add_files_to_ipfs());
}
fn spawn_ipfs_thread() {
thread::spawn(move || {
println!("Starting IPFS daemon...");
Command::new("./ipfs")
.env("IPFS_PATH", &*IPFS_PATH)
.args(&["init"])
.output()
.expect("Could not initialize IPFS.");
Command::new("./ipfs")
.env("IPFS_PATH", &*IPFS_PATH)
.args(&["daemon", "--writable", "--enable-gc"])
.output()
.expect("Could not launch IPFS daemon.");
});
}
fn main() {
println!("Starting hearth...");
spawn_ipfs_thread();
// Create a channel to receive the events.
let (tx, rx) = channel();
// Create a watcher object, delivering debounced events.
// The notification back-end is selected based on the platform.
let mut watcher = watcher(tx, Duration::from_secs(5)).unwrap();
// Add a path to be watched. All files and directories at that path and
// below will be monitored for changes.
watcher
.watch("/home/stavros/Hearth/", RecursiveMode::Recursive)
.unwrap();
loop {
match rx.recv() {
Ok(event) => notify_ipfs(event),
Err(e) => println!("watch error: {:?}", e),
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment