diff options
Diffstat (limited to 'templates/.zsh/functions')
-rw-r--r-- | templates/.zsh/functions/dirty.zsh | 24 | ||||
-rw-r--r-- | templates/.zsh/functions/ebdeploy.zsh | 3 | ||||
-rw-r--r-- | templates/.zsh/functions/ec2.zsh | 6 | ||||
-rw-r--r-- | templates/.zsh/functions/ghclone.zsh | 19 | ||||
-rw-r--r-- | templates/.zsh/functions/git-branches.zsh | 23 | ||||
-rw-r--r-- | templates/.zsh/functions/prs.zsh | 3 | ||||
-rw-r--r-- | templates/.zsh/functions/rfc.zsh | 0 | ||||
-rw-r--r-- | templates/.zsh/functions/rp.zsh | 7 | ||||
-rw-r--r-- | templates/.zsh/functions/sheb.zsh | 22 | ||||
-rw-r--r-- | templates/.zsh/functions/track.zsh | 81 |
10 files changed, 188 insertions, 0 deletions
diff --git a/templates/.zsh/functions/dirty.zsh b/templates/.zsh/functions/dirty.zsh new file mode 100644 index 0000000..19a550d --- /dev/null +++ b/templates/.zsh/functions/dirty.zsh @@ -0,0 +1,24 @@ +function dirty() { + base=~/code/src/github.com/virtyx-technologies + yellow='\033[0;33m' + green='\033[0;32m' + nc='\033[0m' + for repo in $(ls "$base"); do + p="$base/$repo" + is_dirty=$(git --git-dir="$p/.git" --work-tree="$p" diff --shortstat 2> /dev/null | tail -n1) + branch=$(git --git-dir="$p/.git" --work-tree="$p" rev-parse --abbrev-ref HEAD 2> /dev/null) + if [[ $branch != "master" && $branch != "HEAD" ]]; then + pr=$(curl --silent "https://benburwell:$GITHUB_TOKEN@api.github.com/repos/virtyx-technologies/$repo/pulls?state=open&head=virtyx-technologies:$branch" | jq --raw-output --monochrome-output '.[0]._links.html.href') + pr_link=$([[ $pr != "null" ]] && echo "($pr)") + if [[ $is_dirty != "" ]]; then + echo "$repo: ${yellow}$branch${nc} $pr_link" + else + echo "$repo: ${green}$branch${nc} $pr_link" + fi + else + if [[ $is_dirty ]]; then + echo "$repo: ${yellow}$branch${nc} $pr_link" + fi + fi + done +} diff --git a/templates/.zsh/functions/ebdeploy.zsh b/templates/.zsh/functions/ebdeploy.zsh new file mode 100644 index 0000000..76b8a04 --- /dev/null +++ b/templates/.zsh/functions/ebdeploy.zsh @@ -0,0 +1,3 @@ +function ebdeploy() { + eb deploy --timeout 30 --label "$(git rev-parse --short HEAD)-$(date +%Y-%m-%dT%H:%M:%SZ)" +} diff --git a/templates/.zsh/functions/ec2.zsh b/templates/.zsh/functions/ec2.zsh new file mode 100644 index 0000000..dfaf8e1 --- /dev/null +++ b/templates/.zsh/functions/ec2.zsh @@ -0,0 +1,6 @@ +function ec2() { + if [[ "$#" == "0" ]]; then + aws ec2 describe-instances | jq --raw-output 'include "aws"; getInstanceMetadata' | column -t -s $'\t' | sort + fi +} + diff --git a/templates/.zsh/functions/ghclone.zsh b/templates/.zsh/functions/ghclone.zsh new file mode 100644 index 0000000..7b976ef --- /dev/null +++ b/templates/.zsh/functions/ghclone.zsh @@ -0,0 +1,19 @@ +function ghclone() { + local username + local repo + local target_dir + + if [[ $# -lt 1 ]]; then + echo "Usage: ghclone <username/repo>" + return 1 + fi + + username=$(echo "$1" | cut -d'/' -f1) + repo=$(echo "$1" | cut -d'/' -f2) + target_dir="$PROJECTS/src/github.com/$username" + mkdir -p "$target_dir" + url="git@github.com:$username/$repo.git" + echo "Cloning $url into $target_dir..." + git clone "$url" "$target_dir/$repo" + cd "$target_dir/$repo" || return +} diff --git a/templates/.zsh/functions/git-branches.zsh b/templates/.zsh/functions/git-branches.zsh new file mode 100644 index 0000000..4636cab --- /dev/null +++ b/templates/.zsh/functions/git-branches.zsh @@ -0,0 +1,23 @@ +green=`tput setaf 2` +reset=`tput sgr0` + +git-branches() { + set -f + git branch | while read line; do + current=" " + name=${line##\* } ## removes leading * for current + + if [ ! "$name" = "$line" ]; then + current="${green}* " + fi + + description=`git config "branch.$name.description"` + + if [ "$description" != "" ]; then + description=" : $description" + fi + + echo "${reset}${current}${name}${description}${reset}" + done + set +f +} diff --git a/templates/.zsh/functions/prs.zsh b/templates/.zsh/functions/prs.zsh new file mode 100644 index 0000000..9a80c8d --- /dev/null +++ b/templates/.zsh/functions/prs.zsh @@ -0,0 +1,3 @@ +prs () { + hub browse -- pulls +} diff --git a/templates/.zsh/functions/rfc.zsh b/templates/.zsh/functions/rfc.zsh new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/templates/.zsh/functions/rfc.zsh diff --git a/templates/.zsh/functions/rp.zsh b/templates/.zsh/functions/rp.zsh new file mode 100644 index 0000000..1db3b16 --- /dev/null +++ b/templates/.zsh/functions/rp.zsh @@ -0,0 +1,7 @@ +# GitHub Request Pull +# +# Assuming the current git repo's `origin` is on GitHub, open the pull request +# editor for the current branch. +function rp() { + hub pull-request +} diff --git a/templates/.zsh/functions/sheb.zsh b/templates/.zsh/functions/sheb.zsh new file mode 100644 index 0000000..64efe66 --- /dev/null +++ b/templates/.zsh/functions/sheb.zsh @@ -0,0 +1,22 @@ +function sheb() { + # if an address wasn't specified, use the ec2 command to select an instance + if [[ $# == "0" ]]; then + 1=$(ec2 | fzf --no-multi --prompt="ec2 instance> " | sed 's/ .*//') + if [[ -z "$1" ]]; then + return 1 + fi + fi + + # first add key to agent if necessary + if [[ ! $(ssh-add -L) ]]; then + ssh-add -K ~/.ssh/id_rsa + fi + + if [[ $(echo "$1" | grep "@") ]]; then + # If a username was provided, use it + ssh -J virtyx -t "$1" "sudo su -" + else + # Otherwise, assume username is "ec2-user" + ssh -J virtyx -t "ec2-user@$1" "sudo su -" + fi +} diff --git a/templates/.zsh/functions/track.zsh b/templates/.zsh/functions/track.zsh new file mode 100644 index 0000000..e60f944 --- /dev/null +++ b/templates/.zsh/functions/track.zsh @@ -0,0 +1,81 @@ +local function dateimpl() { + if [[ $(uname) == "Darwin" ]]; then + echo "gdate" + else + echo "date" + fi +} + +local function datediff() { + local d0=$($(dateimpl) -d"$1" +%s) + local d1=$($(dateimpl) -d"$2" +%s) + echo "$(($d1 - $d0))" +} + +function track() { + if [[ $# -lt 1 ]]; then + echo "Usage: track <start|stop|hours|archive>" && return 1 + fi + local cmd=$1 + local trackdir + + if [[ $TRACK_ROOT ]]; then + trackdir=$TRACK_ROOT + else + trackdir=~/.track + fi + local logfile="$trackdir/log" + local startfile="$taskdir/started" + local archivedir="$trackdir/archive" + + if [[ $cmd == "archive" ]]; then + mkdir -p $archivedir + mv "$logfile" "$archivedir/$($(dateimpl) --iso-8601=seconds)" + return 0 + fi + + if [[ $# -lt 2 ]]; then + echo "Usage: track $cmd <task>" && return 1 + fi + local task=$2 + local taskdir="$trackdir/task/$task" + + mkdir -p "$taskdir" + + if [[ $cmd == "start" ]]; then + if [[ -f $startfile ]]; then + echo "Task $task was already started at $(cat $startfile)" + return 1 + fi + $(dateimpl) --iso-8601=seconds > "$startfile" + return 0 + fi + + if [[ $cmd == "stop" ]]; then + if [[ ! -f $startfile ]]; then + echo "Task $task has not been started" + return 1 + fi + local started=$(cat "$startfile") + local ended=$($(dateimpl) --iso-8601=seconds) + echo "$task\t$started\t$ended" >> "$logfile" + rm "$startfile" + return 0 + fi + + if [[ $cmd == "hours" ]]; then + local lines=("${(@f)$(grep "^$task" < "$logfile" | cut -f2,3)}") + local sum=0 + for line in $lines; do + local t0=$(echo "$line" | cut -f1) + local t1=$(echo "$line" | cut -f2) + diff=$(datediff "$t0" "$t1") + sum=$((sum + diff)) + done + echo "$(($sum / 3600.0))" + return 0 + fi + + echo "Unrecognized command $cmd" + return 1 +} |