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 --- ticket/Cargo.toml | 1 + ticket/src/main.rs | 71 ++++++++++-------------------------------------------- 2 files changed, 14 insertions(+), 58 deletions(-) (limited to 'ticket') 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