Skip to content

Commit

Permalink
Revert "Use dictionaries (#47)" (#48)
Browse files Browse the repository at this point in the history
This reverts commit 9848d7b.
  • Loading branch information
denisidoro authored Sep 23, 2019
1 parent 9848d7b commit 2753e84
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 230 deletions.
6 changes: 3 additions & 3 deletions cheats/kubernetes.cheat
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
# Print resource documentation
kubectl explain <resource>

# Get nodes (add option '-o wide' for details)
# Get nodes (add option `-o wide` for details)
kubectl get nodes

# Get namespaces
kubectl get namespaces

# Get pods from namespace (add option '-o wide' for details)
# Get pods from namespace (add option `-o wide` for details)
kubectl get pods -n <namespace>

# Get pods from all namespace (add option '-o wide' for details)
# Get pods from all namespace (add option `-o wide` for details)
kubectl get pods --all-namespaces

# Get services from namespace
Expand Down
15 changes: 8 additions & 7 deletions src/arg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

ARG_REGEX="<[0-9a-zA-Z_]+>"

arg::dict() {
local -r fn="$(awk -F'---' '{print $1}')"
local -r opts="$(awk -F'---' '{print $2}')"
arg::fn() {
awk -F'---' '{print $1}'
}

dict::new fn "$fn" opts "$opts"
arg::opts() {
awk -F'---' '{print $2}'
}

arg::interpolate() {
Expand All @@ -29,10 +30,10 @@ arg::pick() {

local -r prefix="$ ${arg}:"
local -r length="$(echo "$prefix" | str::length)"
local -r arg_dict="$(grep "$prefix" "$cheat" | str::sub $((length + 1)) | arg::dict)"
local -r arg_description="$(grep "$prefix" "$cheat" | str::sub $((length + 1)))"

local -r fn="$(echo "$arg_dict" | dict::get fn)"
local -r args_str="$(echo "$arg_dict" | dic::get opts | tr ' ' '\n' || echo "")"
local -r fn="$(echo "$arg_description" | arg::fn)"
local -r args_str="$(echo "$arg_description" | arg::opts | tr ' ' '\n' || echo "")"
local arg_name=""

for arg_str in $args_str; do
Expand Down
2 changes: 1 addition & 1 deletion src/cheat.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ cheat::from_selection() {
local -r cheats="$1"
local -r selection="$2"

local -r tags="$(echo "$selection" | dict::get tags)"
local -r tags="$(echo "$selection" | selection::tags)"

for cheat in $cheats; do
if grep -q "% $tags" "$cheat"; then
Expand Down
94 changes: 0 additions & 94 deletions src/dict.sh

This file was deleted.

5 changes: 2 additions & 3 deletions src/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

source "${SCRIPT_DIR}/src/arg.sh"
source "${SCRIPT_DIR}/src/cheat.sh"
source "${SCRIPT_DIR}/src/dict.sh"
source "${SCRIPT_DIR}/src/health.sh"
source "${SCRIPT_DIR}/src/misc.sh"
source "${SCRIPT_DIR}/src/opts.sh"
Expand All @@ -16,7 +15,7 @@ handler::main() {
local -r selection="$(ui::select "$cheats")"
local -r cheat="$(cheat::from_selection "$cheats" "$selection")"
[ -z "$cheat" ] && exit 67
local cmd="$(selection::dict "$selection" "$cheat")"
local cmd="$(selection::command "$selection" "$cheat")"
local arg value

while $interpolation; do
Expand Down Expand Up @@ -48,7 +47,7 @@ handler::preview() {
local -r selection="$(echo "$query" | selection::standardize)"
local -r cheats="$(cheat::find)"
local -r cheat="$(cheat::from_selection "$cheats" "$selection")"
[ -n "$cheat" ] && selection::dict "$selection" "$cheat"
[ -n "$cheat" ] && selection::command "$selection" "$cheat"
}

main() {
Expand Down
14 changes: 11 additions & 3 deletions src/selection.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,26 @@ selection::standardize() {
local -r tags="$(echo "$str" | awk -F'[' '{print $NF}' | tr -d ']')"
local -r core="$(echo "$str" | sed -e "s/ \[${tags}\]$//")"

dict::new core "$core" tags "$tags"
echo "${core}^${tags}"
}

selection::core() {
cut -d'^' -f1
}

selection::tags() {
cut -d'^' -f2
}

selection::core_is_comment() {
grep -qE '^#'
}

selection::dict() {
selection::command() {
local -r selection="$1"
local -r cheat="$2"

local -r core="$(echo "$selection" | dict::get core)"
local -r core="$(echo $selection | selection::core)"

if echo "$core" | selection::core_is_comment; then
grep "$core" "$cheat" -A999 \
Expand Down
3 changes: 1 addition & 2 deletions src/ui.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ ui::pick() {

ui::select() {
local -r cheats="$1"
# local -r script_path="$(which navi | head -n1 || echo "${SCRIPT_DIR}/navi")"
local -r script_path="$(echo "${SCRIPT_DIR}/navi")"
local -r script_path="$(which navi | head -n1 || echo "${SCRIPT_DIR}/navi")"
local -r preview_cmd="echo \"{}\" | tr ' ' '^' | xargs -I% \"${script_path}\" preview %"

local args=()
Expand Down
10 changes: 0 additions & 10 deletions test/core.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,6 @@ test::run() {
eval "$*" && test::success || test::fail
}

test::equals() {
local -r actual="$(cat | tr -d '\n')"
local -r expected="$(echo "${1:-}" | tr -d '\n' | sed 's/\\n//g')"

if [[ "$actual" != "$expected" ]]; then
echo "Expected '${expected}' but got '${actual}'"
return 2
fi
}

test::finish() {
echo
if [ $FAILED -gt 0 ]; then
Expand Down
106 changes: 0 additions & 106 deletions test/dict_test.sh

This file was deleted.

2 changes: 1 addition & 1 deletion test/run
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -euo pipefail
export SCRIPT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
source "${SCRIPT_DIR}/test/core.sh"

tests="$(find "$SCRIPT_DIR/test" -iname "*${1:-}*_test.sh")"
tests="$(find "$SCRIPT_DIR/test" -iname '*_test.sh')"

for test in $tests; do
source "$test"
Expand Down

0 comments on commit 2753e84

Please sign in to comment.