From cd2473e885cf1b5437091bb5d0226cf6b256763c Mon Sep 17 00:00:00 2001 From: Lonenso Date: Sun, 20 Mar 2022 15:20:45 +0800 Subject: [PATCH] fix: readarray doesn't work on osx, change shebang to /bin/bash since failed to use posix sh to expand tilde --- scripts/git/cheatsheets | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/scripts/git/cheatsheets b/scripts/git/cheatsheets index b5c0493..f126725 100755 --- a/scripts/git/cheatsheets +++ b/scripts/git/cheatsheets @@ -1,16 +1,19 @@ -#!/bin/sh -e +#!/bin/bash -e init() { - readarray d < <(yq -c '.cheatpaths[]' "$(cheat -C | awk '{print $2}')") - - for c in "${d[@]}"; + if ! cheat -C > /dev/null; then + exit 1 + fi + while IFS=$'\t' read -r name path source; do - name="$(echo ${c} | yq -r '.name')" - path="$(echo ${c} | yq -r '.path')" + # failed to use posix sh to expand tilde path="${path/#\~/$HOME}" - src="$( echo ${c} | yq -r '.source')" + # git clone when path not exists or path exists but empty + if [ ! -d "$path" ] || [ -d "$path" ] && [ -z "$(ls -A "$path")" ]; then + echo "Init $name" + git clone "$source" "$path" + fi + done < <(yq -r '.cheatpaths[] | [.name, .path, .source] | @tsv' "$(cheat -C | awk '{print $2}')") - [ ! -d "$path" ] && (echo "Init $name"; git clone $src $path) || : - done echo "Finished init" }