aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Gattozzi <mgattozzi@gmail.com>2019-11-26 15:33:58 -0500
committerMichael Gattozzi <mgattozzi@gmail.com>2019-11-26 15:40:35 -0500
commitb543d090d15d013834874d25f72dc5d57986a303 (patch)
tree1105a7ca24260440eb64a8f265bb6b22e8632448
parent51e92e14357ff7abca4ba04f7a3580b6a66ff442 (diff)
Add logging output to ticket
ticket used to just run without any kind of logging for commands that weren't just printing tickets to the console or getting any kind of information as to what was going on. This change adds logging with info level for the end user by default, with debug and trace statements while developing the code being an option via the RUST_LOG env var.
-rw-r--r--Cargo.lock2
-rw-r--r--ticket/Cargo.toml2
-rw-r--r--ticket/src/main.rs47
3 files changed, 48 insertions, 3 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 545fb18..e978942 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -903,7 +903,9 @@ version = "0.1.0"
dependencies = [
"anyhow 1.0.22 (registry+https://github.com/rust-lang/crates.io-index)",
"colored 1.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
"paw 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "pretty_env_logger 0.3.1 (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",
diff --git a/ticket/Cargo.toml b/ticket/Cargo.toml
index 3110147..1c5c9eb 100644
--- a/ticket/Cargo.toml
+++ b/ticket/Cargo.toml
@@ -15,3 +15,5 @@ serde = { version = "1.0", features = ["derive"] }
shared = { path = "../shared" }
structopt = { version = "0.3", features = ["paw"] }
toml = "0.5"
+log = "0.4"
+pretty_env_logger = "0.3"
diff --git a/ticket/src/main.rs b/ticket/src/main.rs
index 9005471..7e05adf 100644
--- a/ticket/src/main.rs
+++ b/ticket/src/main.rs
@@ -3,6 +3,7 @@ use anyhow::{
Result,
};
use colored::*;
+use log::*;
use rustyline::{
error::ReadlineError,
Editor,
@@ -35,32 +36,44 @@ enum Args {
#[paw::main]
fn main(args: Args) {
+ env::var("RUST_LOG").map(drop).unwrap_or_else(|_| {
+ env::set_var("RUST_LOG", "info");
+ });
+ pretty_env_logger::init();
if let Err(e) = match args {
Args::Init => init(),
Args::New => new(),
Args::Show { id } => show(id),
Args::Close { id } => close(id),
} {
- eprintln!("{}", e);
+ error!("{}", e);
std::process::exit(1);
}
}
fn init() -> Result<()> {
let root = find_root()?.join(".dev-suite").join("ticket");
+ debug!("Creating ticket directory at {}.", root.display());
+ debug!("Creating open directory.");
fs::create_dir_all(&root.join("open"))?;
+ debug!("Creating closed directory");
fs::create_dir_all(&root.join("closed"))?;
+ trace!("Done initializing tickets.");
+ info!("Created ticket directory at {}.", root.display());
Ok(())
}
fn new() -> Result<()> {
+ debug!("Getting ticket root.");
let ticket_root = ticket_root()?;
+ trace!("Got ticket root: {}", ticket_root.display());
let open = ticket_root.join("open");
let closed = ticket_root.join("closed");
let description = ticket_root.join("description");
let mut ticket_num = 1;
// Fast enough for now but maybe not in the future
+ debug!("Getting number of tickets total.");
for entry in fs::read_dir(&open)?.chain(fs::read_dir(&closed)?) {
let entry = entry?;
let path = entry.path();
@@ -68,6 +81,8 @@ fn new() -> Result<()> {
ticket_num += 1;
}
}
+ debug!("Ticket Total: {}", ticket_num - 1);
+ debug!("Next Ticket ID: {}", ticket_num);
let mut rl = Editor::<()>::new();
let title = match rl.readline("Title: ") {
@@ -78,19 +93,28 @@ fn new() -> Result<()> {
line
}
Err(ReadlineError::Interrupted) | Err(ReadlineError::Eof) => {
+ debug!("Exiting due to Ctrl-C or Ctrl-D.");
process::exit(0);
}
Err(e) => return Err(e.into()),
};
+ debug!("Opening up editor.");
+ trace!(
+ "Create buffer file for the description at {}.",
+ description.display()
+ );
fs::File::create(&description)?;
Command::new(&env::var("EDITOR").unwrap_or_else(|_| "vi".into()))
.arg(&description)
.spawn()?
.wait()?;
+ trace!("Read the file into memory.");
let description_contents = fs::read_to_string(&description)?;
+ trace!("Removing the file.");
fs::remove_file(&description)?;
+ debug!("Creating ticket in memory.");
let t = Ticket {
title,
status: Status::Open,
@@ -99,6 +123,7 @@ fn new() -> Result<()> {
description: description_contents,
};
+ debug!("Converting ticket to toml and writing to disk.");
fs::write(
open.join(&format!(
"{}-{}.toml",
@@ -111,6 +136,7 @@ fn new() -> Result<()> {
)),
toml::to_string_pretty(&t)?,
)?;
+ trace!("Finished writing data to disk.");
Ok(())
}
@@ -120,18 +146,24 @@ fn ticket_root() -> Result<PathBuf> {
}
fn show(id: usize) -> Result<()> {
+ debug!("Getting ticket root.");
let ticket_root = ticket_root()?;
+ trace!("Ticket root at {}.", ticket_root.display());
let open = ticket_root.join("open");
let closed = ticket_root.join("closed");
let mut found = false;
// Fast enough for now but maybe not in the future
+ debug!("Looking for ticket.");
for entry in fs::read_dir(&open)?.chain(fs::read_dir(&closed)?) {
let entry = entry?;
let path = entry.path();
+ trace!("Looking at entry {}.", path.display());
if path.is_file() {
if let Some(file_name) = path.file_name().and_then(|f| f.to_str()) {
+ trace!("Entry is a file.");
if file_name.starts_with(&id.to_string()) {
+ trace!("This is the expected entry.");
let ticket = toml::from_slice::<Ticket>(&fs::read(&path)?)?;
println!(
"{}",
@@ -161,26 +193,35 @@ fn show(id: usize) -> Result<()> {
if found {
Ok(())
} else {
- bail!("No ticket with id {} exists", id);
+ bail!("No ticket with id {} exists.", id);
}
}
fn close(id: usize) -> Result<()> {
+ debug!("Getting ticket root.");
let ticket_root = ticket_root()?;
+ trace!("Ticket root at {}.", ticket_root.display());
let open = ticket_root.join("open");
let closed = ticket_root.join("closed");
let mut found = false;
// Fast enough for now but maybe not in the future
+ debug!("Looking for open ticket with id {}", id);
for entry in fs::read_dir(&open)? {
let entry = entry?;
let path = entry.path();
+ trace!("Looking at entry {}.", path.display());
if path.is_file() {
if let Some(file_name) = path.file_name().and_then(|f| f.to_str()) {
if file_name.starts_with(&id.to_string()) {
+ trace!("The ticket is open and exists.");
+ debug!("Reading in the ticket from disk and setting it to closed.");
let mut ticket = toml::from_slice::<Ticket>(&fs::read(&path)?)?;
ticket.status = Status::Closed;
+ debug!("Writing ticket to disk in the closed directory.");
fs::write(closed.join(file_name), toml::to_string_pretty(&ticket)?)?;
+ debug!("Removing old ticket.");
fs::remove_file(&path)?;
+ trace!("Removed the old ticket.");
found = true;
break;
}
@@ -190,7 +231,7 @@ fn close(id: usize) -> Result<()> {
if found {
Ok(())
} else {
- bail!("No ticket with id {} exists", id);
+ bail!("No ticket with id {} exists.", id);
}
}