Skip to content

Commit 9a46eb5

Browse files
committed
- Add support for clean multiline commands (auto-joined with &&)
1 parent 7497bf6 commit 9a46eb5

12 files changed

Lines changed: 117 additions & 44 deletions

File tree

op

Lines changed: 61 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,14 @@ opcode_context() {
4747
fi
4848
}
4949

50+
abort() {
51+
echo "$1" >&2
52+
exit "${2:-1}"
53+
}
54+
5055
need_config() {
5156
if [[ ! -f $CONFIG_FILE ]]; then
52-
echo "Cannot find config file ($CONFIG_FILE)"
53-
exit 1
57+
abort "Cannot find config file ($CONFIG_FILE)"
5458
fi
5559
}
5660

@@ -63,28 +67,63 @@ opcode_context() {
6367
exit 1
6468
fi
6569

66-
exact="^${CODE}:[[:space:]]*(.+)$"
67-
fuzzy="^${CODE}[^\:]*:[[:space:]]*(.+)$"
70+
COMMAND=$(get_command_from_file "exact")
71+
if [[ -z $COMMAND ]]; then
72+
COMMAND=$(get_command_from_file "fuzzy")
73+
fi
74+
}
6875

69-
# shellcheck disable=SC2162
70-
while IFS= read line || [ -n "$line" ]; do
71-
if [[ $line =~ $exact ]]; then
72-
COMMAND="${BASH_REMATCH[1]}"
73-
break
74-
fi
75-
done <"$CONFIG_FILE"
76+
get_command_from_file() {
77+
local pattern
7678

77-
if [[ -n "$COMMAND" ]]; then
78-
return
79+
if [[ $1 == "fuzzy" ]]; then
80+
pattern="^${CODE}[^:]*:[[:space:]]*(.*)$"
81+
else
82+
pattern="^${CODE}:[[:space:]]*(.*)$"
7983
fi
8084

81-
# shellcheck disable=SC2162
82-
while IFS= read line || [ -n "$line" ]; do
83-
if [[ $line =~ $fuzzy ]]; then
84-
COMMAND="${BASH_REMATCH[1]}"
85-
break
85+
local command=""
86+
local collect_command=false
87+
local temp_line=""
88+
89+
while IFS= read -r line || [[ -n $line ]]; do
90+
if [[ -n "$temp_line" ]]; then
91+
trimmed_line=${line#"${line%%[![:space:]]*}"}
92+
temp_line="${temp_line%\\}$trimmed_line"
93+
if [[ "$line" =~ \\$ ]]; then
94+
continue
95+
else
96+
line="$temp_line"
97+
temp_line=""
98+
fi
99+
fi
100+
101+
if [[ "$line" =~ \\$ ]]; then
102+
trimmed_line=${line#"${line%%[![:space:]]*}"}
103+
temp_line="${temp_line%\\}$trimmed_line"
104+
continue
105+
fi
106+
107+
if $collect_command; then
108+
if [[ -z "${line}" || ! "${line}" =~ ^[[:space:]]+ ]]; then
109+
break
110+
fi
111+
112+
trimmed_line="${line#"${line%%[![:space:]]*}"}"
113+
command="${command:+$command && }$trimmed_line"
114+
else
115+
if [[ $line =~ $pattern ]]; then
116+
command="${BASH_REMATCH[1]}"
117+
if [[ -n $command ]]; then
118+
break
119+
fi
120+
121+
collect_command=true
122+
fi
86123
fi
87124
done <"$CONFIG_FILE"
125+
126+
echo "$command"
88127
}
89128

90129
run_command() {
@@ -97,8 +136,7 @@ opcode_context() {
97136
eval "$COMMAND" "$@"
98137
fi
99138
else
100-
echo "Code not found: $CODE"
101-
exit 1
139+
abort "Code not found: $CODE"
102140
fi
103141
}
104142

@@ -135,7 +173,7 @@ opcode_context() {
135173

136174
list_codes() {
137175
need_config
138-
regex="^([^#:]+):"
176+
regex="^([a-zA-Z0-9_-]+):"
139177
break_regex="^[[:space:]]*private[[:space:]]*$"
140178

141179
# shellcheck disable=SC2162
@@ -199,8 +237,7 @@ opcode_context() {
199237
# shellcheck disable=SC2086
200238
echo $COMMAND
201239
else
202-
echo "Code not found: $CODE"
203-
exit 1
240+
abort "Code not found: $CODE"
204241
fi
205242

206243
fi
@@ -270,7 +307,7 @@ opcode_context() {
270307
bold() { printf "\e[1m%b\e[0m\n" "$*"; }
271308

272309
opcode_initialize() {
273-
VERSION="0.6.5"
310+
VERSION="0.7.0"
274311
LONG_USAGE=false
275312
set_config_file
276313
set -e

op.conf

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
1-
test: op approvals && op shellcheck && op shfmt
1+
test:
2+
op approvals
3+
op shellcheck
4+
op shfmt
25
#? Run all tests
36

47
approvals: test/approve
58
#? Run approval tests
69

7-
shellcheck: shellcheck op setup && green "PASS: shellcheck"
10+
shellcheck:
11+
shellcheck op setup
12+
green "PASS: shellcheck"
813
#? Run approval tests
914

10-
shfmt: shfmt -ci -i 2 -d op setup && green "PASS: shfmt"
15+
shfmt:
16+
shfmt -ci -i 2 -d op setup
17+
green "PASS: shfmt"
1118
#? Run shfmt tests
1219

13-
spell: codespell && green "PASS: codespell"
20+
spell:
21+
codespell
22+
green "PASS: codespell"
1423
#? Run spell checker
1524

1625
man-show: op man | man -l
@@ -19,18 +28,19 @@ man-show: op man | man -l
1928
man-save: op man > doc/op.1
2029
#? Convert the markdown man page to man
2130

22-
version: \
23-
sed -i "s/$(op --version)/$1/g" op README.md doc/op.md && \
24-
op man-save && \
25-
git commit -am "version bump $1" && \
31+
version:
32+
[[ -n $1 ]] || abort 'Usage: op version NEW_VERSION'
33+
sed -i "s/$(op --version)/$1/g" op README.md doc/op.md
34+
op man-save
35+
git commit -am "version bump $1"
2636
git tag v$1
2737
#? Update version, regenerate and tag
28-
#? usage: op version NEW_VERSION
38+
#? Usage: op version NEW_VERSION
2939

3040
private
3141

32-
man: \
33-
export DATE=$(date "+%B %Y") && \
34-
export VERSION=$(op --version) && \
42+
man:
43+
export DATE=$(date "+%B %Y")
44+
export VERSION=$(op --version)
3545
envsubst <doc/op.md |pandoc -f markdown-smart -s --to man
3646
#? Generate man to stdout after substituting template vars

test/execute_spec.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@ describe "op <command>"
5656

5757
context "with multiline config"
5858
cd ./fixtures/multiline
59-
approve "op who"
59+
approve "op multi"
60+
cd ../../
61+
62+
context "with concatenated config"
63+
cd ./fixtures/multiline
64+
approve "op concat"
6065
cd ../../
6166

6267
context "with a private command"

test/fixtures/empty-dir/approvals/op_help

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
opcode 0.6.5 - local command shortcuts
1+
opcode 0.7.0 - local command shortcuts
22

33
Usage:
44
op CODE [ARGS]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.6.5
1+
0.7.0
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
why who what
1+
one multi concat advanced-multi two
File renamed without changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
one
2+
two
3+
three
File renamed without changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
echo one && echo two && echo three

0 commit comments

Comments
 (0)