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() {
|
2020-07-12 14:19:25 +00:00
|
|
|
printf "\n${bold}${purple}========== %s ==========${reset}\n" "$@"
|
2020-07-02 14:31:38 +00:00
|
|
|
}
|
|
|
|
arrow() {
|
2020-07-13 05:13:23 +00:00
|
|
|
printf " ➜ %s\n" "$@"
|
2020-07-02 14:31:38 +00:00
|
|
|
}
|
|
|
|
success() {
|
2020-07-12 14:19:25 +00:00
|
|
|
printf "${green} ✔ %s${reset}\n" "$@"
|
2020-07-02 14:31:38 +00:00
|
|
|
}
|
|
|
|
error() {
|
2020-07-12 14:19:25 +00:00
|
|
|
printf "${red} ✖ %s${reset}\n" "$@"
|
2020-07-02 14:31:38 +00:00
|
|
|
}
|
|
|
|
warning() {
|
2020-07-12 14:19:25 +00:00
|
|
|
printf "${tan} ➜ %s${reset}\n" "$@"
|
2020-07-02 14:31:38 +00:00
|
|
|
}
|
|
|
|
underline() {
|
2020-07-12 14:19:25 +00:00
|
|
|
printf "${underline}${bold}%s${reset}\n" "$@"
|
2020-07-02 14:31:38 +00:00
|
|
|
}
|
|
|
|
bold() {
|
2020-07-12 14:19:25 +00:00
|
|
|
printf "${bold}%s${reset}\n" "$@"
|
2020-07-02 14:31:38 +00:00
|
|
|
}
|
|
|
|
note() {
|
2020-07-17 12:52:30 +00:00
|
|
|
printf "${underline}${bold}${blue}Note:${reset} ${blue}%s${reset}\n" "$@"
|
2020-07-02 14:31:38 +00:00
|
|
|
}
|
2020-07-14 13:18:57 +00:00
|
|
|
function substring() {
|
|
|
|
local _length=${#1}
|
|
|
|
local _start=${2}
|
|
|
|
local _end=${3}
|
|
|
|
echo ${1:_start:_length-_end}
|
|
|
|
}
|
|
|
|
|
2020-07-13 05:13:23 +00:00
|
|
|
|
2020-07-02 14:31:38 +00:00
|
|
|
# Browser interaction utilities
|
2020-07-11 11:33:19 +00:00
|
|
|
function browse() {
|
|
|
|
open -na "$DEFAULT_BROWSER" --args "$1"
|
|
|
|
}
|
2019-03-01 08:17:42 +00:00
|
|
|
function google() {
|
2020-07-11 11:33:19 +00:00
|
|
|
browse "https://www.google.com/search?q=$*"
|
2019-03-01 08:17:42 +00:00
|
|
|
}
|
|
|
|
function stackoverflow() {
|
2020-07-11 11:33:19 +00:00
|
|
|
browse "https://www.google.com/search?q=site:stackoverflow.com $*"
|
2019-03-01 08:17:42 +00:00
|
|
|
}
|
|
|
|
function github() {
|
2020-07-11 11:33:19 +00:00
|
|
|
browse "https://github.com/search?q=$*"
|
2019-03-01 08:17:42 +00:00
|
|
|
}
|
|
|
|
function hacker() {
|
2020-07-11 11:33:19 +00:00
|
|
|
browse "https://hn.algolia.com/?sort=byDate&query=$*"
|
2019-03-01 08:17:42 +00:00
|
|
|
}
|
|
|
|
function gmail() {
|
2020-07-11 11:33:19 +00:00
|
|
|
browse "https://mail.google.com/mail/u/0"
|
2019-03-01 08:17:42 +00:00
|
|
|
}
|
|
|
|
function gmail2() {
|
2020-07-11 11:33:19 +00:00
|
|
|
browse "https://mail.google.com/mail/u/1"
|
2019-03-01 08:17:42 +00:00
|
|
|
}
|
2020-06-24 07:51:04 +00:00
|
|
|
function cicdboard() {
|
2020-07-11 11:33:19 +00:00
|
|
|
browse "$JIRA_URL/secure/RapidBoard.jspa?rapidView=457&view=planning.nodetail"
|
2020-06-24 07:51:04 +00:00
|
|
|
}
|
|
|
|
function cicddashboard() {
|
2020-07-11 11:33:19 +00:00
|
|
|
browse "$JIRA_URL/secure/Dashboard.jspa?selectPageId=13131"
|
2019-03-01 08:17:42 +00:00
|
|
|
}
|
2019-03-01 08:28:18 +00:00
|
|
|
function calendar() {
|
2020-07-11 11:33:19 +00:00
|
|
|
browse "https://calendar.google.com/calendar/r?tab=mc"
|
2019-03-01 08:28:18 +00:00
|
|
|
}
|
2019-03-01 08:51:19 +00:00
|
|
|
function asana() {
|
2020-07-11 11:33:19 +00:00
|
|
|
browse "https://app.asana.com"
|
|
|
|
}
|
|
|
|
function confluencetasks() {
|
|
|
|
browse "$CONFLUENCE_URL/plugins/inlinetasks/mytasks.action"
|
|
|
|
}
|
|
|
|
function trello_web() {
|
|
|
|
browse "$TRELLO_BOARD_URL"
|
2019-03-01 08:51:19 +00:00
|
|
|
}
|
2019-07-18 10:03:52 +00:00
|
|
|
function bookmarks() {
|
2020-07-11 11:33:19 +00:00
|
|
|
browse "https://github.com/MorganGeek/bookmarks/blob/master/README.md"
|
2019-07-18 10:03:52 +00:00
|
|
|
}
|
|
|
|
function spotify() {
|
2020-07-11 11:33:19 +00:00
|
|
|
browse "https://open.spotify.com/search/$*"
|
2019-07-18 10:03:52 +00:00
|
|
|
}
|
|
|
|
function lob() {
|
2020-07-11 11:33:19 +00:00
|
|
|
browse "https://lobste.rs"
|
2019-07-18 10:03:52 +00:00
|
|
|
}
|
2019-07-23 12:29:00 +00:00
|
|
|
function archive() {
|
2020-07-11 11:33:19 +00:00
|
|
|
browse "https://web.archive.org/web/*/$*"
|
2019-07-23 12:29:00 +00:00
|
|
|
}
|
2020-07-14 13:20:03 +00:00
|
|
|
function iknowwhatyoudownload() {
|
|
|
|
browse "https://iknowwhatyoudownload.com/en/peer/?ip=$*"
|
|
|
|
}
|
2019-07-25 06:10:40 +00:00
|
|
|
|
2020-07-13 05:13:23 +00:00
|
|
|
# Jira utilities
|
|
|
|
function issues() {
|
|
|
|
jira issue jql "status = Open AND text ~ \"$*\" ORDER BY Created DESC"
|
2019-07-25 06:10:40 +00:00
|
|
|
}
|
2020-07-13 05:13:23 +00:00
|
|
|
function istherenewissues() {
|
|
|
|
LASTISSUE=$(newissues | sed $'s,\x1b\\[[0-9;]*[a-zA-Z],,g' | awk 'FNR==2{print $2}')
|
|
|
|
if [[ -f "$HOME/.newjiraissue" ]]; then
|
|
|
|
previous_jira_issue=$(\cat "$HOME/.newjiraissue" | sed $'s,\x1b\\[[0-9;]*[a-zA-Z],,g') # the sed part is for removing output colors
|
|
|
|
if [ "$LASTISSUE" != "$previous_jira_issue" ]; then
|
|
|
|
newissues
|
|
|
|
else
|
|
|
|
success "no new issue"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
echo "$LASTISSUE" >"$HOME/.newjiraissue"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Prolog / Logtalk
|
|
|
|
function logtalk() {
|
|
|
|
path_swilgt=$(find /usr/local/Cellar/logtalk -name "*swilgt.sh" 2>/dev/null)
|
|
|
|
sh "$path_swilgt"
|
|
|
|
}
|
|
|
|
|
|
|
|
# History / Aliases helpers
|
2019-07-25 06:10:40 +00:00
|
|
|
function top_commands() {
|
2020-07-08 14:59:29 +00:00
|
|
|
local filter="$1"
|
2020-07-12 14:19:25 +00:00
|
|
|
local max_results=${2:-'50'}
|
|
|
|
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 | grep "$filter" | sort -nr | nl | head "-n$max_results"
|
2019-07-25 06:10:40 +00:00
|
|
|
}
|
|
|
|
function top_commands_full() {
|
2020-07-08 14:59:29 +00:00
|
|
|
local filter="$1"
|
2020-07-12 14:19:25 +00:00
|
|
|
local max_results=${2:-'50'}
|
|
|
|
history | \cat | awk '{$1=$1};1' | sed 's/^[0-9 TAB]*//g' | awk '{CMD[$0]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "%\t" a; }' | grep "$filter" | sort -nr | nl | head "-n$max_results"
|
2019-07-25 06:10:40 +00:00
|
|
|
}
|
2020-07-13 05:13:23 +00:00
|
|
|
# Where is a function defined?
|
|
|
|
function whichfunc() {
|
|
|
|
whence -v $1
|
|
|
|
type -a $1
|
|
|
|
}
|
2020-07-17 12:51:39 +00:00
|
|
|
|
|
|
|
source $HOME/Code/dotfiles/dot_scripts/suggest_readable_parameters.sh
|
|
|
|
|
2020-07-15 20:15:19 +00:00
|
|
|
# TODO : suggest spelling fixes
|
2020-07-14 13:22:06 +00:00
|
|
|
function suggest_code_refactoring() {
|
|
|
|
#inspired by : \grep 'awk '\''{$1=$1};1'\' $HOME/Code/dotfiles/dot_zsh*
|
|
|
|
header "code refactoring suggestions"
|
|
|
|
while read -r line; do
|
|
|
|
local short_name=$(echo "$line" | sed -E "s/='.*//g")
|
|
|
|
local alias_value=$(echo "$line" | sed -E 's/[a-zA-Z_-]+=//g')
|
2020-07-15 20:15:19 +00:00
|
|
|
local alias_value_truncated=${alias_value:1:$((${#alias_value} - 2))}
|
|
|
|
\fgrep $alias_value_truncated "$@" 2>/tmp/error.log | \grep -v "alias $short_name=" 1>/dev/null
|
2020-07-14 13:22:06 +00:00
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
success "found $short_name --> $alias_value_truncated"
|
2020-07-15 20:15:19 +00:00
|
|
|
\fgrep "$alias_value_truncated" "$@" --color -n -H --line-buffered | \grep -v "alias $short_name="
|
2020-07-14 13:22:06 +00:00
|
|
|
else
|
|
|
|
local error_message=$(\cat /tmp/error.log)
|
2020-07-15 20:15:19 +00:00
|
|
|
if [ -n "$error_message" ]; then
|
2020-07-14 13:22:06 +00:00
|
|
|
error "issue occured when looking for $alias_value_truncated : $error_message"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done < <(alias | awk -v COUNT=1 'NF>COUNT') # list of all aliases, excluding the less interesting ones (where we use one word for another)
|
|
|
|
}
|
2020-07-11 11:33:19 +00:00
|
|
|
function suggest_aliases() {
|
2020-07-12 14:19:25 +00:00
|
|
|
local search_input_size=${1:-'50'}
|
2020-07-12 08:20:12 +00:00
|
|
|
header "alias recommendations"
|
2020-07-12 14:19:25 +00:00
|
|
|
while read -r line; do
|
2020-07-11 11:33:19 +00:00
|
|
|
local matching_aliases=$(ag "$line")
|
|
|
|
if [ ! -z "$matching_aliases" ]; then
|
2020-07-12 08:20:12 +00:00
|
|
|
success "there is an alias for $line :"
|
2020-07-12 14:19:25 +00:00
|
|
|
while read -r alias_line; do
|
2020-07-12 08:20:12 +00:00
|
|
|
arrow "$alias_line"
|
|
|
|
done < <(echo "$matching_aliases")
|
|
|
|
echo
|
2020-07-12 15:50:17 +00:00
|
|
|
else
|
|
|
|
warning "no alias for $line"
|
2020-07-11 11:33:19 +00:00
|
|
|
fi
|
2020-07-12 14:19:25 +00:00
|
|
|
done < <(top_commands_full "" "$search_input_size" | awk '{ $1=""; $2=""; $3=""; print}' | awk 'NF' | awk '{$1=$1};1' | awk -v COUNT=1 'NF>COUNT' | head "-$search_input_size")
|
2020-07-11 11:33:19 +00:00
|
|
|
}
|
2020-07-13 05:13:23 +00:00
|
|
|
|
|
|
|
# Web Crawling
|
2019-10-12 14:09:24 +00:00
|
|
|
function aboutpage() {
|
|
|
|
year=$(echo "$*" | egrep -Eo '\b[[:digit:]]{4}\b' | head -n1)
|
2020-07-12 14:19:25 +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))
|
2020-07-12 14:19:25 +00:00
|
|
|
currentyear=$(echo $(date +"%Y"))
|
|
|
|
if [ ! -z "$author" ]; then
|
2019-10-13 11:24:34 +00:00
|
|
|
echo "by $author"
|
2019-10-12 14:09:24 +00:00
|
|
|
fi
|
2020-07-12 14:19:25 +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
|
2020-07-12 14:19:25 +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/
|
2020-07-12 14:19:25 +00:00
|
|
|
function skip() {
|
2019-08-05 09:03:44 +00:00
|
|
|
n=$(($1 + 1))
|
|
|
|
cut -d' ' -f$n-
|
|
|
|
}
|
2019-08-08 12:35:18 +00:00
|
|
|
|
2020-07-13 05:13:23 +00:00
|
|
|
# Code search / stats helpers
|
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' \
|
2020-07-09 05:42:09 +00:00
|
|
|
-o -name '*.sh' \
|
2019-10-22 05:57:44 +00:00
|
|
|
-o -name '*.md' \
|
|
|
|
-o -name '*.xml' \
|
2020-07-09 05:42:09 +00:00
|
|
|
-o -name '*.yaml' -o -name '*.yml' \
|
2019-10-22 05:57:44 +00:00
|
|
|
-o -name '*.groovy' \
|
|
|
|
-o -name '*.gradle' \
|
|
|
|
-o -name '*.properties' \
|
2020-07-09 05:42:09 +00:00
|
|
|
\) -exec \cat \{\} \; | LANG=C LC_CTYPE=C sed -e 's/^[ \t]*//;s/[ \t]*$//'"
|
2019-10-22 05:57:44 +00:00
|
|
|
# Unique lines of code
|
|
|
|
# Via https://text.causal.agency/004-uloc.txt
|
2020-07-12 14:19:25 +00:00
|
|
|
function uloc() {
|
2019-10-22 05:57:44 +00:00
|
|
|
eval "$cmd_loc | LANG=C LC_CTYPE=C sort -u | wc -l"
|
|
|
|
}
|
|
|
|
# Top lines of code
|
2020-07-12 14:19:25 +00:00
|
|
|
function toploc() {
|
2020-07-09 05:42:09 +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 | head -50"
|
2019-09-13 13:42:35 +00:00
|
|
|
}
|
2020-07-13 05:13:23 +00:00
|
|
|
function how_in() {
|
|
|
|
where="$1"
|
|
|
|
shift
|
|
|
|
IFS=+ curl "cht.sh/${where}/$*"
|
|
|
|
}
|
2020-07-13 14:19:08 +00:00
|
|
|
# File name references in file
|
|
|
|
function filerefs() {
|
|
|
|
\grep -Eo "\b([a-zA-Z.]+)\.([a-z]{2,4}\b)" "$1" \
|
|
|
|
| sort -u \
|
|
|
|
| egrep -vi "\.com|\.git|\.io|\net|\.org|\.se|\.me|\.fr|contributing\.md"
|
|
|
|
}
|
|
|
|
function invalid_file_refs() {
|
|
|
|
while read -r file_ref; do
|
|
|
|
arrow "processing $file_ref ..."
|
|
|
|
find . -name "$file_ref" 1>/dev/null
|
|
|
|
local exit_status=$?
|
|
|
|
if [ $exit_status -eq 1 ]; then
|
|
|
|
warning "$file_ref does not exist in the project"
|
|
|
|
else
|
|
|
|
success "$file_ref was found in the project"
|
|
|
|
fi
|
|
|
|
arrow "checking if $file_ref is present in the source code..."
|
|
|
|
source_refs=$(\grep "$file_ref" * -r -l | grep -v "$1" | wc -l 2>/dev/null | trim)
|
|
|
|
if [ "$source_refs" -eq 0 ]; then
|
|
|
|
error "$file_ref was not found in source code"
|
|
|
|
else
|
|
|
|
arrow "searching for $file_ref references in soure code..."
|
|
|
|
while read -r source_ref; do
|
|
|
|
success "$file_ref was found in $source_ref"
|
|
|
|
done < <(\grep "$file_ref" * -r -l)
|
|
|
|
fi
|
|
|
|
done < <(filerefs "$1")
|
|
|
|
}
|
2019-09-13 13:42:35 +00:00
|
|
|
|
2020-07-15 20:41:03 +00:00
|
|
|
|
|
|
|
# Text search
|
|
|
|
# run aspell in non interactive mode, to list all mispelled words found
|
|
|
|
function checkenlist() {
|
|
|
|
aspell -d en list < "$1" | sort --unique --ignore-case
|
|
|
|
}
|
|
|
|
|
2020-07-17 12:53:14 +00:00
|
|
|
function checkenremote() {
|
|
|
|
curl -Lks "$1" > /tmp/file && checkenlist /tmp/file
|
|
|
|
}
|
|
|
|
|
2020-07-13 05:13:23 +00:00
|
|
|
# File stats helpers
|
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
|
2020-07-12 14:19:25 +00:00
|
|
|
Darwin)
|
|
|
|
sh ~/.scripts/mouse_bluetooth.sh
|
|
|
|
;;
|
2019-10-13 11:24:34 +00:00
|
|
|
esac
|
2019-08-16 12:52:59 +00:00
|
|
|
}
|
2020-07-13 05:13:23 +00:00
|
|
|
|
2020-07-14 08:35:29 +00:00
|
|
|
# Conversion
|
|
|
|
function epub2pdf() {
|
|
|
|
local epub_file_name=$(echo $1 | sed 's/.epub$/.pdf/')
|
|
|
|
ebook-convert "$1" "$epub_file_name"
|
|
|
|
}
|
|
|
|
|
2020-07-13 05:13:23 +00:00
|
|
|
# Information gathering
|
2019-08-17 12:16:10 +00:00
|
|
|
function meteo() {
|
|
|
|
curl "fr.wttr.in/$*"
|
|
|
|
}
|
2019-08-17 20:26:53 +00:00
|
|
|
function rate() {
|
|
|
|
curl "http://rate.sx/$*"
|
|
|
|
}
|
2020-07-13 05:13:23 +00:00
|
|
|
|
|
|
|
# Uploaders
|
2020-07-02 14:31:38 +00:00
|
|
|
function transfer() {
|
2019-09-02 14:35:59 +00:00
|
|
|
# check arguments
|
2020-07-12 14:19:25 +00:00
|
|
|
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
|
2020-07-12 14:19:25 +00:00
|
|
|
tmpfile=$(mktemp -t transferXXX)
|
2019-09-02 14:35:59 +00:00
|
|
|
|
|
|
|
# upload stdin or file
|
|
|
|
file=$1
|
|
|
|
|
2020-07-12 14:19:25 +00:00
|
|
|
if tty -s; then
|
2019-09-02 14:35:59 +00:00
|
|
|
basefile=$(basename "$file" | sed -e 's/[^a-zA-Z0-9._-]/-/g')
|
|
|
|
|
2020-07-12 14:19:25 +00:00
|
|
|
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
|
|
|
|
2020-07-12 14:19:25 +00:00
|
|
|
if [ -d $file ]; then
|
2019-09-02 14:35:59 +00:00
|
|
|
# zip directory and transfer
|
2020-07-12 14:19:25 +00:00
|
|
|
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
|
2019-09-02 14:35:59 +00:00
|
|
|
rm -f $zipfile
|
|
|
|
else
|
|
|
|
# transfer file
|
2020-07-12 14:19:25 +00:00
|
|
|
curl --progress-bar --upload-file "$file" "https://transfer.sh/$basefile" >>$tmpfile
|
2019-09-02 14:35:59 +00:00
|
|
|
fi
|
|
|
|
else
|
|
|
|
# transfer pipe
|
2020-07-12 14:19:25 +00:00
|
|
|
curl --progress-bar --upload-file "-" "https://transfer.sh/$file" >>$tmpfile
|
2019-09-02 14:35:59 +00:00
|
|
|
fi
|
|
|
|
# cat output link
|
|
|
|
cat $tmpfile
|
|
|
|
|
|
|
|
# cleanup
|
|
|
|
rm -f $tmpfile
|
|
|
|
}
|
2019-12-01 11:01:19 +00:00
|
|
|
|
2020-07-12 14:19:25 +00:00
|
|
|
function checkov() { docker run -i --rm -v "$(pwd):/tf" bridgecrew/checkov -d /tf "$@"; }
|
|
|
|
function vimat() {
|
2020-02-22 15:31:41 +00:00
|
|
|
vim +/$1 $2
|
|
|
|
}
|
2020-07-12 14:19:25 +00:00
|
|
|
function httperr() {
|
2020-07-11 10:25:30 +00:00
|
|
|
curl -s "https://http.cat/$1" | imgcat
|
2020-02-22 15:31:41 +00:00
|
|
|
}
|
2020-07-13 05:13:23 +00:00
|
|
|
|
|
|
|
# Config / Infra as code
|
|
|
|
function terraform-compliance() { docker run --rm -v "$(pwd):/target" -i -t eerkunt/terraform-compliance "$@"; }
|
2020-07-12 14:19:25 +00:00
|
|
|
function terragrunt_color() {
|
2020-02-22 15:31:41 +00:00
|
|
|
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-12 14:19:25 +00:00
|
|
|
function jenkins-cli() {
|
2020-06-21 10:38:50 +00:00
|
|
|
local script_location=$(find $HOME/code/jenkins-cloudbees-core -name "jenkins-cli.sh")
|
|
|
|
eval "$script_location $*"
|
|
|
|
}
|
2020-07-13 05:13:23 +00:00
|
|
|
|
|
|
|
# Security / Secrets helpers
|
2020-07-12 14:19:25 +00:00
|
|
|
function passwords() {
|
2020-06-24 13:21:13 +00:00
|
|
|
bw list items --search "$1" | jq -c '.[] | .name + " " + .login.username + ":" + .login.password + " " + .login.uris[0].uri'
|
|
|
|
}
|
2020-07-12 14:19:25 +00:00
|
|
|
function password() {
|
2020-06-24 14:59:50 +00:00
|
|
|
bw get password "$1"
|
|
|
|
}
|
2020-07-12 14:19:25 +00:00
|
|
|
function vaultgetsecret() {
|
2020-06-24 14:59:50 +00:00
|
|
|
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-07-13 05:13:23 +00:00
|
|
|
|
|
|
|
### Git helpers
|
|
|
|
function backupgithub() {
|
|
|
|
cd "$REPO_PATH"
|
|
|
|
curl -sL "https://api.github.com/users/$1/repos" | jq -r '.[] | .ssh_url' | xargs -n1 git clone --mirror --no-hardlinks
|
|
|
|
}
|
|
|
|
function gitydiff() {
|
|
|
|
local path_to_file="$1"
|
|
|
|
git show "HEAD:$path_to_file" | colordiff -y - "$path_to_file"
|
|
|
|
}
|
|
|
|
function gcrb() {
|
|
|
|
branch=$1
|
|
|
|
git checkout -b $branch origin/$branch
|
|
|
|
}
|
|
|
|
function installhooks() {
|
|
|
|
pre-commit install --install-hooks --overwrite --allow-missing-config
|
|
|
|
}
|
|
|
|
function copyhooks() {
|
|
|
|
cp -f ~/.git-template/.pre-commit-config.yaml ./
|
|
|
|
installhooks
|
|
|
|
runhooks
|
|
|
|
}
|
|
|
|
function git_listobjectsbysize() {
|
|
|
|
tempFile=$(mktemp)
|
|
|
|
IFS=$'\n'
|
|
|
|
for commitSHA1 in $(git rev-list --all); do
|
|
|
|
git ls-tree -r --long "$commitSHA1" >>"$tempFile"
|
|
|
|
done
|
|
|
|
|
|
|
|
# sort files by SHA1, de-dupe list and finally re-sort by filesize
|
|
|
|
sort --key 3 "$tempFile" |
|
|
|
|
uniq |
|
|
|
|
sort --key 4 --numeric-sort --reverse
|
|
|
|
# remove temp file
|
|
|
|
rm -f "$tempFile"
|
|
|
|
}
|
2020-07-12 14:19:25 +00:00
|
|
|
function setorigin() {
|
2020-06-29 11:34:06 +00:00
|
|
|
gra origin "$1" 2>/dev/null
|
|
|
|
grset origin "$1"
|
2020-07-13 05:13:23 +00:00
|
|
|
if [[ "$1" =~ *"$COMPANY_NAME"* ]]; then
|
2020-07-06 14:43:26 +00:00
|
|
|
copyhooks
|
|
|
|
fi
|
2020-06-29 11:34:06 +00:00
|
|
|
}
|
2020-07-12 14:19:25 +00:00
|
|
|
function gitpushcurrentremote() {
|
2020-07-08 15:09:19 +00:00
|
|
|
gitpushallremote "$(git_current_branch)"
|
|
|
|
}
|
2020-07-12 14:19:25 +00:00
|
|
|
function gitpushallremote() {
|
2020-07-08 15:09:19 +00:00
|
|
|
local param_branch="$1"
|
2020-06-24 14:59:50 +00:00
|
|
|
grv
|
2020-07-12 14:19:25 +00:00
|
|
|
grv | grep push | awk '{print $1}' | while read -r remote; do
|
|
|
|
if [ -z "$param_branch" ]; then
|
2020-07-08 15:09:19 +00:00
|
|
|
arrow "pushing all branches to $remote"
|
|
|
|
git push --all "$remote"
|
|
|
|
else
|
|
|
|
arrow "pushing $param_branch to $remote"
|
|
|
|
git push "$remote" "$param_branch"
|
|
|
|
fi
|
2020-06-24 14:59:50 +00:00
|
|
|
done
|
2020-06-24 13:21:13 +00:00
|
|
|
}
|
2020-07-12 14:19:25 +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"
|
2020-07-12 15:50:17 +00:00
|
|
|
arrow "cloning repository ..."
|
2020-07-02 14:51:30 +00:00
|
|
|
if gcls "$1"; then
|
2020-07-12 15:50:17 +00:00
|
|
|
success "repository cloned"
|
2020-07-02 14:51:30 +00:00
|
|
|
if [[ -n "$folder" ]]; then
|
|
|
|
cd "$folder" || exit
|
2020-07-13 05:13:23 +00:00
|
|
|
if [[ "$1:u" =~ *"$COMPANY_NAME:u"* ]]; then
|
2020-07-12 15:50:17 +00:00
|
|
|
arrow "copying pre-commit hooks ..."
|
2020-07-12 14:19:25 +00:00
|
|
|
copyhooks
|
2020-07-06 14:43:26 +00:00
|
|
|
fi
|
2020-07-02 14:51:30 +00:00
|
|
|
else
|
|
|
|
error "unable to change current directory to : $folder"
|
|
|
|
fi
|
|
|
|
else
|
2020-07-17 12:54:47 +00:00
|
|
|
error "unable to clone repository from url : $1"
|
2020-07-02 14:51:30 +00:00
|
|
|
fi
|
2020-06-29 11:34:06 +00:00
|
|
|
}
|
2020-07-17 12:54:47 +00:00
|
|
|
function fork() {
|
|
|
|
# FIXME : go to local copy of the repo after the fork
|
|
|
|
code
|
|
|
|
local folder=$(basename $1 | sed 's/\.git.*//g')
|
|
|
|
gh repo fork "$1"
|
|
|
|
}
|
2020-07-13 05:13:23 +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
|
|
|
|
cd "$dir" || exit
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
function gitignorefor() {
|
|
|
|
local language=${1:-''}
|
|
|
|
if [ ! -d "$HOME/Code/gitignore" ]; then
|
|
|
|
arrow "cloning https://github.com/github/gitignore into $REPO_PATH/gitignore"
|
2020-07-13 14:18:58 +00:00
|
|
|
git clone "https://github.com/github/gitignore $_"
|
2020-07-13 05:13:23 +00:00
|
|
|
fi
|
|
|
|
local gitignore_file=$(fd "$language" "$REPO_PATH/gitignore" | head -n1)
|
|
|
|
if [ ! -z "$gitignore_file" ]; then
|
|
|
|
success "matched gitignore file : $gitignore_file"
|
|
|
|
if [ ! -f "$(pwd)/.gitignore" ]; then
|
|
|
|
arrow "you don't have a $(pwd)/.gitignore file, but that's not an issue :-) ..."
|
|
|
|
fi
|
|
|
|
arrow "copying the file content to your $(pwd)/.gitignore file"
|
|
|
|
adhoc blockinfile -a "block='{{ lookup('file', '$gitignore_file') }}' dest='$(pwd)/.gitignore' create=yes"
|
|
|
|
else
|
|
|
|
error "no gitignore file found for $language"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Miscellaneous helpers
|
2020-07-12 14:19:25 +00:00
|
|
|
function colorpic() {
|
2020-06-21 10:38:50 +00:00
|
|
|
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-07-13 05:13:23 +00:00
|
|
|
|
|
|
|
function bookmarkadd() {
|
|
|
|
adhoc lineinfile -a "path=$HOME/Code/bookmarks/README.md insertafter='"$1"' line='* "$2"'"
|
|
|
|
}
|
|
|
|
function rssadd() {
|
|
|
|
adhoc lineinfile -a "path=~/.newsboat/urls line='"$1"'"
|
|
|
|
newsboat
|
|
|
|
}
|
|
|
|
|
2020-07-13 14:17:33 +00:00
|
|
|
# Via https://stackoverflow.com/a/58598185/2309958
|
|
|
|
# capture the output of a command so it can be retrieved with ret
|
|
|
|
function cap () { tee /tmp/capture.out}
|
|
|
|
# return the output of the most recent command that was captured by cap
|
|
|
|
function ret () { \cat /tmp/capture.out }
|
|
|
|
|
2020-07-13 05:13:23 +00:00
|
|
|
# Package / Dependencies management helpers
|
|
|
|
function adhocbis() {
|
|
|
|
local ansible_output=$(adhoc "$*")
|
|
|
|
echo $ansible_output | sed 's/127.0.0.1.*SUCCESS/WOKE/g'
|
|
|
|
}
|
2020-07-12 14:19:25 +00:00
|
|
|
function brewadd() {
|
2020-06-29 11:34:06 +00:00
|
|
|
brew install "$1"
|
2020-07-12 15:50:17 +00:00
|
|
|
adhoc lineinfile -a "path=~/Brewfile line='brew \"$1\"'"
|
2020-06-29 11:34:06 +00:00
|
|
|
}
|
2020-07-12 14:19:25 +00:00
|
|
|
function pipadd() {
|
2020-07-11 10:25:30 +00:00
|
|
|
pip install "$1"
|
2020-07-12 14:19:25 +00:00
|
|
|
pip freeze >"$HOME/requirements.txt"
|
2020-07-11 10:25:30 +00:00
|
|
|
}
|
2020-07-12 14:19:25 +00:00
|
|
|
function goadd() {
|
2020-07-12 15:50:17 +00:00
|
|
|
adhoc lineinfile -a "path=~/.scripts/godeps.sh line='go get -u -v $1'"
|
2020-07-11 10:25:30 +00:00
|
|
|
go get -u -v "$1"
|
|
|
|
}
|
2020-07-13 05:13:23 +00:00
|
|
|
|
|
|
|
|
2020-07-03 04:22:30 +00:00
|
|
|
# Make a directory and cd to it
|
2020-07-12 14:19:25 +00:00
|
|
|
function take() {
|
2020-07-13 05:13:23 +00:00
|
|
|
mkdir -p "$@" && cd "${@:$#}"
|
2020-07-06 18:03:34 +00:00
|
|
|
}
|
2020-07-13 05:13:23 +00:00
|
|
|
|
|
|
|
# Date / Time management helpers
|
2020-07-12 14:19:25 +00:00
|
|
|
function endofday() {
|
2020-07-06 18:03:34 +00:00
|
|
|
local planned_end=$(moro status 2>&1 | \grep -Eo "Working until ([0-9:]+) will make.*" | uniq | \grep -Eo "([0-9]+:[0-9]+)")
|
|
|
|
local max_hour="$planned_end"
|
2020-07-12 14:19:25 +00:00
|
|
|
local min_hour=$(current_time)
|
2020-07-06 18:03:34 +00:00
|
|
|
if [ -z "$planned_end" ]; then
|
|
|
|
local clockout=$(moro report 2>&1 | \grep -Eo "Clock out.*([0-9:]+)" | \grep -Eo "([0-9]+:[0-9]+)")
|
|
|
|
max_hour="$clockout"
|
|
|
|
fi
|
|
|
|
if is_earlier "$min_hour" "$max_hour"; then
|
|
|
|
arrow "you should keep working, it's only $min_hour while you should work until $max_hour"
|
|
|
|
moro status
|
|
|
|
else
|
|
|
|
warning "you should stop working now because it's later than $max_hour"
|
|
|
|
moro report
|
|
|
|
fi
|
|
|
|
}
|
2020-07-12 14:19:25 +00:00
|
|
|
function convtimetodate() {
|
2020-07-06 18:03:34 +00:00
|
|
|
date -j -f '%H:%M' "$1" +'%Y/%m/%d %H:%M'
|
|
|
|
}
|
2020-07-12 14:19:25 +00:00
|
|
|
function convtimetotimestamp() {
|
2020-07-06 18:03:34 +00:00
|
|
|
date -j -f '%H:%M' "$1" +'%s'
|
|
|
|
}
|
2020-07-12 14:19:25 +00:00
|
|
|
function is_earlier() {
|
2020-07-06 18:03:34 +00:00
|
|
|
local first=$(convtimetotimestamp "$1")
|
|
|
|
local second=$(convtimetotimestamp "$2")
|
|
|
|
if [ "$second" -gt "$first" ]; then
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
fi
|
2020-07-03 04:22:30 +00:00
|
|
|
}
|
2020-07-13 05:13:23 +00:00
|
|
|
|
2020-07-12 14:19:25 +00:00
|
|
|
function dl_stopwords() {
|
2020-07-07 13:19:29 +00:00
|
|
|
curl -Lks https://raw.githubusercontent.com/MorganGeek/bookmarks/master/stopwords.txt -o "$HOME/stopwords.txt"
|
|
|
|
}
|
2020-07-12 14:19:25 +00:00
|
|
|
function file_getwords() {
|
|
|
|
dl_stopwords
|
2020-07-12 20:55:16 +00:00
|
|
|
\cat "$1" | tr '[:upper:]' '[:lower:]' | \grep -o -E '\w{3,}' | \grep --invert-match --word-regexp --fixed-strings --file="$HOME/stopwords.txt" | \sed 's/s$//g' | \sed 's/ing$//g' | sort_count
|
2020-07-07 13:19:29 +00:00
|
|
|
}
|
2020-07-12 14:19:25 +00:00
|
|
|
function file_getpairs() {
|
|
|
|
dl_stopwords
|
2020-07-12 20:55:16 +00:00
|
|
|
\cat "$1" | filter_pairs
|
2020-07-07 13:19:29 +00:00
|
|
|
}
|
2020-07-12 14:19:25 +00:00
|
|
|
function file_dups() {
|
2020-07-12 20:55:16 +00:00
|
|
|
\cat "$1" | sort_count
|
2020-07-08 09:16:34 +00:00
|
|
|
}
|
2020-07-12 14:19:25 +00:00
|
|
|
function foreach_run() {
|
2020-07-11 10:25:30 +00:00
|
|
|
find . -name "$1" -exec "$2" {} \;
|
|
|
|
}
|
2020-07-12 08:20:12 +00:00
|
|
|
# input should be something like : 1-10 to generate one number between 1 and 10
|
2020-07-12 14:19:25 +00:00
|
|
|
function chance() {
|
2020-07-12 08:20:12 +00:00
|
|
|
[[ $(shuf -i "$1" -n 1) == 1 ]]
|
|
|
|
}
|
2020-07-12 14:19:25 +00:00
|
|
|
function runiflucky() {
|
2020-07-12 08:20:12 +00:00
|
|
|
if chance "1-10"; then
|
2020-07-13 14:18:58 +00:00
|
|
|
if alias "$1" 2>/dev/null || (compgen -A function | grep "$1" 1>/dev/null && compgen -A function "$1" 1>/dev/null); then
|
2020-07-12 08:20:12 +00:00
|
|
|
eval "$1"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|