2020-07-02 14:31:38 +00:00
# Text formatting utilities
bold=$(tput bold)
underline=$(tput sgr 0 1)
reset=$(tput sgr0)
purple=$(tput setaf 171)
red=$(tput setaf 1)
green=$(tput setaf 76)
tan=$(tput setaf 3)
blue=$(tput setaf 38)
header() {
printf "\n${bold}${purple}========== %s ==========${reset}\n" "$@"
}
arrow() {
printf " ➜ $@\n"
}
success() {
printf "${green} ✔ %s${reset}\n" "$@"
}
error() {
printf "${red} ✖ %s${reset}\n" "$@"
}
warning() {
printf "${tan} ➜ %s${reset}\n" "$@"
}
underline() {
printf "${underline}${bold}%s${reset}\n" "$@"
}
bold() {
printf "${bold}%s${reset}\n" "$@"
}
note() {
printf "${underline}${bold}${blue}Note:${reset} ${blue}%s${reset}\n" "$@"
}
# Browser interaction utilities
2019-03-01 08:17:42 +00:00
function google() {
open -na "Google Chrome" --args "https://www.google.com/search?q=$*"
}
function stackoverflow() {
open -na "Google Chrome" --args "https://www.google.com/search?q=site:stackoverflow.com $*"
}
function github() {
open -na "Google Chrome" --args "https://github.com/search?q=$*"
}
function hacker() {
open -na "Google Chrome" --args "https://hn.algolia.com/?sort=byDate&query=$*"
}
function gmail() {
open -na "Google Chrome" --args "https://mail.google.com/mail/u/0"
}
function gmail2() {
open -na "Google Chrome" --args "https://mail.google.com/mail/u/1"
}
2020-06-24 07:51:04 +00:00
function cicdboard() {
open -na "Google Chrome" --args "$JIRA_URL/secure/RapidBoard.jspa?rapidView=457&view=planning.nodetail"
}
function cicddashboard() {
open -na "Google Chrome" --args "$JIRA_URL/secure/Dashboard.jspa?selectPageId=13131"
2019-03-01 08:17:42 +00:00
}
function issues() {
2019-08-18 15:25:54 +00:00
jira issue jql "status = Open AND text ~ \"$*\" ORDER BY Created DESC"
2019-03-01 08:17:42 +00:00
}
2019-03-01 08:28:18 +00:00
function calendar() {
open -na "Google Chrome" --args "https://calendar.google.com/calendar/r?tab=mc"
}
2019-03-01 08:51:19 +00:00
function asana() {
open -na "Google Chrome" --args "https://app.asana.com"
}
2019-07-18 10:03:52 +00:00
function bookmarks() {
open -na "Google Chrome" --args "https://github.com/MorganGeek/bookmarks/blob/master/README.md"
}
function spotify() {
2019-11-09 14:31:15 +00:00
open -na "Google Chrome" --args "https://open.spotify.com/search/$*"
2019-07-18 10:03:52 +00:00
}
function lob() {
open -na "Google Chrome" --args "https://lobste.rs"
}
2019-07-22 16:27:08 +00:00
function logtalk() {
path_swilgt=$(find /usr/local/Cellar/logtalk -name "*swilgt.sh" 2>/dev/null)
sh "$path_swilgt"
}
2019-07-23 12:29:00 +00:00
function archive() {
open -na "Google Chrome" --args "https://web.archive.org/web/*/$*"
}
2019-07-25 06:10:40 +00:00
function git_listobjectsbysize() {
tempFile=$(mktemp)
IFS=$'\n'
for commitSHA1 in $(git rev-list --all); do
2019-10-13 11:24:34 +00:00
git ls-tree -r --long "$commitSHA1" >>"$tempFile"
2019-07-25 06:10:40 +00:00
done
# sort files by SHA1, de-dupe list and finally re-sort by filesize
sort --key 3 "$tempFile" | \
2019-10-13 11:24:34 +00:00
uniq | \
sort --key 4 --numeric-sort --reverse
2019-07-25 06:10:40 +00:00
# remove temp file
rm -f "$tempFile"
}
function top_commands() {
2019-07-25 07:20:12 +00:00
history | cat | awk '{CMD[$2]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a; }' | grep -v "./" | column -c3 -s " " -t | sort -nr | nl | head -n50
2019-07-25 06:10:40 +00:00
}
function top_commands_full() {
2019-07-25 07:20:12 +00:00
history | cat | sed 's/^[0-9 TAB]*//g' | awk '{CMD[$0]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "%\t" a; }' | sort -nr | nl | head -n50
2019-07-25 06:10:40 +00:00
}
2019-07-31 11:28:08 +00:00
function istherenewissues() {
2019-08-18 15:18:09 +00:00
LASTISSUE=$(newissues | sed $'s,\x1b\\[[0-9;]*[a-zA-Z],,g' | awk 'FNR==2{print $2}')
2020-03-06 11:19:52 +00:00
if [[ -f "$HOME/.newjiraissue" ]]
2019-07-31 11:28:08 +00:00
then
2020-03-06 11:19:52 +00:00
previous_jira_issue=$(cat "$HOME/.newjiraissue" | sed $'s,\x1b\\[[0-9;]*[a-zA-Z],,g') # the sed part is for removing output colors
2019-10-13 11:24:34 +00:00
if [ "$LASTISSUE" != "$previous_jira_issue" ]; then
newissues
else
2020-07-02 14:51:30 +00:00
success "no new issue"
2019-10-13 11:24:34 +00:00
fi
2019-07-31 11:28:08 +00:00
fi
2020-03-06 11:19:52 +00:00
echo "$LASTISSUE" > "$HOME/.newjiraissue"
2019-07-31 11:28:08 +00:00
}
2019-10-12 14:09:24 +00:00
function aboutpage() {
year=$(echo "$*" | egrep -Eo '\b[[:digit:]]{4}\b' | head -n1)
2019-08-05 10:56:04 +00:00
if [ -z "$year" ]
then
2019-10-13 11:24:34 +00:00
year=$(curl -sSL "$*" | tr '<' '\r' | \egrep -i "date|datetime" -A 1 | \grep -Eo '\b[[:digit:]]{4}\b' | head -n1)
2019-08-05 10:56:04 +00:00
fi
2019-10-12 14:19:10 +00:00
author=$(curl -sSL "$*" | tr '<' '\r' | \egrep -i "author" -A 1 | \grep -Eo '([A-Z][A-Za-z]+\s([A-Za-z ]+)*)' | head -n1)
2019-10-13 10:00:14 +00:00
title=$(curl -sSL "$*" | tr '<' '<\n' | \grep title -A 1 | head -n1 | sed -E 's/.*<title>(.*)<\/title>.*/\1/' | sed "s/ [^[:alnum:]]*$author//")
2019-08-04 20:44:51 +00:00
yearint=$(($year + 0))
currentyear=$(echo `date +"%Y"`)
2019-10-12 14:09:24 +00:00
if [ ! -z "$author" ]
then
2019-10-13 11:24:34 +00:00
echo "by $author"
2019-10-12 14:09:24 +00:00
fi
2019-10-13 10:00:14 +00:00
if [ ! -z "$title" ]
then
2019-10-13 11:24:34 +00:00
echo "-> $title"
2019-10-13 10:00:14 +00:00
fi
2019-08-04 20:44:51 +00:00
if [[ $yearint -ge 1970 && $yearint -le $currentyear ]]
then
2019-10-13 11:24:34 +00:00
echo "$yearint"
2019-08-04 20:44:51 +00:00
fi
2019-10-13 10:00:14 +00:00
if [ ! -z "$title" ] && [ ! -z "$author" ] && [[ $yearint -ge 1970 && $yearint -le $currentyear ]]; then
2019-10-13 11:24:34 +00:00
echo "[$author]($*) - ($yearint) $title"
2019-10-13 10:00:14 +00:00
fi
2019-08-04 12:34:51 +00:00
}
2019-10-12 14:09:24 +00:00
2019-08-05 09:03:44 +00:00
# Extract a column from a tabular output
# via https://blog.developer.atlassian.com/ten-tips-for-wonderful-bash-productivity/
function col() {
awk -v col=$1 '{print $col}'
}
# Skip first x words in line
# via https://blog.developer.atlassian.com/ten-tips-for-wonderful-bash-productivity/
function skip {
n=$(($1 + 1))
cut -d' ' -f$n-
}
2019-08-08 12:35:18 +00:00
2019-10-22 05:57:44 +00:00
cmd_loc="find . -type f \( \
2019-10-13 11:24:34 +00:00
-name '*.py' \
-o -name '*.rb' \
-o -name '*.go' \
-o -name '*.java' \
2019-10-22 08:21:06 +00:00
-o -name '*.kt' \
2019-10-13 11:24:34 +00:00
-o -name '*.c' -o -name '*.h' \
-o -name '*.js' \
2019-10-22 05:57:44 +00:00
-o -name '*.tsx' \
-o -name '*.md' \
-o -name '*.xml' \
-o -name '*.groovy' \
-o -name '*.gradle' \
-o -name '*.properties' \
\) -exec \cat \{\} \; | LANG=C LC_CTYPE=C sed -e 's/^[ \t]*//;s/[ \t]*$//'"
# Unique lines of code
# Via https://text.causal.agency/004-uloc.txt
function uloc {
eval "$cmd_loc | LANG=C LC_CTYPE=C sort -u | wc -l"
}
# Top lines of code
function toploc {
2019-10-22 08:21:06 +00:00
eval "$cmd_loc | LANG=C LC_CTYPE=C cut -c 1-100 | LANG=C LC_CTYPE=C sort | uniq -c | LANG=C LC_CTYPE=C sort -nr"
2019-09-13 13:42:35 +00:00
}
2019-08-08 12:35:18 +00:00
# Find files bigger than X size and sort them by size
function biggerthan() {
find . -size "+$*" -type f -print0 | xargs -0 ls -Ssh | sort -z
}
2019-08-08 18:11:20 +00:00
# To automatically ls when changing directory
function cd() {
2019-10-13 11:24:34 +00:00
builtin cd "$@" && ls -latr
2019-08-08 18:11:20 +00:00
}
2019-08-16 12:52:59 +00:00
function mouse() {
2019-10-13 11:24:34 +00:00
case "$(uname -s)" in
Darwin)
sh ~/.scripts/mouse_bluetooth.sh
;;
esac
2019-08-16 12:52:59 +00:00
}
2019-08-17 12:16:10 +00:00
function meteo() {
curl "fr.wttr.in/$*"
}
2019-08-17 14:03:44 +00:00
function how_in() {
2019-10-13 11:24:34 +00:00
where="$1"; shift
IFS=+ curl "cht.sh/${where}/$*"
2019-08-17 14:03:44 +00:00
}
2019-08-17 20:26:53 +00:00
function rate() {
curl "http://rate.sx/$*"
}
2020-07-02 14:31:38 +00:00
function transfer() {
2019-09-02 14:35:59 +00:00
# check arguments
if [ $# -eq 0 ];
then
2020-07-02 14:51:30 +00:00
warning "No arguments specified. Usage:\necho transfer /tmp/test.md\ncat /tmp/test.md | transfer test.md"
2019-09-02 14:35:59 +00:00
return 1
fi
# get temporarily filename, output is written to this file show progress can be showed
tmpfile=$( mktemp -t transferXXX )
# upload stdin or file
file=$1
if tty -s;
then
basefile=$(basename "$file" | sed -e 's/[^a-zA-Z0-9._-]/-/g')
if [ ! -e $file ];
then
2020-07-02 14:51:30 +00:00
error "File $file doesn't exists."
2019-09-02 14:35:59 +00:00
return 1
fi
2019-08-17 20:26:53 +00:00
2019-09-02 14:35:59 +00:00
if [ -d $file ];
then
# zip directory and transfer
zipfile=$( mktemp -t transferXXX.zip )
cd $(dirname $file) && zip -r -q - $(basename $file) >> $zipfile
curl --progress-bar --upload-file "$zipfile" "https://transfer.sh/$basefile.zip" >> $tmpfile
rm -f $zipfile
else
# transfer file
curl --progress-bar --upload-file "$file" "https://transfer.sh/$basefile" >> $tmpfile
fi
else
# transfer pipe
curl --progress-bar --upload-file "-" "https://transfer.sh/$file" >> $tmpfile
fi
# cat output link
cat $tmpfile
# cleanup
rm -f $tmpfile
}
2019-09-30 11:21:18 +00:00
# Where is a function defined?
function whichfunc() {
2019-10-13 11:24:34 +00:00
whence -v $1
type -a $1
2019-09-30 11:21:18 +00:00
}
2019-12-01 11:01:19 +00:00
# git shortcuts
function gcrb {
branch=$1
git checkout -b $branch origin/$branch
}
2020-02-08 10:23:45 +00:00
function terraform-compliance { docker run --rm -v "$(pwd):/target" -i -t eerkunt/terraform-compliance "$@"; }
function checkov { docker run -i --rm -v "$(pwd):/tf" bridgecrew/checkov -d /tf "$@" ; }
2020-02-22 15:31:41 +00:00
function vimat {
vim +/$1 $2
}
2020-02-15 14:16:45 +00:00
function copyhooks {
cp -f ~/.git-template/.pre-commit-config.yaml ./
pre-commit install --install-hooks --overwrite
pre-commit run -a
}
2020-02-22 15:31:41 +00:00
function httperr {
curl -s "https://http.cat/$1" | imgcat
}
function terragrunt_color {
BOLD=$(tput bold)
BLACK=$(tput setaf 0)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
BLUE=$(tput setaf 4)
CYAN=$(tput setaf 6)
RESET=$(tput sgr0)
REDBOLD=${RED}${BOLD}
REDRESET=${RESET}${RED}
BLUEBOLD=${BLUE}${BOLD}
BLUERESET=${RESET}${BLUE}
terragrunt ${*} 2>&1 | sed \
-e "s/\(\\[terragrunt\\] \\[.*\\]\)\( [0-9\\/]* [0-9:]*\) /${BLUEBOLD}\1${BLUERESET}\2${RESET} /" \
-e "s/\(\\[terragrunt\\]\)\( [0-9\\/]* [0-9:]*\) /${BLUEBOLD}\1${BLUERESET}\2${RESET} /" \
-e "s/\(Error: \)\(.*\)/${REDBOLD}\1${REDRESET}\2${RESET}/" \
-e "s/\(Hit multiple errors:\)/${REDBOLD}\1${RESET}/" \
-e "s/\(exit status 1\)/${RED}\1${RESET}/" \
-e "s/\( WARNING: \)\(.*\)/${YELLOW}${BOLD}\1${RESET}${YELLOW}\2${RESET}/" \
-e "s/\( Running command: \)\(.*\)/\1${CYAN}\2${RESET}/" \
-e "s/\( *.*: *\)\(\".*\"\)\( => \)\(\".*\"\)/${YELLOW}\1${RED}\2${BLACK}\3${GREEN}\4${RESET}/" \
-e "s/\( *.*: *\".*\"\)/${GREEN}\1${RESET}/"
}
2020-07-02 14:27:08 +00:00
2020-03-06 11:19:52 +00:00
function git-project {
if [ -d "$REPO_PATH" ]; then
REPO_PATH="$(pwd)"
fi
local preview='lsd --color always --icon always --group-dirs first {}'
local dir=$(find $REPO_PATH -maxdepth 3 -type d -name ".git" | sed 's#.git$##' | fzf --select-1 --query="$*" --preview "$preview")
if [[ -n "$dir" ]]; then
2020-07-02 13:51:10 +00:00
cd "$dir" || exit
2020-03-06 11:19:52 +00:00
fi
}
2020-06-21 10:38:50 +00:00
function jenkins-cli {
local script_location=$(find $HOME/code/jenkins-cloudbees-core -name "jenkins-cli.sh")
eval "$script_location $*"
}
2020-06-24 13:21:13 +00:00
function passwords {
bw list items --search "$1" | jq -c '.[] | .name + " " + .login.username + ":" + .login.password + " " + .login.uris[0].uri'
}
function password {
2020-06-24 14:59:50 +00:00
bw get password "$1"
}
function vaultgetsecret {
local secret=$(grep -A 500 "ANSIBLE_VAULT" "$1" | awk '{$1=$1;print}' | \grep -Eo "^[0-9a-z^ ]+$")
local secret_string=$(echo "\$ANSIBLE_VAULT;1.1;AES256\n$secret")
echo "$secret_string" | awk '{$1=$1;print}' | ansible-vault decrypt --vault-password-file=$VAULT_PASSWORD_FILE
}
2020-06-29 11:34:06 +00:00
function setorigin {
gra origin "$1" 2>/dev/null
grset origin "$1"
copyhooks
}
2020-06-24 14:59:50 +00:00
function gitpushallremote {
grv
grv | grep push | awk '{print $1}' | while read -r remote
do
2020-07-02 14:51:30 +00:00
arrow "$remote"
2020-06-24 14:59:50 +00:00
git push --all "$remote"
done
2020-06-24 13:21:13 +00:00
}
2020-06-29 11:34:06 +00:00
function clone {
2020-07-02 13:51:10 +00:00
local folder=$(basename $1 | sed 's/\.git.*//g')
2020-07-02 14:51:30 +00:00
arrow "git project identified as $folder"
if gcls "$1"; then
if [[ -n "$folder" ]]; then
cd "$folder" || exit
copyhooks
else
error "unable to change current directory to : $folder"
fi
else
error "unable to clone repository url : $1"
fi
2020-06-29 11:34:06 +00:00
}
2020-06-21 10:38:50 +00:00
function colorpic {
local picture_url="$1"
2020-07-02 14:51:30 +00:00
arrow "Colorizing $picture_url"
2020-06-24 07:51:04 +00:00
local result_url=$(\curl -F "image=@$picture_url" -H "api-key:$COLORPIC_APIKEY" https://api.deepai.org/api/colorizer -s | jq '.output_url' | strings)
2020-07-02 14:51:30 +00:00
success "Generated $result_url"
arrow "Display in progress..."
2020-06-21 10:38:50 +00:00
eval "\curl -s $result_url | imgcat"
}
2020-06-29 11:34:06 +00:00
function brewadd {
brew install "$1"
ansible 127.0.0.1 -m lineinfile -a "path=~/Brewfile line='brew \"$1\"'"
}
2020-06-30 12:22:03 +00:00
function rssadd {
ansible 127.0.0.1 -m lineinfile -a "path=~/.newsboat/urls line='"$1"'"
newsboat
}
2020-07-02 13:51:10 +00:00
function backupgithub {
curl -sL "https://api.github.com/users/$1/repos" | jq -r '.[] | .ssh_url' | xargs -n1 git clone --mirror --no-hardlinks
}