use clap::Args;
use std::fs;
use std::io::{self, BufRead};
use std::os::unix;
use std::path::PathBuf;
#[derive(Args, Debug)]
pub struct Symlinks {}
impl Symlinks {
pub fn run(&self) -> Result<i32, Box<dyn std::error::Error>> {
for line in io::stdin().lock().lines() {
let line = line?;
let v: Vec<&str> = line.split(" -> ").collect();
if v.len() != 2 {
continue;
}
let link = PathBuf::from(v[0].trim());
let original = PathBuf::from(v[1].trim());
if let Some(dir) = link.parent() {
if dir.as_os_str() != "" {
fs::create_dir_all(dir)?;
}
}
let _ = fs::remove_file(&link);
unix::fs::symlink(original, link)?;
}
Ok(0)
}
}