aboutsummaryrefslogtreecommitdiff
path: root/dotup_cli/src/commands/link.rs
blob: fd99253cfaab902e671875b2c41e9df45cfd2006 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use clap::Clap;
use std::{
    fs::{DirEntry, Metadata},
    path::{Path, PathBuf},
};

use super::prelude::*;

#[derive(Clap)]
pub struct Opts {
    #[clap(long)]
    directory: bool,

    paths: Vec<PathBuf>,
}

/*
   config/
       nvim/
           init.vim
           lua/
               setup.lua
       bash/
           .bashrc

   link nvim .config/nvim
       nvim/init.vim -> .config/nvim/init.vim
       nvim/lua/setup.lua -> config/nvim/lua/setup.lua

   link bash .
       bash/.bashrc -> ./.bashrc

    link --directory scripts .scripts
        scripts/ -> ./.scripts
*/

pub fn main(config: Config, opts: Opts) -> anyhow::Result<()> {
    let mut depot = utils::read_depot(&config.archive_path)?;

    let (origins, destination) = match opts.paths.as_slice() {
        p @ [] | p @ [_] => (p, None),
        [o @ .., dest] => (o, Some(dest)),
        _ => unreachable!(),
    };

    if let Some(destination) = destination {
        for path in collect_file_type(origins, FileType::File)? {
            let link_desc = LinkDesc {
                origin: path,
                destination: destination.clone(),
            };
            log::info!("Creating link : {}", link_desc);
            depot.create_link(link_desc)?;
        }
    } else {
        let base_path = match origins {
            [] => std::env::current_dir()?,
            [path] => path.clone(),
            _ => unreachable!(),
        };

        for link in utils::collect_links_by_base_paths(&depot, std::iter::once(base_path)) {
            log::info!("{}", link);
        }
    }

    //if let Some(destination) = destination {
    //    for origin in origins {
    //        let origin_canonical = origin.canonicalize()?;
    //        let base = if origin_canonical.is_file() {
    //            origin_canonical.parent().unwrap().to_path_buf()
    //        } else {
    //            origin_canonical.to_path_buf()
    //        };

    //        link(&mut depot, origin.as_path(), destination.as_path(), &base)?;
    //    }
    //} else {
    //    log::warn!("Missing destination");
    //}

    utils::write_depot(&depot)?;

    Ok(())
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum FileType {
    File,
    Directory,
}

/// Collects canonical files of the given type starting from, and including, entry_paths
fn collect_file_type(
    entry_paths: impl IntoIterator<Item = impl AsRef<Path>>,
    collect_type: FileType,
) -> anyhow::Result<Vec<PathBuf>> {
    let entry_paths: Vec<PathBuf> = entry_paths
        .into_iter()
        .map(|p| p.as_ref().to_path_buf())
        .collect();
    let mut collected = Vec::new();
    let mut pending: Vec<_> = entry_paths.iter().cloned().filter(|p| p.is_dir()).collect();

    for path in entry_paths {
        let path = path.canonicalize()?;
        if (path.is_file() && collect_type == FileType::File)
            || (path.is_dir() && collect_type == FileType::Directory)
        {
            collected.push(path);
        }
    }

    while let Some(dir_path) = pending.pop() {
        for entry in dir_path.read_dir()? {
            let entry = entry?;
            let filetype = entry.file_type()?;

            if filetype.is_file() && collect_type == FileType::File {
                collected.push(entry.path());
            } else if filetype.is_dir() {
                if collect_type == FileType::Directory {
                    collected.push(entry.path());
                }
                pending.push(entry.path());
            }
        }
    }

    Ok(collected)
}

fn link(depot: &mut Depot, origin: &Path, destination: &Path, base: &Path) -> anyhow::Result<()> {
    let metadata = std::fs::metadata(origin)?;
    if metadata.is_file() {
        link_file(depot, origin, destination, base)?;
    } else if metadata.is_dir() {
        link_directory_recursive(depot, origin, destination, base)?;
    } else {
        unimplemented!()
    }
    Ok(())
}

fn link_file(
    depot: &mut Depot,
    origin: &Path,
    destination: &Path,
    base: &Path,
) -> anyhow::Result<()> {
    let origin_canonical = origin
        .canonicalize()
        .expect("Failed to canonicalize origin path");
    let partial = origin_canonical
        .strip_prefix(base)
        .expect("Failed to remove prefix from origin path");
    let destination = destination.join(partial);

    let link_desc = LinkDesc {
        origin: origin_canonical,
        destination,
    };

    log::debug!("Linking file {:#?}", link_desc);
    depot.create_link(link_desc)?;

    Ok(())
}

fn link_directory_recursive(
    depot: &mut Depot,
    dir_path: &Path,
    destination: &Path,
    base: &Path,
) -> anyhow::Result<()> {
    for origin in dir_path.read_dir()? {
        let origin = origin?.path();
        link(depot, &origin, destination, base)?;
    }
    Ok(())
}