aboutsummaryrefslogtreecommitdiff
path: root/ticket
diff options
context:
space:
mode:
authorMichael Gattozzi <mgattozzi@gmail.com>2019-11-21 10:59:44 -0500
committerMichael Gattozzi <mgattozzi@gmail.com>2019-11-21 14:21:07 -0500
commit2cd27b8dd40c7361f6f2b8832e210a58f30f3639 (patch)
treef52f9c39570e227f58ee498c3943e50d2e3f732c /ticket
parent5cec4e3f5c551731d1202d892da570ad62d9682d (diff)
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
Diffstat (limited to 'ticket')
-rw-r--r--ticket/Cargo.toml1
-rw-r--r--ticket/src/main.rs71
2 files changed, 14 insertions, 58 deletions
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<PathBuf> {
- 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<PathBuf> {
- Ok(find_root()?.join("ticket"))
+ Ok(find_root()?.join(".dev-suite").join("ticket"))
}
fn show(id: usize) -> Result<()> {