coliru

A minimal, flexible, dotfile installer
git clone https://git.ashermorgan.net/coliru/
Log | Files | Refs | README

commit 27456e1a2ae9cfb55a3e728249c8be45bb3f72e8
parent 63cc39016d5601d608749bf6d999f1f0e22bd5e9
Author: Asher Morgan <59518073+ashermorgan@users.noreply.github.com>
Date:   Thu, 20 Jun 2024 12:26:54 -0700

Implement copy_file function

Diffstat:
MCargo.lock | 180+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
MCargo.toml | 1+
Msrc/core.rs | 6+++++-
Msrc/main.rs | 1+
Msrc/manifest.rs | 18+++++++++---------
Asrc/utils.rs | 18++++++++++++++++++
6 files changed, 214 insertions(+), 10 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock @@ -52,6 +52,41 @@ dependencies = [ ] [[package]] +name = "arrayref" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b4930d2cb77ce62f89ee5d5289b4ac049559b1c45539271f5ed4fdc7db34545" + +[[package]] +name = "arrayvec" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" + +[[package]] +name = "base64" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" + +[[package]] +name = "blake2b_simd" +version = "0.5.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "afa748e348ad3be8263be728124b24a24f268266f6f5d58af9d75f6a40b5c587" +dependencies = [ + "arrayref", + "arrayvec", + "constant_time_eq", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] name = "clap" version = "4.5.7" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -96,6 +131,7 @@ name = "coliru" version = "0.0.0" dependencies = [ "clap", + "expanduser", "serde", "serde_yaml", ] @@ -107,12 +143,57 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" [[package]] +name = "constant_time_eq" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" + +[[package]] +name = "crossbeam-utils" +version = "0.8.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" + +[[package]] +name = "dirs" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fd78930633bd1c6e35c4b42b1df7b0cbc6bc191146e512bb3bedf243fcc3901" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + +[[package]] name = "equivalent" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" [[package]] +name = "expanduser" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14e0b79235da57db6b6c2beed9af6e5de867d63a973ae3e91910ddc33ba40bc0" +dependencies = [ + "dirs", + "lazy_static", + "pwd", +] + +[[package]] +name = "getrandom" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] name = "hashbrown" version = "0.14.5" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -147,6 +228,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" [[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.155" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" + +[[package]] name = "proc-macro2" version = "1.0.85" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -156,6 +249,16 @@ dependencies = [ ] [[package]] +name = "pwd" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72c71c0c79b9701efe4e1e4b563b2016dd4ee789eb99badcb09d61ac4b92e4a2" +dependencies = [ + "libc", + "thiserror", +] + +[[package]] name = "quote" version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -165,6 +268,35 @@ dependencies = [ ] [[package]] +name = "redox_syscall" +version = "0.1.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" + +[[package]] +name = "redox_users" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de0737333e7a9502c789a36d7c7fa6092a49895d4faa31ca5df163857ded2e9d" +dependencies = [ + "getrandom", + "redox_syscall", + "rust-argon2", +] + +[[package]] +name = "rust-argon2" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b18820d944b33caa75a71378964ac46f58517c92b6ae5f762636247c09e78fb" +dependencies = [ + "base64", + "blake2b_simd", + "constant_time_eq", + "crossbeam-utils", +] + +[[package]] name = "ryu" version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -221,6 +353,26 @@ dependencies = [ ] [[package]] +name = "thiserror" +version = "1.0.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c546c80d6be4bc6a00c0f01730c08df82eaa7a7a61f11d656526506112cc1709" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] name = "unicode-ident" version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -239,6 +391,34 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" [[package]] +name = "wasi" +version = "0.9.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] name = "windows-sys" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/Cargo.toml b/Cargo.toml @@ -5,5 +5,6 @@ edition = "2021" [dependencies] clap = { version = "4.5.7", features = ["derive"] } +expanduser = "1.2" serde = { version = "1.0", features = ["derive"] } serde_yaml = "0.9" diff --git a/src/core.rs b/src/core.rs @@ -1,5 +1,6 @@ use super::manifest; use super::tags; +use super::utils::copy_file; /// Execute the steps in a coliru manifest according to a set of tag rules pub fn execute_manifest(manifest: manifest::Manifest, rules: Vec<String>) { @@ -16,6 +17,9 @@ pub fn execute_manifest(manifest: manifest::Manifest, rules: Vec<String>) { /// Execute the copy commands specified in a coliru manifest step fn execute_copies(copies: &[manifest::CopyOptions]) { for copy in copies { - println!(" Copy {} to {}", copy.src.display(), copy.dst.display()); + println!(" Copy {} to {}", copy.src, copy.dst); + if let Err(why) = copy_file(&copy.src, &copy.dst) { + eprintln!(" Error: {}", why); + } } } diff --git a/src/main.rs b/src/main.rs @@ -2,6 +2,7 @@ mod cli; mod core; mod manifest; mod tags; +mod utils; use clap::Parser; diff --git a/src/manifest.rs b/src/manifest.rs @@ -1,12 +1,12 @@ use serde::{Serialize, Deserialize}; use serde_yaml; use std::fs::read_to_string; -use std::path::{Path, PathBuf}; +use std::path::Path; #[derive(Debug, PartialEq, Serialize, Deserialize)] pub struct CopyOptions { - pub src: PathBuf, - pub dst: PathBuf, + pub src: String, + pub dst: String, } #[derive(Debug, PartialEq, Serialize, Deserialize)] @@ -57,12 +57,12 @@ mod tests { Step { copy: vec![ CopyOptions{ - src: PathBuf::from("foo"), - dst: PathBuf::from("~/foo") + src: String::from("foo"), + dst: String::from("~/foo"), }, CopyOptions{ - src: PathBuf::from("bar"), - dst: PathBuf::from("~/test/bar") + src: String::from("bar"), + dst: String::from("~/test/bar"), }, ], tags: vec![String::from("a"), String::from("b")], @@ -70,8 +70,8 @@ mod tests { Step { copy: vec![ CopyOptions{ - src: PathBuf::from("baz"), - dst: PathBuf::from("/baz") + src: String::from("baz"), + dst: String::from("/baz"), }, ], tags: vec![String::from("b"), String::from("c")], diff --git a/src/utils.rs b/src/utils.rs @@ -0,0 +1,18 @@ +extern crate expanduser; + +use expanduser::expanduser; +use std::io::Result; +use std::fs::{copy, create_dir_all}; + +/// Copies the contents of a local file to another local file. +/// +/// Tildes are expanded if present and the destination file is overwritten if +/// necessary. +pub fn copy_file(src: &str, dst: &str) -> Result<()> { + let _dst = expanduser(dst)?; + if let Some(path) = _dst.parent() { + create_dir_all(path)?; + } + copy(src, _dst)?; + Ok(()) +}