Created
March 22, 2018 20:18
-
-
Save dbluhm/eecb9e9f247f186cfc73cc2cefa77faf to your computer and use it in GitHub Desktop.
Ruby vs. Rust - roughly equivalent linking programs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
has_toml = `gem list -i "^toml$"` | |
if has_toml.strip != 'true' then | |
puts "You must first install the toml gem" | |
puts "Run: gem install toml" | |
exit 1 | |
end | |
require 'toml' | |
require 'fileutils' | |
include FileUtils | |
links = TOML.load_file("links.toml") | |
source_dir = `printf #{links["main"]["source_dir"]}` | |
for link in links["links"] | |
source = source_dir + "/" + link["source"] | |
dest = `printf #{link["dest"]}` | |
printf("%-30s -> %s\n", link["source"], dest) | |
if not Dir.exists?(File.dirname(dest)) then | |
mkdir_p(File.dirname(dest)) | |
end | |
if File.file?(dest) or File.symlink?(dest) then | |
rm(dest) | |
end | |
ln_s(source, dest) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate failure; | |
extern crate shellexpand; | |
extern crate toml; | |
use std::path::Path; | |
use failure::Error; | |
#[macro_use] | |
extern crate failure_derive; | |
#[derive(Debug, Fail)] | |
enum LinkError { | |
#[fail(display = "invalid structure in toml")] | |
InvalidStructure, | |
#[fail(display = "invalid value in toml")] | |
InvalidValue, | |
} | |
fn main() { | |
use std::io::Write; | |
::std::process::exit(match make_links("links.toml") { | |
Ok(_) => 0, | |
Err(err) => { | |
writeln!(std::io::stderr(), "Could not perform linking: {}", err).unwrap(); | |
1 | |
} | |
}); | |
} | |
fn make_links<P: AsRef<Path>>(path: P) -> Result<(), Error> { | |
use std::fs::File; | |
use std::io::Read; | |
use toml::Value; | |
let mut string = String::new(); | |
File::open(path)?.read_to_string(&mut string)?; | |
let toml = string.parse::<Value>()?; | |
let source_dir = shellexpand::env(toml.get("main") | |
.ok_or(LinkError::InvalidStructure)? | |
.get("source_dir") | |
.ok_or(LinkError::InvalidStructure)? | |
.as_str() | |
.ok_or(LinkError::InvalidValue)?)?; | |
for table in toml.get("links") | |
.ok_or(LinkError::InvalidStructure)? | |
.as_array() | |
.ok_or(LinkError::InvalidStructure)? | |
.iter() | |
{ | |
let name = table | |
.get("name") | |
.ok_or(LinkError::InvalidStructure)? | |
.as_str() | |
.ok_or(LinkError::InvalidValue)?; | |
let dest = shellexpand::env(table | |
.get("dest") | |
.ok_or(LinkError::InvalidStructure)? | |
.as_str() | |
.ok_or(LinkError::InvalidValue)?)? | |
.into_owned(); | |
let source = format!( | |
"{}/{}", | |
source_dir, | |
table | |
.get("source") | |
.ok_or(LinkError::InvalidStructure)? | |
.as_str() | |
.ok_or(LinkError::InvalidValue)? | |
); | |
if !parent_exists(&dest) { | |
create_parent(&dest)?; | |
} | |
if let Ok(sym_meta) = std::fs::symlink_metadata(&dest) { | |
if sym_meta.file_type().is_file() || sym_meta.file_type().is_symlink() { | |
remove_old_dest(&dest)?; | |
} | |
}; | |
symlink(&source, &dest)?; | |
println!("{source: <30} -> {dest: >40}", source = name, dest = &dest); | |
} | |
Ok(()) | |
} | |
fn parent_exists<P: AsRef<Path>>(path: P) -> bool { | |
path.as_ref() | |
.parent() | |
.map_or(false, |parent| parent.exists()) | |
} | |
fn create_parent<P: AsRef<Path>>(path: P) -> Result<(), Error> { | |
std::fs::create_dir_all(path.as_ref().parent().ok_or(LinkError::InvalidValue)?) | |
.map_err(|err| Error::from(err)) | |
} | |
fn remove_old_dest<P: AsRef<Path>>(path: P) -> Result<(), Error> { | |
std::fs::remove_file(path).map_err(|err| Error::from(err)) | |
} | |
fn symlink<P: AsRef<Path>, T: AsRef<Path>>(source: P, dest: T) -> Result<(), Error> { | |
std::os::unix::fs::symlink(source, dest).map_err(|err| Error::from(err)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment