Commit ea293044 authored by Stavros Korokithakis's avatar Stavros Korokithakis

Improve directory paths

parent 0c7b8f8d
Hearth
======
To run Hearth, copy the IPFS executable in the directory where `hearth` resides, and run the latter.
......@@ -3,13 +3,23 @@ extern crate lazy_static;
extern crate notify;
extern crate xdg;
use notify::{Watcher, RecursiveMode, watcher};
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
use std::fs;
use std::io;
use std::env;
use std::time::Duration;
use std::sync::mpsc::channel;
use std::thread;
use std::process;
use std::process::Command;
static HEARTH_DIR: &'static str = "/home/stavros/Hearth/";
lazy_static! {
static ref HEARTH_DIR: std::path::PathBuf = {
let mut dir = env::home_dir().expect("Could not get home directory.");
dir.push("Hearth");
dir
};
}
lazy_static! {
static ref IPFS_PATH: std::path::PathBuf = {
......@@ -23,23 +33,31 @@ lazy_static! {
fn add_files_to_ipfs() -> String {
println!("Adding files to IPFS...");
let output = Command::new("./ipfs")
.env("IPFS_PATH", &*IPFS_PATH)
.args(&["add", "-r", "-Q", &HEARTH_DIR])
.args(&["add", "-r", "-Q", HEARTH_DIR.to_str().unwrap()])
.output()
.expect("Could not add directory.");
String::from_utf8(output.stdout).unwrap()
let hash = String::from_utf8(output.stdout).unwrap();
hash
}
fn notify_ipfs(event: notify::DebouncedEvent) {
println!("{:?}", event);
println!("{}", add_files_to_ipfs());
match event {
notify::DebouncedEvent::Create(_) => {}
notify::DebouncedEvent::Remove(_) => {}
notify::DebouncedEvent::Rename(_, _) => {}
notify::DebouncedEvent::Write(_) => {}
_ => return,
}
println!("https://www.eternum.io/ipfs/{}", add_files_to_ipfs());
}
fn spawn_ipfs_thread() {
thread::spawn(move || {
println!("Starting IPFS daemon...");
println!("Initializing IPFS...");
Command::new("./ipfs")
.env("IPFS_PATH", &*IPFS_PATH)
......@@ -47,6 +65,8 @@ fn spawn_ipfs_thread() {
.output()
.expect("Could not initialize IPFS.");
println!("Starting IPFS daemon...");
Command::new("./ipfs")
.env("IPFS_PATH", &*IPFS_PATH)
.args(&["daemon", "--writable", "--enable-gc"])
......@@ -61,22 +81,29 @@ fn main() {
spawn_ipfs_thread();
match fs::create_dir(&*HEARTH_DIR) {
Err(ref e) if e.kind() != io::ErrorKind::AlreadyExists => {
println!("Could not create the Hearth directory, for some reason.");
process::exit(1)
}
_ => {}
}
// 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();
let mut watcher: RecommendedWatcher = Watcher::new(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();
watcher.watch(&*HEARTH_DIR, RecursiveMode::Recursive)
.expect("There was an error when trying to watch the Hearth directory for changes.");
loop {
match rx.recv() {
Ok(event) => notify_ipfs(event),
Ok(e) => notify_ipfs(e),
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