diff options
Diffstat (limited to 'bash/moon-maker/mm')
-rwxr-xr-x | bash/moon-maker/mm | 83 |
1 files changed, 75 insertions, 8 deletions
diff --git a/bash/moon-maker/mm b/bash/moon-maker/mm index 699d542..b008e90 100755 --- a/bash/moon-maker/mm +++ b/bash/moon-maker/mm @@ -26,6 +26,40 @@ add-moons () { fi } +delete-moon-by-moon-id () { + if [[ -d ".moons" ]]; then + rm .moons/*"$1"* + echo -e "The moon $1 has been removed" + else + path=$(pwd) + while [ -n "$path" ]; do + path=${path%/*} + if [ -d "$path/.moons" ]; then + rm "$path/.moons/"*"$1"* + echo -e "The moon $1 has been removed" + return + fi + done + fi +} + +delete-moons-by-planet () { + if [[ -d ".moons" ]]; then + rm .moons/"$1"* + echo -e "All moons orbiting $1 have been deleted." + else + path=$(pwd) + while [ -n "$path" ]; do + path=${path%/*} + if [ -d "$path/.moons" ]; then + rm "$path/.moons/$1"* + echo -e "All moons orbiting $1 have been deleted." + return + fi + done + fi +} + open-moons () { if [[ -d ".moons" ]]; then for file in .moons/$1; do @@ -47,6 +81,38 @@ open-moons () { fi } +list-moons-by-planet () { + if [[ -d ".moons" ]]; then + ls -a .moons/"$1"* + else + path=$(pwd) + while [ -n "$path" ]; do + path=${path%/*} + if [ -d "$path/.moons" ]; then + ls -a "$path/.moons/$1"* + return + fi + done + fi +} + +rename-planet-and-orbiting-moons () { + if [[ -d ".moons" ]]; then + for file in .moons/"$1"* ; do mv "$file" "${file//$1/$2}" ; done + echo -e "Planets and moons have been renamed from $1 to $2" + else + path=$(pwd) + while [ -n "$path" ]; do + path=${path%/*} + if [ -d "$path/.moons" ]; then + for file in "$path/.moons/$1"* ; do mv "$file" "${file//$1/$2}" ; done + echo -e "Planets and moons have been renamed from $1 to $2" + return + fi + done + fi +} + PRE_SHORT_RAND=$(openssl rand -base64 6) SHORT_RAND=${PRE_SHORT_RAND//\//_} @@ -62,6 +128,7 @@ help_text() { usage: ./mm -i(nit)....initialize a directory to store moon files -o(pen)....open a planet file and all of its moon files + -l(ist)....list all moons for a given planet file -a(dd).....create a new moon file for a given planet file -d(elete)..remove a moon file by its id -c(lear)...remove all moon files for a given planet file @@ -91,23 +158,23 @@ elif [[ "${1-}" =~ ^-*a(dd)?$ ]]; then if [ $# -ge 3 ]; then FILE_TYPE="$3" # optionally pass a file type you'd like to append to the moon else - FILE_TYPE="txt" # if you don't provide a file type, assume .txt for the new moon + FILE_TYPE="txt" # if you don't provide a file type assume .txt for the new moon fi NEW_MOON="$2"."$SHORT_RAND"."$FILE_TYPE" add-moons "$NEW_MOON" "$2" exit +elif [[ "${1-}" =~ ^-*l(ist)?$ ]]; then + list-moons-by-planet "$2" + exit elif [[ "${1-}" =~ ^-*d(elete)?$ ]]; then - echo "delete" + delete-moon-by-moon-id "$2" exit elif [[ "${1-}" =~ ^-*c(lear)?$ ]]; then - echo "clear" + delete-moons-by-planet "$2" exit elif [[ "${1-}" =~ ^-*u(pdate)?$ ]]; then - echo "update" - exit -elif [[ "${1-}" =~ ^-*z(eplin)?$ ]]; then - echo -e "zeplin" - $EDITOR "$2" + mv "$2" "$3" + rename-planet-and-orbiting-moons "$2" "$3" exit fi |