Skip to content

Instantly share code, notes, and snippets.

@matsu-chara
Last active June 23, 2017 05:18
Show Gist options
  • Save matsu-chara/f5944d133a66d1f9fd2f847f076b31fd to your computer and use it in GitHub Desktop.
Save matsu-chara/f5944d133a66d1f9fd2f847f076b31fd to your computer and use it in GitHub Desktop.
go link in bash
#!/bin/bash
CONF_PATH="$HOME/.config/gol/links"
# _gol_record key
_gol_record() {
key="$1"
if [ -n "$key" ]; then
grep "$key " "$CONF_PATH"
fi
}
# gol_get key
gol_get() {
key="$1"
_gol_record "$key" | cut -d ' ' -f 2
}
# gol_add key link
gol_add() {
key="$1"
link="$2"
found=$(gol_get "$key")
if [ -z "$found" ]; then
added=$(cat "$CONF_PATH"; echo "$key $link")
echo "$added" | sed '/^$/d' | sort > "$CONF_PATH"
else
echo "$key was already registered. link = $found"
fi
}
# gol_rm key
gol_rm() {
key="$1"
found=$(gol_get "$key")
record="$key $found"
removed=$(grep -v "$record" "$CONF_PATH")
echo "$removed" > "$CONF_PATH"
}
# gol_ls
gol_ls() {
cat "$CONF_PATH"
}
# gol_open key
gol_open() {
key="$1"
result=$(gol_get "$key")
if [ -n "$result" ]; then
open "$result"
fi
}
# open_peco [prefix]
gol_open_peco() {
prefix="$1"
if [ -z "$prefix" ]; then
target=$(gol_ls)
else
target=$(gol_ls | grep "$prefix")
fi
echo "$target" | peco | cut -d ' ' -f 2 | xargs open
}
# edit
gol_edit() {
"$EDITOR" "$CONF_PATH"
}
# debug
gol_debug() {
"$EDITOR" "$0"
}
# help
help() {
cat << EOS
gol add google https://google.com
gol get google # https://google.com
gol open
gol ls
gol rm google
gol ls
EOS
}
subcommand="$1"
shift
case $subcommand in
add | a)
gol_add "$@"
;;
rm | r)
gol_rm "$@"
;;
ls)
gol_ls
;;
get | g)
gol_get "$@"
;;
open | o)
gol_open "$@"
;;
peco | p)
gol_open_peco "$@"
;;
edit | e)
gol_edit "$@"
;;
debug | d)
gol_debug "$@"
;;
help| h)
gol_help "$@"
;;
*)
original_arg="$subcommand"
gol_open_peco "$original_arg" "$@"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment