From ab693b8fd91c93dfd7ee7b82cbae75b2a6043b0f Mon Sep 17 00:00:00 2001 From: Jan Allersma Date: Dec 29 2018 15:15:27 +0000 Subject: Implement `hide` command. Make errors from `backend::config.rs` functions clearer. --- diff --git a/Cargo.lock b/Cargo.lock index 0766c6a..0aa7ce0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -46,6 +46,7 @@ dependencies = [ name = "beheer" version = "0.1.0" dependencies = [ + "git2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "rustyline 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.33 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -80,6 +81,20 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] +name = "curl-sys" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cc 1.0.28 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.45 (registry+https://github.com/rust-lang/crates.io-index)", + "libz-sys 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl-sys 0.9.40 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", + "vcpkg 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "dirs" version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -124,6 +139,30 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] +name = "git2" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.45 (registry+https://github.com/rust-lang/crates.io-index)", + "libgit2-sys 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl-probe 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl-sys 0.9.40 (registry+https://github.com/rust-lang/crates.io-index)", + "url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "idna" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-normalization 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "itoa" version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -134,6 +173,44 @@ version = "0.2.45" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] +name = "libgit2-sys" +version = "0.7.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cc 1.0.28 (registry+https://github.com/rust-lang/crates.io-index)", + "curl-sys 0.4.15 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.45 (registry+https://github.com/rust-lang/crates.io-index)", + "libssh2-sys 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "libz-sys 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl-sys 0.9.40 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "libssh2-sys" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cc 1.0.28 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.45 (registry+https://github.com/rust-lang/crates.io-index)", + "libz-sys 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl-sys 0.9.40 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", + "vcpkg 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "libz-sys" +version = "1.0.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cc 1.0.28 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.45 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", + "vcpkg 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "log" version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -142,6 +219,11 @@ dependencies = [ ] [[package]] +name = "matches" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] name = "memchr" version = "2.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -169,6 +251,32 @@ version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] +name = "openssl-probe" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "openssl-sys" +version = "0.9.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cc 1.0.28 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.45 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", + "vcpkg 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "percent-encoding" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "pkg-config" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] name = "proc-macro2" version = "0.4.24" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -278,6 +386,19 @@ dependencies = [ ] [[package]] +name = "unicode-bidi" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "unicode-normalization" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] name = "unicode-segmentation" version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -293,11 +414,26 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] +name = "url" +version = "1.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "idna 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", + "percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "utf8parse" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] +name = "vcpkg" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] name = "version_check" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -337,17 +473,28 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum cc 1.0.28 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4a8b715cb4597106ea87c7c84b2f1d452c7492033765df7f32651e66fcf749" "checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4" "checksum constant_time_eq 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8ff012e225ce166d4422e0e78419d901719760f62ae2b7969ca6b564d1b54a9e" +"checksum curl-sys 0.4.15 (registry+https://github.com/rust-lang/crates.io-index)" = "721c204978be2143fab0a84b708c49d79d1f6100b8785610f456043a90708870" "checksum dirs 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "88972de891f6118092b643d85a0b28e0678e0f948d7f879aa32f2d5aafe97d2a" "checksum failure 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6dd377bcc1b1b7ce911967e3ec24fa19c3224394ec05b54aa7b083d498341ac7" "checksum failure_derive 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "64c2d913fe8ed3b6c6518eedf4538255b989945c14c2a7d5cbff62a5e2120596" "checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" +"checksum git2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c7339329bfa14a00223244311560d11f8f489b453fb90092af97f267a6090ab0" +"checksum idna 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "38f09e0f0b1fb55fdee1f17470ad800da77af5186a1a76c026b679358b7e844e" "checksum itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1306f3464951f30e30d12373d31c79fbd52d236e5e896fd92f96ec7babbbe60b" "checksum libc 0.2.45 (registry+https://github.com/rust-lang/crates.io-index)" = "2d2857ec59fadc0773853c664d2d18e7198e83883e7060b63c924cb077bd5c74" +"checksum libgit2-sys 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)" = "48441cb35dc255da8ae72825689a95368bf510659ae1ad55dc4aa88cb1789bf1" +"checksum libssh2-sys 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "126a1f4078368b163bfdee65fbab072af08a1b374a5551b21e87ade27b1fbf9d" +"checksum libz-sys 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)" = "2eb5e43362e38e2bca2fd5f5134c4d4564a23a5c28e9b95411652021a8675ebe" "checksum log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c84ec4b527950aa83a329754b01dbe3f58361d1c5efacd1f6d68c494d08a17c6" +"checksum matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" "checksum memchr 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "db4c41318937f6e76648f42826b1d9ade5c09cafb5aef7e351240a70f39206e9" "checksum nix 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d37e713a259ff641624b6cb20e3b12b2952313ba36b6823c0f16e6cfd9e5de17" "checksum nodrop 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "2f9667ddcc6cc8a43afc9b7917599d7216aa09c463919ea32c59ed6cac8bc945" +"checksum openssl-probe 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "77af24da69f9d9341038eba93a073b1fdaaa1b788221b00a69bce9e762cb32de" +"checksum openssl-sys 0.9.40 (registry+https://github.com/rust-lang/crates.io-index)" = "1bb974e77de925ef426b6bc82fce15fd45bdcbeb5728bffcfc7cdeeb7ce1c2d6" +"checksum percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "31010dd2e1ac33d5b46a5b413495239882813e0369f8ed8a5e266f173602f831" +"checksum pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "676e8eb2b1b4c9043511a9b7bea0915320d7e502b0a079fb03f9635a5252b18c" "checksum proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)" = "77619697826f31a02ae974457af0b29b723e5619e113e9397b8b82c6bd253f09" "checksum quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "53fa22a1994bd0f9372d7a816207d8a2677ad0325b073f5c5332760f0fb62b5c" "checksum rand 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8356f47b32624fef5b3301c1be97e5944ecdd595409cc5da11d05f211db6cfbd" @@ -361,10 +508,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum serde_json 1.0.33 (registry+https://github.com/rust-lang/crates.io-index)" = "c37ccd6be3ed1fdf419ee848f7c758eb31b054d7cd3ae3600e3bae0adf569811" "checksum syn 0.15.23 (registry+https://github.com/rust-lang/crates.io-index)" = "9545a6a093a3f0bd59adb472700acc08cad3776f860f16a897dfce8c88721cbc" "checksum synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "73687139bf99285483c96ac0add482c3776528beac1d97d444f6e91f203a2015" +"checksum unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5" +"checksum unicode-normalization 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "6a0180bc61fc5a987082bfa111f4cc95c4caff7f9799f3e46df09163a937aa25" "checksum unicode-segmentation 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "aa6024fc12ddfd1c6dbc14a80fa2324d4568849869b779f6bd37e5e4c03344d1" "checksum unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526" "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" +"checksum url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dd4e7c0d531266369519a4aa4f399d748bd37043b00bde1e4ff1f60a120b355a" "checksum utf8parse 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8772a4ccbb4e89959023bc5b7cb8623a795caa7092d99f3aa9501b9484d4557d" +"checksum vcpkg 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "def296d3eb3b12371b2c7d0e83bfe1403e4db2d7a0bba324a12b21c4ee13143d" "checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd" "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" "checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0" diff --git a/Cargo.toml b/Cargo.toml index e696031..47d2e5c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,4 @@ authors = ["Jan Allersma "] [dependencies] serde_json = "1.0" rustyline = "2.1.0" +git2 = "0.8" diff --git a/src/backend/config.rs b/src/backend/config.rs index 495c65e..4ece9c9 100644 --- a/src/backend/config.rs +++ b/src/backend/config.rs @@ -1,11 +1,17 @@ extern crate serde_json; +use std::io; use std::io::{Error, ErrorKind, Write, Read}; use std::fs::File; use std::path::PathBuf; use std::result::Result; pub fn create(mut path: PathBuf) -> Result<(), Error> { + path.push("beheer.json"); + init(&path) +} + +fn init(path: &PathBuf) -> Result<(), Error> { let content = json!({ "build": { "windows": "", @@ -19,8 +25,6 @@ pub fn create(mut path: PathBuf) -> Result<(), Error> { } }); - path.push("beheer.json"); - match File::open(path.to_str().unwrap()) { Ok(_) => return Err(Error::new(ErrorKind::AlreadyExists, "Already found a 'beheer.json' file.")), Err(_) => { @@ -50,24 +54,49 @@ pub fn update(mut path: PathBuf, value: serde_json::Value) -> Result<(), String> } } -fn read(mut path: PathBuf) -> Result { +fn read(path: &mut PathBuf) -> Result { let mut config = String::new(); path.push("beheer.json"); + check(&path); match File::open(path.to_str().unwrap()) { Ok(mut file) => { file.read_to_string(&mut config).unwrap(); }, - Err(e) => return Err(e.to_string()) + Err(e) => return Err(e) } Ok(config) } -pub fn get_json(path: PathBuf) -> Result { - match read(path) { +pub fn get_json(mut path: PathBuf) -> Result { + match read(&mut path) { Ok(config) => super::dep::json(config), - Err(e) => Err(e.to_string()) + Err(e) => { + println!("Error on config file: '{}'.", path.to_str().unwrap()); + match e.kind() { + ErrorKind::NotFound => Err(String::from("No config file found.")), + ErrorKind::PermissionDenied => Err(String::from("Config file unreadable: permission denied.")), + _ => Err(e.to_string()) + } + } + } +} + +fn check(config: &PathBuf) { + if !config.is_file() { + let mut input = String::new(); + + println!("'{}' not found. ", config.to_str().unwrap()); + println!("Initialize module with new config file [y/N]?"); + + match io::stdin().read_line(&mut input) { + Ok(_) if input.as_str() == "y\n" => match init(config) { + Ok(_) => {}, + Err(e) => println!("Module initialization failed. Details: {}", e) + }, + Ok(_) | Err(_) => {} + } } } diff --git a/src/backend/delete.rs b/src/backend/delete.rs index ca3192b..87818db 100644 --- a/src/backend/delete.rs +++ b/src/backend/delete.rs @@ -56,8 +56,7 @@ fn dep_check(node: &Node) -> Result<(), String> { println!("Continue [y/N]?"); match io::stdin().read_line(&mut input) { - Ok(_) if input.as_str() == "y\n" => { - }, + Ok(_) if input.as_str() == "y\n" => {}, Ok(_) => return Err(String::from("Aborted.")), Err(e) => return Err(e.to_string()) } diff --git a/src/backend/git/ignore.rs b/src/backend/git/ignore.rs new file mode 100644 index 0000000..2b17844 --- /dev/null +++ b/src/backend/git/ignore.rs @@ -0,0 +1,22 @@ +use std::io::Write; +use std::path::PathBuf; +use std::fs::OpenOptions; + +pub fn add(path: &mut PathBuf, item: &mut String) -> Result<(), String> { + path.push(".gitignore"); + item.push('\n'); + + let file_options = OpenOptions::new() + .append(true) + .open(path); + + match file_options { + Ok(mut file) => { + match file.write(item.as_bytes()) { + Ok(_) => Ok(()), + Err(e) => Err(e.to_string()) + } + }, + Err(e) => Err(e.to_string()) + } +} diff --git a/src/backend/git/mod.rs b/src/backend/git/mod.rs new file mode 100644 index 0000000..bf699b5 --- /dev/null +++ b/src/backend/git/mod.rs @@ -0,0 +1,3 @@ +mod rec; +pub mod status; +pub mod ignore; diff --git a/src/backend/git/rec.rs b/src/backend/git/rec.rs new file mode 100644 index 0000000..27ff93a --- /dev/null +++ b/src/backend/git/rec.rs @@ -0,0 +1 @@ +extern crate git2; diff --git a/src/backend/git/status.rs b/src/backend/git/status.rs new file mode 100644 index 0000000..27ff93a --- /dev/null +++ b/src/backend/git/status.rs @@ -0,0 +1 @@ +extern crate git2; diff --git a/src/backend/mod.rs b/src/backend/mod.rs index c6fa915..7bdad7e 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -8,3 +8,4 @@ pub mod build; pub mod dep; pub mod fetch; pub mod system; +pub mod git; diff --git a/src/backend/project.rs b/src/backend/project.rs index be666ca..83054f2 100644 --- a/src/backend/project.rs +++ b/src/backend/project.rs @@ -1,6 +1,5 @@ use std::env; use std::result::Result; -use std::process::{Command, Stdio}; use super::deptree; pub fn init(args: &mut I) where I: Iterator { @@ -96,6 +95,18 @@ pub fn delete(path: &mut I) -> Result where I: Iterator(args: &mut I) -> Result<(), String> where I: Iterator { + let mut path = match args.next() { + Some(arg) => arg, + None => return Err(String::from("Missing path as argument.")) + }; + + match env::current_dir() { + Ok(mut dir) => super::git::ignore::add(&mut dir, &mut path), + Err(e) => Err(e.to_string()) + } +} + pub fn dep_tree(args: &mut I) -> Result where I: Iterator { let path = match super::filesystem::get_current_module_root() { Some(p) => p, @@ -125,6 +136,7 @@ pub fn help() { println!("run [ARGUMENTS]\t\t\t Build and run current project with ARGUMENTS to run project with."); println!("exe [ARGUMENTS]\t\t\t Run current project with ARGUMENTS. The project won't be built."); println!("add NAME COMMAND [ARGUMENTS]\t Add dependency with NAME to module and is built through COMMAND with ARGUMENTS."); - println!("delete PATH\t\t\t Delete a dependency in PATH."); + println!("hide NAME COMMAND [ARGUMENTS]\t Add dependency with NAME to module and is built through COMMAND with ARGUMENTS. Add configfile to '.gitignore'."); + println!("delete PATH\t\t\t Delete a dependency in PATH."); println!("dep-tree [all|linux|os-x|windows] Print a tree of all dependencies used (indirectly) by a project for specified OS. Defaults to 'all'."); } diff --git a/src/parser.rs b/src/parser.rs index 1944511..cca6685 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -48,6 +48,17 @@ fn parse(args: &mut I, open_shell: bool) -> bool where I: Iterator println!("Could not add dependency: {}", e) } } + else if argument == "hide" { + let result = match backend::project::ignore(args) { + Ok(_) => backend::add::add(args), + Err(e) => Err(e) + }; + + match result { + Ok(msg) => println!("{}", msg), + Err(e) => println!("Could not add dependency: {}", e) + } + } else if argument == "delete" { match backend::project::delete(args) { Ok(module) => println!("Module '{}' deleted.", module),