Skip to content

Commit

Permalink
add some error contexts (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshrotenberg authored Mar 14, 2024
1 parent 2d1617d commit d83e57d
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/cmd/edit.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{fs::read_to_string, path::Path};

use anyhow::Result;
use anyhow::{Context, Result};
use clap::Args;
use edit::edit;

Expand All @@ -13,7 +13,7 @@ pub(crate) struct EditArgs {
}

pub(crate) fn run(args: &EditArgs) -> Result<()> {
let adr_dir = find_adr_dir()?;
let adr_dir = find_adr_dir().context("No ADR directory found")?;

let adr = find_adr(Path::new(&adr_dir), &args.name)?;
let content = read_to_string(adr.clone())?;
Expand Down
8 changes: 4 additions & 4 deletions src/cmd/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::{
path::{Path, PathBuf},
};

use crate::adr::{find_adr_dir, get_links, get_title, list_adrs, read_adr_dir_file};
use anyhow::Result;
use crate::adr::{find_adr_dir, get_links, get_title, list_adrs};
use anyhow::{Context, Result};
use clap::{Args, Subcommand};

#[derive(Debug, Args)]
Expand Down Expand Up @@ -44,7 +44,7 @@ pub(crate) fn run(args: &GenerateCommands) -> Result<()> {
}

fn run_toc(args: &TocArgs) -> Result<()> {
let adr_dir = find_adr_dir()?;
let adr_dir = find_adr_dir().context("No ADR directory found")?;
let adrs = list_adrs(Path::new(&adr_dir))?;

println! {"# Architecture Decision Records\n"};
Expand All @@ -69,7 +69,7 @@ fn run_toc(args: &TocArgs) -> Result<()> {
}

fn run_graph(args: &GraphArgs) -> Result<()> {
let adr_dir = read_adr_dir_file()?;
let adr_dir = find_adr_dir().context("No ADR directory found")?;
let adrs = list_adrs(Path::new(&adr_dir))?;

let extension = args
Expand Down
23 changes: 16 additions & 7 deletions src/cmd/init.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::{fs::create_dir_all, path::PathBuf};
use std::{
fs::create_dir_all,
path::{Path, PathBuf},
};

use anyhow::Result;
use anyhow::{Context, Result};
use clap::Args;
use serde::Serialize;
use tinytemplate::TinyTemplate;
Expand All @@ -24,15 +27,18 @@ struct InitAdrContext {
}

pub(crate) fn run(args: &InitArgs) -> Result<()> {
create_dir_all(&args.directory)?;
create_dir_all(&args.directory)
.with_context(|| format!("Unable to create {}", args.directory.display()))?;

let number = next_adr_number(Path::new(&args.directory))
.context("Unable to determine next ADR number")?;

let number = next_adr_number(&args.directory)?;
let title = "Record architecture decisions";

let filename = format_adr_path(&args.directory, number, title);

let init_context = InitAdrContext {
number: next_adr_number(&args.directory)?,
number,
date: now()?,
};

Expand All @@ -43,8 +49,11 @@ pub(crate) fn run(args: &InitArgs) -> Result<()> {

let mut tt = TinyTemplate::new();
tt.add_template("init_adr", INIT_TEMPLATE)?;
let rendered = tt.render("init_adr", &init_context)?;
std::fs::write(&filename, rendered)?;
let rendered = tt
.render("init_adr", &init_context)
.context("Unable to render template")?;
std::fs::write(&filename, rendered)
.with_context(|| format!("Unable to write ADR file: {}", filename.display()))?;

println!("{}", filename.display());

Expand Down
20 changes: 11 additions & 9 deletions src/cmd/link.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::path::Path;

use anyhow::Result;
use anyhow::{Context, Result};
use clap::Args;

use crate::adr::{self, append_status, find_adr_dir};
use crate::adr::{append_status, find_adr, find_adr_dir, get_title};

#[derive(Debug, Args)]
pub(crate) struct LinkArgs {
Expand All @@ -18,24 +18,26 @@ pub(crate) struct LinkArgs {
}

pub(crate) fn run(args: &LinkArgs) -> Result<()> {
let adr_dir = find_adr_dir()?;
let adr_dir = find_adr_dir().context("No ADR directory found")?;

let source = adr::find_adr(Path::new(&adr_dir), &args.source)?;
let source =
find_adr(Path::new(&adr_dir), &args.source).context("Unable to find source ADR")?;
let source_filename = source.file_name().unwrap().to_str().unwrap();
let source_title = adr::get_title(&source)?;
let source_title = get_title(&source).context("Unable to get title for source ADR")?;

let target = adr::find_adr(Path::new(&adr_dir), &args.target.to_string())?;
let target = find_adr(Path::new(&adr_dir), &args.target.to_string())
.context("Unable to find target ADR")?;
let target_filename = target.file_name().unwrap().to_str().unwrap();
let target_title = adr::get_title(&target)?;
let target_title = get_title(&target).context("Unable to get title for target ADR")?;

let source_link = format!("{} [{}]({})", args.link, target_title, target_filename);
let target_link = format!(
"{} [{}]({})",
args.reverse_link, source_title, source_filename
);

append_status(&source, &source_link)?;
append_status(&target, &target_link)?;
append_status(&source, &source_link).context("Unable to append status for source ADR")?;
append_status(&target, &target_link).context("Unable to append status for target ADR")?;

Ok(())
}
4 changes: 2 additions & 2 deletions src/cmd/list.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::Result;
use anyhow::{Context, Result};
use clap::Args;

use crate::adr::{find_adr_dir, list_adrs};
Expand All @@ -7,7 +7,7 @@ use crate::adr::{find_adr_dir, list_adrs};
pub(crate) struct ListArgs {}

pub(crate) fn run(_args: &ListArgs) -> Result<()> {
let adr_dir = find_adr_dir()?;
let adr_dir = find_adr_dir().context("No ADR directory found")?;

let adrs = list_adrs(&adr_dir)?;
for adr in adrs {
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/new.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::Result;
use anyhow::{Context, Result};
use clap::Args;
use edit::edit;
use serde::Serialize;
Expand Down Expand Up @@ -35,7 +35,7 @@ struct NewAdrContext {
}

pub(crate) fn run(args: &NewArgs) -> Result<()> {
let adr_dir = find_adr_dir()?;
let adr_dir = find_adr_dir().context("No ADR directory found")?;
let number = next_adr_number(&adr_dir)?;

let title = args.title.join(" ");
Expand Down

0 comments on commit d83e57d

Please sign in to comment.