diff options
| author | diogo464 <[email protected]> | 2021-10-30 19:47:49 +0100 |
|---|---|---|
| committer | diogo464 <[email protected]> | 2021-10-30 19:47:49 +0100 |
| commit | d027ec1c61b28c1ab914d61dd3dbce15af5a820e (patch) | |
| tree | 2134f752ee1945785e480a0ba75520a70cff30a5 | |
| parent | 1c0dee8e60d9c1c989a3712e7d1e19debe981e8a (diff) | |
Small changes
| -rw-r--r-- | dotup/src/depot.rs | 6 | ||||
| -rw-r--r-- | dotup_cli/src/commands/link.rs | 16 | ||||
| -rw-r--r-- | dotup_cli/src/utils.rs | 2 |
3 files changed, 15 insertions, 9 deletions
diff --git a/dotup/src/depot.rs b/dotup/src/depot.rs index 4997996..5ee8f1a 100644 --- a/dotup/src/depot.rs +++ b/dotup/src/depot.rs | |||
| @@ -159,6 +159,7 @@ struct DepotShared { | |||
| 159 | 159 | ||
| 160 | #[derive(Debug)] | 160 | #[derive(Debug)] |
| 161 | pub struct Depot { | 161 | pub struct Depot { |
| 162 | // TODO: remove shared | ||
| 162 | shared: Arc<DepotShared>, | 163 | shared: Arc<DepotShared>, |
| 163 | // Maps the origin to the link | 164 | // Maps the origin to the link |
| 164 | links: SlotMap<LinkID, Link>, | 165 | links: SlotMap<LinkID, Link>, |
| @@ -166,12 +167,15 @@ pub struct Depot { | |||
| 166 | } | 167 | } |
| 167 | 168 | ||
| 168 | impl Depot { | 169 | impl Depot { |
| 170 | /// Creates a new [`Depot`] using the config. | ||
| 171 | /// Fails if any of the links in the provided archive fail to be created. | ||
| 169 | pub fn new(config: DepotConfig) -> Result<Self> { | 172 | pub fn new(config: DepotConfig) -> Result<Self> { |
| 170 | depot_create(config) | 173 | depot_create(config) |
| 171 | } | 174 | } |
| 172 | 175 | ||
| 173 | /// Creates a new link from the description. | 176 | /// Creates a new link from the description. |
| 174 | /// The origin path must exist. | 177 | /// The origin path must exist. |
| 178 | /// If a link with the same origin already existed then it is replaced. | ||
| 175 | pub fn create_link(&mut self, link_desc: LinkCreateParams) -> Result<LinkID, LinkCreateError> { | 179 | pub fn create_link(&mut self, link_desc: LinkCreateParams) -> Result<LinkID, LinkCreateError> { |
| 176 | let link = depot_create_link(self, link_desc)?; | 180 | let link = depot_create_link(self, link_desc)?; |
| 177 | let link_id = depot_insert_link(self, link); | 181 | let link_id = depot_insert_link(self, link); |
| @@ -267,6 +271,7 @@ fn depot_create_link(depot: &Depot, link_desc: LinkCreateParams) -> Result<Link, | |||
| 267 | link_ensure_relative_path(&link_desc.destination)?; | 271 | link_ensure_relative_path(&link_desc.destination)?; |
| 268 | debug_assert!(utils::is_canonical(depot.base_path())?); | 272 | debug_assert!(utils::is_canonical(depot.base_path())?); |
| 269 | 273 | ||
| 274 | // Check if the file/directory at origin actually exists | ||
| 270 | let origin_joined = depot.base_path().join(&link_desc.origin); | 275 | let origin_joined = depot.base_path().join(&link_desc.origin); |
| 271 | let origin_result = origin_joined.canonicalize(); | 276 | let origin_result = origin_joined.canonicalize(); |
| 272 | let origin_canonical = match origin_result { | 277 | let origin_canonical = match origin_result { |
| @@ -291,7 +296,6 @@ fn depot_create_link(depot: &Depot, link_desc: LinkCreateParams) -> Result<Link, | |||
| 291 | .strip_prefix(depot.base_path()) | 296 | .strip_prefix(depot.base_path()) |
| 292 | .unwrap() | 297 | .unwrap() |
| 293 | .to_path_buf(); | 298 | .to_path_buf(); |
| 294 | // let origin = origin_canonical; | ||
| 295 | let destination = link_desc.destination; | 299 | let destination = link_desc.destination; |
| 296 | 300 | ||
| 297 | let ty = if origin.is_dir() { | 301 | let ty = if origin.is_dir() { |
diff --git a/dotup_cli/src/commands/link.rs b/dotup_cli/src/commands/link.rs index 4c1ae06..66194e1 100644 --- a/dotup_cli/src/commands/link.rs +++ b/dotup_cli/src/commands/link.rs | |||
| @@ -22,6 +22,8 @@ pub struct Opts { | |||
| 22 | paths: Vec<PathBuf>, | 22 | paths: Vec<PathBuf>, |
| 23 | } | 23 | } |
| 24 | 24 | ||
| 25 | // TODO: require destination | ||
| 26 | // remove else branch | ||
| 25 | pub fn main(config: Config, opts: Opts) -> anyhow::Result<()> { | 27 | pub fn main(config: Config, opts: Opts) -> anyhow::Result<()> { |
| 26 | let mut depot = utils::read_depot(&config.archive_path)?; | 28 | let mut depot = utils::read_depot(&config.archive_path)?; |
| 27 | 29 | ||
| @@ -43,7 +45,7 @@ pub fn main(config: Config, opts: Opts) -> anyhow::Result<()> { | |||
| 43 | } else { | 45 | } else { |
| 44 | let mut params = Vec::new(); | 46 | let mut params = Vec::new(); |
| 45 | for origin in origins { | 47 | for origin in origins { |
| 46 | link(&depot, origin, destination, origin, &mut params)?; | 48 | generate_link_params(&depot, origin, destination, origin, &mut params)?; |
| 47 | } | 49 | } |
| 48 | params | 50 | params |
| 49 | }; | 51 | }; |
| @@ -69,7 +71,7 @@ pub fn main(config: Config, opts: Opts) -> anyhow::Result<()> { | |||
| 69 | Ok(()) | 71 | Ok(()) |
| 70 | } | 72 | } |
| 71 | 73 | ||
| 72 | fn link( | 74 | fn generate_link_params( |
| 73 | depot: &Depot, | 75 | depot: &Depot, |
| 74 | origin: &Path, | 76 | origin: &Path, |
| 75 | destination: &Path, | 77 | destination: &Path, |
| @@ -78,14 +80,14 @@ fn link( | |||
| 78 | ) -> anyhow::Result<()> { | 80 | ) -> anyhow::Result<()> { |
| 79 | let metadata = std::fs::metadata(origin)?; | 81 | let metadata = std::fs::metadata(origin)?; |
| 80 | if metadata.is_file() { | 82 | if metadata.is_file() { |
| 81 | link_file(depot, origin, destination, base, params)?; | 83 | generate_file_link_params(depot, origin, destination, base, params)?; |
| 82 | } else if metadata.is_dir() { | 84 | } else if metadata.is_dir() { |
| 83 | link_directory_recursive(depot, origin, destination, base, params)?; | 85 | generate_directory_link_params_recursive(depot, origin, destination, base, params)?; |
| 84 | } | 86 | } |
| 85 | Ok(()) | 87 | Ok(()) |
| 86 | } | 88 | } |
| 87 | 89 | ||
| 88 | fn link_file( | 90 | fn generate_file_link_params( |
| 89 | depot: &Depot, | 91 | depot: &Depot, |
| 90 | origin: &Path, | 92 | origin: &Path, |
| 91 | destination: &Path, | 93 | destination: &Path, |
| @@ -118,7 +120,7 @@ fn link_file( | |||
| 118 | Ok(()) | 120 | Ok(()) |
| 119 | } | 121 | } |
| 120 | 122 | ||
| 121 | fn link_directory_recursive( | 123 | fn generate_directory_link_params_recursive( |
| 122 | depot: &Depot, | 124 | depot: &Depot, |
| 123 | dir_path: &Path, | 125 | dir_path: &Path, |
| 124 | destination: &Path, | 126 | destination: &Path, |
| @@ -127,7 +129,7 @@ fn link_directory_recursive( | |||
| 127 | ) -> anyhow::Result<()> { | 129 | ) -> anyhow::Result<()> { |
| 128 | for origin in dir_path.read_dir()? { | 130 | for origin in dir_path.read_dir()? { |
| 129 | let origin = origin?.path(); | 131 | let origin = origin?.path(); |
| 130 | link(depot, &origin, destination, base, params)?; | 132 | generate_link_params(depot, &origin, destination, base, params)?; |
| 131 | } | 133 | } |
| 132 | Ok(()) | 134 | Ok(()) |
| 133 | } | 135 | } |
diff --git a/dotup_cli/src/utils.rs b/dotup_cli/src/utils.rs index d284a22..a5c7647 100644 --- a/dotup_cli/src/utils.rs +++ b/dotup_cli/src/utils.rs | |||
| @@ -83,8 +83,8 @@ pub fn read_depot(archive_path: impl AsRef<Path>) -> anyhow::Result<Depot> { | |||
| 83 | let archive_path = archive_path.as_ref().to_path_buf(); | 83 | let archive_path = archive_path.as_ref().to_path_buf(); |
| 84 | let archive = read_archive(&archive_path)?; | 84 | let archive = read_archive(&archive_path)?; |
| 85 | let depot_config = DepotConfig { | 85 | let depot_config = DepotConfig { |
| 86 | archive_path, | ||
| 87 | archive: Default::default(), | 86 | archive: Default::default(), |
| 87 | archive_path, | ||
| 88 | }; | 88 | }; |
| 89 | let mut depot = Depot::new(depot_config)?; | 89 | let mut depot = Depot::new(depot_config)?; |
| 90 | 90 | ||
