From 2cd27b8dd40c7361f6f2b8832e210a58f30f3639 Mon Sep 17 00:00:00 2001 From: Michael Gattozzi Date: Thu, 21 Nov 2019 10:59:44 -0500 Subject: Move find_root function into the new shared crate This cleans up the init function using the modified find_root function for ticket and moves it into a new shared crate so that other tools that might be built can use it. This means we can easily find the root of a git repo no matter where in the repo one is and build paths relative to it. Closes #3 --- .../closed/3-create-a-find_root-function.toml | 12 ++++ .../ticket/open/3-create-a-find_root-function.toml | 12 ---- Cargo.lock | 8 +++ Cargo.toml | 1 + shared/Cargo.toml | 10 +++ shared/src/lib.rs | 23 +++++++ ticket/Cargo.toml | 1 + ticket/src/main.rs | 71 ++++------------------ 8 files changed, 68 insertions(+), 70 deletions(-) create mode 100644 .dev-suite/ticket/closed/3-create-a-find_root-function.toml delete mode 100644 .dev-suite/ticket/open/3-create-a-find_root-function.toml create mode 100644 shared/Cargo.toml create mode 100644 shared/src/lib.rs diff --git a/.dev-suite/ticket/closed/3-create-a-find_root-function.toml b/.dev-suite/ticket/closed/3-create-a-find_root-function.toml new file mode 100644 index 0000000..5057356 --- /dev/null +++ b/.dev-suite/ticket/closed/3-create-a-find_root-function.toml @@ -0,0 +1,12 @@ +title = 'Create a find_root function' +status = 'Closed' +number = 3 +description = ''' +One of the issues with ticket is that it has a bug in init where if it's +not given the path to the root of a git then it will fail. This is +frankly frustrating and can be a bit unexpected in behavior. We fixed +this in the ticket creation with a `find_root` function. This should be +a more generalized function and be shared amongst future tools as this +will be a common operation. It shouldn't matter where in a repo you are, +the tools should just work. +''' diff --git a/.dev-suite/ticket/open/3-create-a-find_root-function.toml b/.dev-suite/ticket/open/3-create-a-find_root-function.toml deleted file mode 100644 index 7ec030e..0000000 --- a/.dev-suite/ticket/open/3-create-a-find_root-function.toml +++ /dev/null @@ -1,12 +0,0 @@ -title = 'Create a find_root function' -status = 'Open' -number = 3 -description = ''' -One of the issues with ticket is that it has a bug in init where if it's -not given the path to the root of a git then it will fail. This is -frankly frustrating and can be a bit unexpected in behavior. We fixed -this in the ticket creation with a `find_root` function. This should be -a more generalized function and be shared amongst future tools as this -will be a common operation. It shouldn't matter where in a repo you are, -the tools should just work. -''' diff --git a/Cargo.lock b/Cargo.lock index 32936cd..a5ec158 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -373,6 +373,13 @@ dependencies = [ "syn 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "shared" +version = "0.1.0" +dependencies = [ + "anyhow 1.0.22 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "strsim" version = "0.8.0" @@ -437,6 +444,7 @@ dependencies = [ "paw 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "rustyline 5.0.4 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.102 (registry+https://github.com/rust-lang/crates.io-index)", + "shared 0.1.0", "structopt 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/Cargo.toml b/Cargo.toml index 0554ab7..5651345 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,4 +1,5 @@ [workspace] members = [ + "shared", "ticket", ] diff --git a/shared/Cargo.toml b/shared/Cargo.toml new file mode 100644 index 0000000..a072d12 --- /dev/null +++ b/shared/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "shared" +version = "0.1.0" +authors = ["Michael Gattozzi "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +anyhow = "1.0" diff --git a/shared/src/lib.rs b/shared/src/lib.rs new file mode 100644 index 0000000..9fdcc27 --- /dev/null +++ b/shared/src/lib.rs @@ -0,0 +1,23 @@ +use anyhow::{bail, Result}; +use std::{env, path::PathBuf}; + +pub fn find_root() -> Result { + let mut location = env::current_dir()?; + let mut found_root = false; + + for loc in location.ancestors() { + let mut loc = loc.join(".git"); + if loc.exists() { + loc.pop(); + found_root = true; + location = loc.canonicalize()?; + break; + } + } + + if found_root { + Ok(location) + } else { + bail!("Unable to find a valid git repo"); + } +} diff --git a/ticket/Cargo.toml b/ticket/Cargo.toml index fd97293..3110147 100644 --- a/ticket/Cargo.toml +++ b/ticket/Cargo.toml @@ -12,5 +12,6 @@ colored = "1.9" paw = "1.0" rustyline = "5.0" serde = { version = "1.0", features = ["derive"] } +shared = { path = "../shared" } structopt = { version = "0.3", features = ["paw"] } toml = "0.5" diff --git a/ticket/src/main.rs b/ticket/src/main.rs index 8b9b542..00b6715 100644 --- a/ticket/src/main.rs +++ b/ticket/src/main.rs @@ -1,31 +1,14 @@ -use anyhow::{ - bail, - Result, -}; +use anyhow::{bail, Result}; use colored::*; -use rustyline::{ - error::ReadlineError, - Editor, -}; -use serde::{ - Deserialize, - Serialize, -}; -use std::{ - env, - fs, - path::PathBuf, - process, - process::Command, -}; +use rustyline::{error::ReadlineError, Editor}; +use serde::{Deserialize, Serialize}; +use shared::find_root; +use std::{env, fs, path::PathBuf, process, process::Command}; #[derive(structopt::StructOpt)] enum Args { /// Initialize the repo to use ticket - Init { - #[structopt(default_value = ".", parse(from_os_str))] - location: PathBuf, - }, + Init, New, Show { id: usize, @@ -38,7 +21,7 @@ enum Args { #[paw::main] fn main(args: Args) { if let Err(e) = match args { - Args::Init { location } => init(location), + Args::Init => init(), Args::New => new(), Args::Show { id } => show(id), Args::Close { id } => close(id), @@ -48,19 +31,11 @@ fn main(args: Args) { } } -fn init(location: PathBuf) -> Result<()> { - if location.join(".git").is_dir() { - let root = location.join(".dev-suite").join("ticket"); - fs::create_dir_all(&root)?; - fs::create_dir_all(&root.join("open"))?; - fs::create_dir_all(&root.join("closed"))?; - Ok(()) - } else { - bail!( - "{} is not a valid git repository", - location.canonicalize()?.display() - ) - } +fn init() -> Result<()> { + let root = find_root()?.join(".dev-suite").join("ticket"); + fs::create_dir_all(&root.join("open"))?; + fs::create_dir_all(&root.join("closed"))?; + Ok(()) } fn new() -> Result<()> { @@ -125,28 +100,8 @@ fn new() -> Result<()> { Ok(()) } -fn find_root() -> Result { - let mut location = env::current_dir()?; - let mut found_root = false; - - for loc in location.ancestors() { - let loc = loc.join(".dev-suite"); - if loc.exists() { - found_root = true; - location = loc.canonicalize()?; - break; - } - } - - if found_root { - Ok(location) - } else { - bail!("Unable to find a dev-suite enabled repo"); - } -} - fn ticket_root() -> Result { - Ok(find_root()?.join("ticket")) + Ok(find_root()?.join(".dev-suite").join("ticket")) } fn show(id: usize) -> Result<()> { -- cgit v1.2.3