From 5df7d66df773d33612d78e1fc7ac70ec80ef8f99 Mon Sep 17 00:00:00 2001 From: Kalle Struik Date: Thu, 13 Oct 2022 17:34:29 +0200 Subject: [PATCH] Initial commit --- .gitignore | 1 + Cargo.lock | 353 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 13 ++ README.md | 2 + src/main.rs | 154 +++++++++++++++++++++++ 5 files changed, 523 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 README.md create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..fb0d7b7 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,353 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "anyhow" +version = "1.0.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98161a4e3e2184da77bb14f02184cdd111e83bbbcc9979dfee3c44b9a85f5602" + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[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.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ea54a38e4bce14ff6931c72e5b3c43da7051df056913d4e7e1fcdb1c03df69d" +dependencies = [ + "atty", + "bitflags", + "clap_derive", + "clap_lex", + "once_cell", + "strsim", + "termcolor", +] + +[[package]] +name = "clap_derive" +version = "4.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c42f169caba89a7d512b5418b09864543eeb4d497416c917d7137863bd2076ad" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d4198f73e42b4936b35b5bb248d81d2b595ecb170da0bac7655c54eedfa8da8" +dependencies = [ + "os_str_bytes", +] + +[[package]] +name = "dirs" +version = "4.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + +[[package]] +name = "getrandom" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "heck" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[package]] +name = "itoa" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4217ad341ebadf8d8e724e264f13e593e0648f5b3e94b3896a5df283be015ecc" + +[[package]] +name = "libc" +version = "0.2.135" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68783febc7782c6c5cb401fbda4de5a9898be1762314da0bb2c10ced61f18b0c" + +[[package]] +name = "once_cell" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e82dad04139b71a90c080c8463fe0dc7902db5192d939bd0950f074d014339e1" + +[[package]] +name = "os_str_bytes" +version = "6.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ff7415e9ae3fff1225851df9e0d9e4e5479f947619774677a63572e55e80eff" + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro2" +version = "1.0.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94e2ef8dbfc347b10c094890f778ee2e36ca9bb4262e86dc99cd217e35f3470b" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "project" +version = "0.1.0" +dependencies = [ + "anyhow", + "clap", + "serde", + "serde_json", + "xdg", +] + +[[package]] +name = "quote" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "redox_syscall" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" +dependencies = [ + "bitflags", +] + +[[package]] +name = "redox_users" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b" +dependencies = [ + "getrandom", + "redox_syscall", + "thiserror", +] + +[[package]] +name = "ryu" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" + +[[package]] +name = "serde" +version = "1.0.145" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "728eb6351430bccb993660dfffc5a72f91ccc1295abaa8ce19b27ebe4f75568b" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.145" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81fa1584d3d1bcacd84c277a0dfe21f5b0f6accf4a23d04d4c6d61f1af522b4c" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41feea4228a6f1cd09ec7a3593a682276702cd67b5273544757dae23c096f074" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + +[[package]] +name = "syn" +version = "1.0.102" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fcd952facd492f9be3ef0d0b7032a6e442ee9b361d4acc2b1d0c4aaa5f613a1" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "termcolor" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "thiserror" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10deb33631e3c9018b9baf9dcbbc4f737320d2b576bac10f6aefa048fa407e3e" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "982d17546b47146b28f7c22e3d08465f6b8903d0ea13c1660d9d84a6e7adcdbb" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "unicode-ident" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[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-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + +[[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 = "xdg" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c4583db5cbd4c4c0303df2d15af80f0539db703fa1c68802d4cbbd2dd0f88f6" +dependencies = [ + "dirs", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..96e223c --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "project" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +anyhow = "1.0.65" +clap = { version = "4.0.14", features = ["derive"] } +serde = { version = "1.0.145", features = ["derive"] } +serde_json = "1.0.86" +xdg = "2.4.1" diff --git a/README.md b/README.md new file mode 100644 index 0000000..8e6ac4c --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# Projects +A tool for managing projects from the commandline. diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..67846c8 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,154 @@ +use anyhow::Result; +use std::{fs::{File, self}, path::PathBuf, io::{BufReader, Write, self}, collections::HashMap}; +use clap::{Parser, Subcommand, Args}; +use xdg; + +#[derive(Debug, Parser)] +struct Cli { + #[command(subcommand)] + action: Action, +} + +#[derive(Debug, Subcommand)] +enum Action { + New(NewProjectArgs), + Delete(ProjectNameArgs), + Path(ProjectNameArgs), + List, +} + +#[derive(Debug, Args)] +struct NewProjectArgs { + project_name: String, + project_path: String, +} + +#[derive(Debug, Args)] +struct ProjectNameArgs { + project_name: String, +} + +type ProjectData = HashMap; + +fn main() { + let cli = Cli::parse(); + + let project_data = match load_or_default_project_data() { + Ok(data) => data, + Err(error) => { + println!("An error occured while reading the project data file. {}", error); + return; + } + }; + + process_command(cli, project_data); +} + +fn load_or_default_project_data() -> Result { + let data_file_path = xdg::BaseDirectories::with_prefix("projects")?.place_data_file("projects.json")?; + create_default_data_file_if_needed(&data_file_path); + let data_file = File::open(data_file_path)?; + let reader = BufReader::new(data_file); + + let project_data: ProjectData = serde_json::from_reader(reader)?; + + return Ok(project_data); +} + +fn save_project_data(project_data: ProjectData) -> Result<()> { + let data_file_path = xdg::BaseDirectories::with_prefix("projects")?.place_data_file("projects.json")?; + let mut data_file = File::create(data_file_path)?; + + let data_string = serde_json::to_string(&project_data)?; + write!(data_file, "{}", data_string).expect("Failed to write new data file!"); + + return Ok(()); +} + +fn create_default_data_file_if_needed(data_file_path: &PathBuf) { + if data_file_path.exists() { return; } + + fs::create_dir_all(data_file_path.parent().unwrap()).ok(); + + let mut data_file = File::create(data_file_path).expect("Error creating data file!"); + write!(data_file, "{{}}").expect("Error writing default content to data file!"); +} + +fn process_command(cli: Cli, project_data: ProjectData) -> Option<()> { + match cli.action { + Action::New(NewProjectArgs { project_name, project_path }) => new_project(project_name, project_path, project_data), + Action::Delete(ProjectNameArgs {project_name}) => delete_project(project_name, project_data), + Action::Path(ProjectNameArgs {project_name}) => project_path(project_name, project_data), + Action::List => project_list(project_data), + }; + + return None; +} + +fn new_project(project_name: String, project_path: String, mut project_data: ProjectData) { + if project_data.contains_key(&project_name) { + println!("Project with the name {} already exists!", project_name); + return; + } + + let project_path = std::path::Path::new(&project_path); + + if !project_path.exists() { + fs::create_dir_all(&project_path).ok(); + } + + let project_path = match project_path.canonicalize() { + Ok(path) => path, + Err(error) => { + println!("An error occured while resolving the given path. {}", error); + return; + } + }; + + project_data.insert(project_name.to_owned(), project_path.into_os_string().into_string().expect("Invalid path!")); + + save_project_data(project_data).expect("Error while saving data file!"); + println!("Successfully created new project {}!", project_name); +} + +fn delete_project(project_name: String, mut project_data: ProjectData) { + if !project_data.contains_key(&project_name) { + println!("No project with the name {} exists!", project_name); + return; + } + + let project_path_string = &project_data[&project_name]; + let project_path = std::path::Path::new(project_path_string); + + println!("Are you sure you want to remove project with name {}? WARNING: This will also delete all files at {}! (y/N) ", project_name, project_path_string); + let mut buffer = String::new(); + io::stdin().read_line(&mut buffer).expect("Error while reading stdin!"); + if buffer.trim() != "y" { + println!("Aborting..."); + return; + } + + fs::remove_dir_all(project_path).expect("Error while deleting project directory!"); + + project_data.remove(&project_name); + + save_project_data(project_data).expect("Error while saving data file!"); + println!("Successfully removed project {}!", project_name); +} + +fn project_path(project_name: String, project_data: ProjectData) { + let project_path = project_data.get(&project_name); + + if let Some(project_path) = project_path { + println!("{}", project_path); + } else { + println!("No such project!") + }; +} + +fn project_list(project_data: ProjectData) { + for project_name in project_data.keys() { + println!("{}", project_name); + } +} +