Skip to content

Instantly share code, notes, and snippets.

@jmfayard
Last active October 12, 2022 06:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmfayard/89755b8c7e61ac8afc9db6ff4d3ddd9c to your computer and use it in GitHub Desktop.
Save jmfayard/89755b8c7e61ac8afc9db6ff4d3ddd9c to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
fail() {
echo "ERROR: $1"
exit 1
}
isInstalled() {
which "$1" > /dev/null
}
openUrls() {
URLS="$*"
echo "Open URLs $URLS"
if isInstalled xdg-open; then
# Linux
echo -e $URLS | xargs -n 1 xdg-open 2> /dev/null
else
# MacOS
open $URLS
fi
}
isInstalled gum || fail "Install gum from https://github.com/charmbracelet/gum"
git rev-parse > /dev/null 2>&1 || fail "You must be in a git repository"
HEIGHT="7"
BROWSE="xdg-open" # linux
isInstalled "$BROWSE" || BROWSE="open" # mac
ROOT=$(git rev-parse --show-toplevel )
FILE="$ROOT/bookmarks.properties"
SEARCH=$( echo "$*" | xargs) # | xargs is nused to trim the string
TEMP=$( mktemp )
REGEX="^\s*[^ =]+\s*=\s*http"
test -r "$FILE" || fail "Create '$FILE' file first, format key = URL"
echo "Reading bookmarks from $FILE"
if test -z "$SEARCH" ; then
grep -E "$REGEX" "$FILE" > "$TEMP"
else
grep -E "$REGEX" "$FILE" | grep "$SEARCH" > "$TEMP"
fi
KEYS=$(cut -d = -f 1,1 "$TEMP" | xargs )
if test -z "$KEYS" ; then
fail "No keys found for search=$SEARCH in $FILE"
elif echo "$KEYS" | grep -q ' ' ; then
echo "$ goto $SEARCH ... where?"
KEY=$(gum choose --height "$HEIGHT" $KEYS)
else
KEY="$KEYS"
fi
KEY=$(echo "$KEY" | xargs)
test -z "$KEY" && exit 0
URL=$(grep "$KEY" "$TEMP" | cut -d = -f 2,2 | head -n 1)
openUrls $URL
rm "$TEMP"
echo "🎉 We hope you considered GOTO helpful! "

GOTO considered helpful

If you share a repo with your colleagues, why not sharing the most useful bookmarks to work on that repo?

Usage:

./goto

./goto <search>

For this to work, you need to create a bookmarks.properties file

# Usage:
# $ goto
# $ goto refreshVersions
doc.kotest=https://kotest.io/
doc.refreshVersion=https://github.com/jmfayard/refreshVersions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment