Skip to content

Commit

Permalink
Add edit command
Browse files Browse the repository at this point in the history
  • Loading branch information
dvaillant committed Jan 29, 2019
1 parent a682a5b commit 9220859
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 1 deletion.
38 changes: 38 additions & 0 deletions src/adr-edit
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash
set -e
eval "$($(dirname $0)/adr-config)"

## usage: adr edit [ADR_NUMBER]

## The last ADR, or the specified ADR NUMBER, is opened for
## editing in the editor specified by the VISUAL or EDITOR
## environment variable (VISUAL is preferred; EDITOR is
## used if VISUAL is not set).

if [ ! -z "$1" ]
then
editid=$(printf "%04d" $1)
fi

if [[ -z "$VISUAL" && -z "$EDITOR" ]]
then
echo "ERROR: editor not configured"
exit 1
fi

dstdir=$("$adr_bin_dir/_adr_dir")

if [[ -d $dstdir && -z "$editid" ]]
then
editid=$(ls $dstdir | grep -Eo '^[0-9]+' | sort -rn | head -1)
fi

dstfile=$dstdir/$editid-*.md

if [ -e $dstfile ]
then
${VISUAL:-${EDITOR:-true}} $dstfile
else
echo "ERROR: ADR $editid not found"
exit 1
fi
2 changes: 1 addition & 1 deletion tests/autocomplete.expected
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
_adr_autocomplete adr
config generate help init link list new upgrade-repository
config edit generate help init link list new upgrade-repository
_adr_autocomplete adr li
link list
_adr_autocomplete adr generate
Expand Down
31 changes: 31 additions & 0 deletions tests/edit-adr.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
adr init architecture-log
architecture-log/0001-record-architecture-decisions.md
adr new First Record
architecture-log/0002-first-record.md

export VISUAL
export EDITOR

if adr edit
then
echo ERROR: should have failed
fi
ERROR: editor not configured

VISUAL=
EDITOR=fake-editor
adr edit
EDITOR architecture-log/0002-first-record.md

VISUAL=fake-visual
EDITOR=
adr edit
VISUAL architecture-log/0002-first-record.md

VISUAL=fake-visual
EDITOR=fake-editor
adr edit
VISUAL architecture-log/0002-first-record.md

adr edit 1
VISUAL architecture-log/0001-record-architecture-decisions.md
24 changes: 24 additions & 0 deletions tests/edit-adr.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
adr init architecture-log
adr new First Record

export VISUAL
export EDITOR

if adr edit
then
echo ERROR: should have failed
fi

VISUAL=
EDITOR=fake-editor
adr edit

VISUAL=fake-visual
EDITOR=
adr edit

VISUAL=fake-visual
EDITOR=fake-editor
adr edit

adr edit 1

0 comments on commit 9220859

Please sign in to comment.