Something went wrong. Try again.
Mirroring: https://github.com/MunifTanjim/scripts.sh
Something went wrong. Try again.
scripts.sh ncd
40 kB · 1,557 lines
at main
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558#!/usr/bin/env bash
set -euo pipefail
declare GITHUB_USERNAME="MunifTanjim"
command_exists() { type "${1}" >/dev/null 2>&1}
assert_command() { if ! type "${1}" >/dev/null 2>&1; then echo "${2:-"command not found: ${1}"}" exit 1 fi}
assert_dir() { if ! [[ -d "${1}" ]]; then echo "${2}" exit 1 fi}
assert_val() { if [[ -z "${1}" ]]; then echo "${2}" exit 1 fi}
assert_func() { if ! declare -F "${1}" >/dev/null; then echo "${2}" exit 1 fi}
echo_err() { >&2 echo "$@"}
ncd_config() { assert_command rq
local blob="$(cat ~/.newscred.yaml | rq --input-yaml | jq -r '.ncd')" echo "${blob}" | jq -r ".${1:-""}"}
ncd_workspace() { assert_command rq
local dir="$(cat ~/.newscred.yaml | rq --input-yaml | jq -r '.workspace')" dir="${dir/#\~/${HOME}}" echo "${dir}"}
__pyenv_init() { if [[ "${PYENV_VIRTUAL_INIT:-}" != "1" ]]; then eval "$(pyenv init -)" eval "$(pyenv virtualenv-init -)" fi}
__pyenv_list_versions() { pyenv versions --bare --skip-envs --skip-aliases}
__pyenv_virtualenv_exists() { local -r virtualenvs="$(pyenv virtualenvs --bare)" echo "${virtualenvs}" | grep -q "${1}"}
__pyenv_is_version_installed() { local -r versions="$(__pyenv_list_versions)" echo "${versions}" | grep -q "${1}"}
__pyenv_virtualenv_current() { local name="$(basename "${PYENV_VIRTUAL_ENV:-}")" if test -n "${name}"; then echo "${name}" else pyenv virtualenvs | grep "^* " | awk '{ print $2 }' fi}
__pyenv_virtualenv_activate() { local name="${1}" if [[ "$(__pyenv_virtualenv_current)" = "${name}" ]]; then return fi pyenv activate "${name}"}
__ncd_is_inside_workspace_repo() { [[ "${PWD}" = "$(ncd workspace)"/* ]]}
__ncd_is_container() { ncd_list_container | grep -q "${1}"}
__ncd_assert_inside_workspace_repo() { if ! __ncd_is_inside_workspace_repo; then echo "not inside workspace repo" exit 1 fi}
__ncd_current_repo() { if ! __ncd_is_inside_workspace_repo; then echo "" fi
echo "$(basename "$(git rev-parse --show-toplevel)")"}
__ncd_py_venv_name() { local -r curr_repo="$(__ncd_current_repo)" local venv_name="nc-${curr_repo}" if [[ "${PWD}" != "$(ncd workspace)/${curr_repo}" ]]; then venv_name="${venv_name}${PWD:(($(ncd workspace | wc -c) + ${#curr_repo}))}" venv_name="${venv_name//\//-}" fi echo "${venv_name}"}
__ncd_assert_py_repo_venv() { local -r venv_name="$(__ncd_py_venv_name)" if [[ "$(__pyenv_virtualenv_current)" != "${venv_name}" ]]; then echo "virtualenv not active: ${venv_name}" exit 1 fi}
__ncd_cmd() { local -r cmd="${1}" shift
local -r subcmd="${1:-}" assert_val "${subcmd}" "missing param" shift
local -r func="ncd_${cmd// /_}_${subcmd//:/_}" assert_func "${func}" "invalid param: ${subcmd}"
$func "$@"}
# ncd completionncd_completion() { local dir="$(realpath "${BASH_SOURCE[0]}")" dir="${dir%/*}" local -r completion_file="${dir}/completions/_ncd" if test -f "${completion_file}"; then cat "${completion_file}" else curl -fsSL https://raw.githubusercontent.com/MunifTanjim/scripts.sh/main/completions/_ncd fi}
AWS_CODEARTIFACT_TOKEN_CACHE_FILE="${HOME}/.nc_codeartifact_token"
__ncd_aws_apigateway_resolve_region() { local region="${1:-"${AWS_REGION:-"${AWS_DEFAULT_REGION:-"us-east-1"}"}"}" export AWS_REGION="${region}"}
__ncd_aws_apigateway_resolve_key_id() { local key_id="${1}" api_key="${2}" if [[ -z "${key_id}" ]]; then if [[ -n "${api_key}" ]]; then key_id="$(ncd aws:apigateway:get-api-key --api-key "${api_key}")" else key_id="$(ncd aws:apigateway:get-api-key)" fi fi echo -n "${key_id}"}
# ncd aws apigateway get-api-keyncd_aws_apigateway_get-api-key() { local region="" api_key="" local refresh="false"
while (("$#")); do case "${1}" in --region) region="${2}" shift 2 ;; --api-key) api_key="${2}" shift 2 ;; --refresh) refresh="true" shift ;; *) shift ;; esac done
__ncd_aws_apigateway_resolve_region "${region}"
if [[ -z "${api_key}" ]]; then read -e -p "API Key: " api_key fi assert_val "${api_key}" "missing param: --api-key"
local -r cache_file="${XDG_CACHE_HOME}/ncd/aws_apigateway_api-keys_${AWS_REGION}.json"
if [[ "${refresh}" = "true" ]]; then rm -f "${cache_file}" fi
local result="" if [[ "${refresh}" = "true" ]]; then rm -f "${cache_file}" elif [[ -f "${cache_file}" && -n "$(find "${cache_file}" -mmin -90)" ]]; then result="$(cat "${cache_file}")" fi
if [[ -z "${result}" ]]; then result="$(aws apigateway get-api-keys \ --include-values \ --output json)"
mkdir -p "$(dirname "${cache_file}")" echo "${result}" >"${cache_file}" fi
local key_id key_id="$(echo "${result}" | jq -r --arg api_key "${api_key}" '.items[] | select(.value == $api_key) | .id')" assert_val "${key_id}" "api key not found: ${api_key}"
echo -n "${key_id}"}
# ncd aws apigateway get-usage-planncd_aws_apigateway_get-usage-plan() { local region="" key_id="" api_key="" local refresh="false"
while (("$#")); do case "${1}" in --region) region="${2}" shift 2 ;; --key-id) key_id="${2}" shift 2 ;; --api-key) api_key="${2}" shift 2 ;; --refresh) refresh="true" shift ;; *) shift ;; esac done
__ncd_aws_apigateway_resolve_region "${region}"
key_id="$(__ncd_aws_apigateway_resolve_key_id "${key_id}" "${api_key}")" assert_val "${key_id}" "missing key-id"
local -r cache_file="${XDG_CACHE_HOME}/ncd/aws_apigateway_usage-plans_${AWS_REGION}_${key_id}.json"
if [[ "${refresh}" = "true" ]]; then rm -f "${cache_file}" fi
local result="" if [[ "${refresh}" = "true" ]]; then rm -f "${cache_file}" elif [[ -f "${cache_file}" && -n "$(find "${cache_file}" -mmin -30)" ]]; then result="$(cat "${cache_file}")" fi
if [[ -z "${result}" ]]; then result="$(aws apigateway get-usage-plans \ --key-id "${key_id}" \ --output json)"
mkdir -p "$(dirname "${cache_file}")" echo "${result}" >"${cache_file}" fi
local selected selected="$(echo "${result}" | jq -r '.items[] | .id + " " + .name' | fzf --header="Usage Plan" --select-1)" assert_val "${selected}" "no usage plan selected"
local usage_plan_id usage_plan_id="$(echo "${selected}" | awk '{ print $1 }')"
echo -n "${usage_plan_id}"}
# ncd aws apigateway list-usage-plansncd_aws_apigateway_list-usage-plans() { local region="" local refresh="false"
while (("$#")); do case "${1}" in --region) region="${2}" shift 2 ;; --refresh) refresh="true" shift ;; *) shift ;; esac done
__ncd_aws_apigateway_resolve_region "${region}"
local -r cache_file="${XDG_CACHE_HOME}/ncd/aws_apigateway_list-usage-plans_${AWS_REGION}.json"
if [[ "${refresh}" = "true" ]]; then rm -f "${cache_file}" fi
local result="" if [[ "${refresh}" = "true" ]]; then rm -f "${cache_file}" elif [[ -f "${cache_file}" && -n "$(find "${cache_file}" -mmin -30)" ]]; then result="$(cat "${cache_file}")" fi
if [[ -z "${result}" ]]; then result="$(aws apigateway get-usage-plans --output json)"
mkdir -p "$(dirname "${cache_file}")" echo "${result}" >"${cache_file}" fi
echo "${result}" | jq -r '.items[] | .id + " " + .name'}
# ncd aws apigateway view-usage-planncd_aws_apigateway_view-usage-plan() { local region="" usage_plan_id="" local -a list_args=()
while (("$#")); do case "${1}" in --region) region="${2}" shift 2 ;; --usage-plan-id) usage_plan_id="${2}" shift 2 ;; --refresh) list_args+=("--refresh") shift ;; *) shift ;; esac done
__ncd_aws_apigateway_resolve_region "${region}"
if [[ -z "${usage_plan_id}" ]]; then local selected selected="$(ncd aws:apigateway:list-usage-plans "${list_args[@]}" | fzf --header="Usage Plan")" assert_val "${selected}" "no usage plan selected" usage_plan_id="$(echo "${selected}" | awk '{ print $1 }')" else ncd aws:apigateway:list-usage-plans "${list_args[@]}" >/dev/null fi
assert_val "${usage_plan_id}" "missing usage-plan-id"
local -r cache_file="${XDG_CACHE_HOME}/ncd/aws_apigateway_list-usage-plans_${AWS_REGION}.json" cat "${cache_file}" | jq --arg id "${usage_plan_id}" '.items[] | select(.id == $id)'}
# ncd aws apigateway change-usage-planncd_aws_apigateway_change-usage-plan() { local region="" key_id="" api_key="" usage_plan_id=""
while (("$#")); do case "${1}" in --region) region="${2}" shift 2 ;; --key-id) key_id="${2}" shift 2 ;; --api-key) api_key="${2}" shift 2 ;; --usage-plan-id) usage_plan_id="${2}" shift 2 ;; *) shift ;; esac done
__ncd_aws_apigateway_resolve_region "${region}"
key_id="$(__ncd_aws_apigateway_resolve_key_id "${key_id}" "${api_key}")" assert_val "${key_id}" "missing key-id"
local current_plans current_plans="$(aws apigateway get-usage-plans --key-id "${key_id}" --output json)" local old_plan_id old_plan_name old_plan_id="$(echo "${current_plans}" | jq -r '.items[0].id // ""')" old_plan_name="$(echo "${current_plans}" | jq -r '.items[0].name // ""')"
if [[ -z "${usage_plan_id}" ]]; then local selected selected="$(ncd aws:apigateway:list-usage-plans | fzf --header="Select Usage Plan")" assert_val "${selected}" "no usage plan selected" usage_plan_id="$(echo "${selected}" | awk '{ print $1 }')" fi assert_val "${usage_plan_id}" "missing usage-plan-id"
if [[ "${old_plan_id}" = "${usage_plan_id}" ]]; then echo_err "Key already in usage plan ${old_plan_name} (${old_plan_id})" exit 0 fi
local new_plan_name new_plan_name="$(ncd aws:apigateway:list-usage-plans | grep "^${usage_plan_id} " | cut -d' ' -f2-)"
if [[ -n "${old_plan_id}" ]]; then local old_plan_data new_plan_data old_plan_data="$(ncd aws:apigateway:view-usage-plan --usage-plan-id "${old_plan_id}")" new_plan_data="$(ncd aws:apigateway:view-usage-plan --usage-plan-id "${usage_plan_id}")"
local old_file new_file old_file="$(mktemp)" new_file="$(mktemp)"
echo "${old_plan_data}" | jq -r ' [ "Name: " + .name, "ID: " + .id, "Description: " + (.description // "-"), "Quota Limit: " + (.quota.limit // "-" | tostring), "Quota Period: " + (.quota.period // "-"), "Throttle Burst: " + (.throttle.burstLimit // "-" | tostring), "Throttle Rate: " + (.throttle.rateLimit // "-" | tostring) ] | .[]' >"${old_file}"
echo "${new_plan_data}" | jq -r ' [ "Name: " + .name, "ID: " + .id, "Description: " + (.description // "-"), "Quota Limit: " + (.quota.limit // "-" | tostring), "Quota Period: " + (.quota.period // "-"), "Throttle Burst: " + (.throttle.burstLimit // "-" | tostring), "Throttle Rate: " + (.throttle.rateLimit // "-" | tostring) ] | .[]' >"${new_file}"
echo_err "" git diff --no-index --color-words --no-prefix \ --src-prefix="Current: " \ --dst-prefix="New: " \ "${old_file}" "${new_file}" >&2 || true echo_err ""
rm -f "${old_file}" "${new_file}" else echo_err "" echo_err "New Usage Plan:" ncd aws:apigateway:view-usage-plan --usage-plan-id "${usage_plan_id}" | jq -r ' [ " Name: " + .name, " ID: " + .id, " Description: " + (.description // "-"), " Quota Limit: " + (.quota.limit // "-" | tostring), " Quota Period: " + (.quota.period // "-"), " Throttle Burst: " + (.throttle.burstLimit // "-" | tostring), " Throttle Rate: " + (.throttle.rateLimit // "-" | tostring) ] | .[]' >&2 echo_err "" fi
read -p "Change usage plan from '${old_plan_name}' to '${new_plan_name}'? [yN] " -n 1 -r echo case ${REPLY} in y | Y) ;; *) exit 1 ;; esac
if [[ -n "${old_plan_id}" ]]; then aws apigateway delete-usage-plan-key \ --usage-plan-id "${old_plan_id}" \ --key-id "${key_id}" echo_err "Removed key from usage plan ${old_plan_name}" fi
aws apigateway create-usage-plan-key \ --usage-plan-id "${usage_plan_id}" \ --key-id "${key_id}" \ --key-type "API_KEY" echo_err "Added key to usage plan ${new_plan_name}"}
# ncd aws apigateway get-usagencd_aws_apigateway_get-usage() { local region="" usage_plan_id="" key_id="" start_date="" end_date="" api_key="" local refresh="false" local plot="false"
while (("$#")); do case "${1}" in --region) region="${2}" shift 2 ;; --usage-plan-id) usage_plan_id="${2}" shift 2 ;; --key-id) key_id="${2}" shift 2 ;; --api-key) api_key="${2}" shift 2 ;; --start-date) start_date="${2}" shift 2 ;; --end-date) end_date="${2}" shift 2 ;; --refresh) refresh="true" shift ;; --plot) plot="true" shift ;; *) shift ;; esac done
__ncd_aws_apigateway_resolve_region "${region}"
key_id="$(__ncd_aws_apigateway_resolve_key_id "${key_id}" "${api_key}")" assert_val "${key_id}" "missing key-id"
if [[ -z "${usage_plan_id}" ]]; then usage_plan_id="$(ncd aws:apigateway:get-usage-plan --key-id "${key_id}")" fi assert_val "${usage_plan_id}" "missing usage-plan-id"
start_date="${start_date:-$(date +%Y-%m-01)}" end_date="${end_date:-$(date +%Y-%m-%d)}"
local -r cache_file="${XDG_CACHE_HOME}/ncd/aws_apigateway_get-usage_${usage_plan_id}_${key_id}_${start_date}_${end_date}.json"
local result="" if [[ "${refresh}" = "true" ]]; then rm -f "${cache_file}" elif [[ -f "${cache_file}" && -n "$(find "${cache_file}" -mmin -30)" ]]; then result="$(cat "${cache_file}")" fi
if [[ -z "${result}" ]]; then result="$(aws apigateway get-usage \ --usage-plan-id "${usage_plan_id}" \ --key-id "${key_id}" \ --start-date "${start_date}" \ --end-date "${end_date}")"
mkdir -p "$(dirname "${cache_file}")" echo "${result}" >"${cache_file}" fi
if [[ "${plot}" = "true" ]]; then local max_used max_used="$(echo "${result}" | jq -r '.items | to_entries[0].value | map(.[0]) | max')"
echo "${result}" | jq -r ' (.startDate | split("-")[2] | tonumber) as $startDay | (["Day", "Used", "Remaining"]), (.items | to_entries[0].value | to_entries[] | [($startDay + .key), .value[0], .value[1]] ) | @tsv ' | uplot lineplots \ -H --fmt xyy \ -t "API Plan Usage" \ -w 80 \ -h 20 \ --grid \ --ylim "0,${max_used}" else echo "${result}" fi}
# ncd aws:apigateway:update-usagencd_aws_apigateway_update-usage() { local region="" usage_plan_id="" key_id="" api_key="" local refresh="false" local -a args=()
while (("$#")); do case "${1}" in --region) region="${2}" shift 2 ;; --usage-plan-id) usage_plan_id="${2}" shift 2 ;; --key-id) key_id="${2}" shift 2 ;; --api-key) api_key="${2}" shift 2 ;; --refresh) refresh="true" shift ;; *) args+=("${1}") shift ;; esac done
__ncd_aws_apigateway_resolve_region "${region}"
key_id="$(__ncd_aws_apigateway_resolve_key_id "${key_id}" "${api_key}")" assert_val "${key_id}" "missing key-id"
if [[ -z "${usage_plan_id}" ]]; then usage_plan_id="$(ncd aws:apigateway:get-usage-plan --key-id "${key_id}")" fi assert_val "${usage_plan_id}" "missing usage-plan-id"
local -a get_usage_args=("--usage-plan-id" "${usage_plan_id}" "--key-id" "${key_id}") [[ "${refresh}" = "true" ]] && get_usage_args+=("--refresh")
local current_usage current_usage="$(ncd aws:apigateway:get-usage "${get_usage_args[@]}")" local current_value current_value="$(echo "${current_usage}" | jq -r ".items | to_entries[] | .value | last | .[1]")"
echo_err "Current Remaining: ${current_value}"
local value="${args[1]:-}" if [[ -z "${value}" ]]; then read -e -p "Replace Remaining: " value fi assert_val "${value}" "missing value"
read -p "Update to ${value}? [yN] " -n 1 -r case ${REPLY} in y | Y) ;; *) exit 1 ;; esac
# https://docs.aws.amazon.com/apigateway/latest/api/patch-operations.html#UpdateUsage-Patch aws apigateway update-usage \ --usage-plan-id "${usage_plan_id}" \ --key-id "${key_id}" \ --patch-operations "op=replace,path=/remaining,value=${value}"}
# ncd aws:codeartifact-auth-tokenncd_aws_codeartifact-auth-token() { if [[ ! -e "${XDG_CACHE_HOME}/newscred_aws_codeartifact_off" && ! (-f "${AWS_CODEARTIFACT_TOKEN_CACHE_FILE}" && -n "$(find "${AWS_CODEARTIFACT_TOKEN_CACHE_FILE}" -mmin -$((10 * 60)))") ]]; then if aws sts get-caller-identity --query "Account" >/dev/null 2>&1; then AWS_CODEARTIFACT_AUTH_TOKEN="$(aws codeartifact get-authorization-token \ --duration-seconds "$((10 * 60 * 60))" \ --domain "$(ncd config aws.domain)" \ --domain-owner "$(ncd config aws.domain_owner)" \ --query authorizationToken \ --output text)"
if test -n "${AWS_CODEARTIFACT_AUTH_TOKEN}"; then echo -n "${AWS_CODEARTIFACT_AUTH_TOKEN}" >"${AWS_CODEARTIFACT_TOKEN_CACHE_FILE}" fi fi fi
cat "${AWS_CODEARTIFACT_TOKEN_CACHE_FILE}"}
# ncd aws apigatewayncd_aws_apigateway() { ncd_aws_login
__ncd_cmd "aws apigateway" "$@"}
# ncd aws loginncd_aws_login() { if ! aws sts get-caller-identity --query "Account" >/dev/null 2>&1; then aws sso login fi}
# ncd awsncd_aws() { __ncd_cmd "aws" "$@"}
ncd_on_aws() { echo_err "Checking AWS Login Status..."
ncd aws login
rm -f "${XDG_CACHE_HOME}/newscred_aws_codeartifact_off"
ncd aws:codeartifact-auth-token >/dev/null}
ncd_off_aws() { echo_err "Turning Off AWS CodeArtifact Token Check..."
touch "${XDG_CACHE_HOME}/newscred_aws_codeartifact_off"}
ncd_on_docker() { echo "Checking Docker..."
osascript >/dev/null <<EOL if not (application "Docker" is running) then tell application "Docker" to activate end ifEOL}
ncd_off_docker() { echo "Closing Docker Desktop..."
osascript >/dev/null <<EOL if application "Docker Desktop" is running then tell application "System Events" to tell process "Docker Desktop" tell menu bar item 1 of menu bar 2 click tell menu 1 click menu item "Quit Docker Desktop" end tell end tell end tell end ifEOL}
ncd_on_vpn() { echo "Checking VPN Connection..."
local -r openvpn_connect="/Applications/OpenVPN Connect/OpenVPN Connect.app/contents/MacOS/OpenVPN Connect" local -r profile_name="vpn.cmp.optimizely.com"
osascript >/dev/null <<EOL if not (application "OpenVPN Connect" is running) then do shell script "'${openvpn_connect}' --minimize --skip-startup-dialogs" end if
tell application "System Events" to tell process "OpenVPN Connect" repeat until menu bar item 1 of menu bar 2 exists delay 0.1 end repeat
tell menu bar item 1 of menu bar 2 click tell menu 1 click if (menu item "Connect" exists) or not (menu item "${profile_name}" exists) then tell menu item 3 click click menu item "${profile_name}" of menu 1 end tell else key code 53 -- <esc> close menu end if end tell end tell end tellEOL}
ncd_off_vpn() { echo "Disconnecting VPN..."
osascript >/dev/null <<EOL if application "OpenVPN Connect" is running then tell application "System Events" to tell process "OpenVPN Connect" tell menu bar item 1 of menu bar 2 click tell menu 1 click if menu item "Disconnect" exists then click menu item "Disconnect" else key code 53 -- <esc> close menu end if end tell end tell end tell end ifEOL}
ncd_off_repo-window() { __ncd_untag_current_repo_window}
# ncd onncd_on() { ncd_on_vpn ncd_on_aws ncd_on_docker}
# ncd offncd_off() { ncd_off_aws ncd_off_vpn ncd_off_docker ncd_off_repo-window if __ncd_is_inside_workspace_repo; then cd "${HOME}" fi}
__ncd_dc_list_commands() { ncd_dc --help | sed '1,/Commands:/d' | sed '/^$/,$d' | sed 's/^ //' | cut -d' ' -f1}
# ncd dcncd_dc() { __pyenv_init __pyenv_virtualenv_activate localdev
local cmd="${1:-}" shift || true
if test -z "${cmd}"; then cmd="$(__ncd_dc_list_commands | fzf --header=Command)" fi
local args=() local containers=()
case "${cmd}" in build | up | logs | restart | shell) while (("$#")); do case "${1}" in *) if __ncd_is_container "${1}"; then containers+=("${1}") else args+=("${1}") fi shift ;; esac done if test "${#containers[@]}" -eq 0; then containers+=("$(__ncd_current_repo)") fi ;; *) while (("$#")); do args+=("${1}") shift done ;; esac
case "${cmd}" in logs) if test "${#args[@]}" -eq 0; then args+=("-f" "--tail" "1000") fi if test "${#containers[@]}" -eq 1; then args+=("--no-log-prefix") fi ;; esac
case "${cmd}" in build | up | logs | restart | shell) exec nc-docker ${cmd} "${args[@]}" "${containers[@]}" ;; *) exec nc-docker ${cmd} "${args[@]}" ;; esac}
# ncd list containerncd_list_container() { assert_command rq cat "$(ncd workspace)/docker-compose.yml" | rq --input-yaml | jq -r '.services | keys | .[]'}
# ncd list reponcd_list_repo() { find "$(ncd workspace)" -maxdepth 2 -type d -name '.git' | xargs dirname | xargs -n1 basename}
# ncd list repo-windowncd_list_repo-window() { local -a windows=()
while IFS= read -r window; do if test -n "${window}"; then local window_id="$(echo "${window}" | awk '{ print $2 }')" local tag="$(tmux show-option -w -t "${window_id}" -qv '@ncd_repo_window')" if [[ -n "${tag}" ]]; then windows+=("${tag}") fi fi done <<<"$(tmux list-windows -F '#{window_active} #{window_id} #W')"
if ((${#windows[@]})); then printf '%s\n' "${windows[@]}" fi}
# ncd listncd_list() { __ncd_cmd "list" "$@"}
__ncd_untag_current_repo_window() { if test -n "$(tmux show-option -w -qv '@ncd_repo_window')"; then tmux set-option -w -u '@ncd_repo_window' tmux set-option -w automatic-rename on fi}
# ncd repo [repo-name]ncd_repo() { local repo="${1:-}" [[ -n "${repo}" ]] && shift
if [[ -z "${repo}" ]]; then if __ncd_is_inside_workspace_repo; then # already inside workspace repo local -r curr_tag="$(tmux show-option -w -qv '@ncd_repo_window')" local -r curr_repo="$(__ncd_current_repo)" if [[ -z "${curr_tag}" ]] || [[ "${curr_tag}" != "${curr_repo}" ]]; then # current window is untagged or tagged for a different repo __ncd_untag_current_repo_window repo="${curr_repo}" fi fi fi
if [[ -z "${repo}" ]]; then repo="$(ncd list:repo-window | fzf --exit-0)" || true fi
if [[ -z "${repo}" ]]; then repo="$(ncd list:repo | fzf)" || true fi
assert_val "${repo}" "missing repo" assert_dir "$(ncd workspace)/${repo}" "invalid repo"
# if already tagged, jump to window while IFS= read -r window; do if test -n "${window}"; then local window_id="$(echo "${window}" | awk '{ print $2 }')" if [[ "${repo}" = "$(tmux show-option -w -t "${window_id}" -qv '@ncd_repo_window')" ]]; then tmux select-window -t "${window_id}" exit 0 fi fi done <<<"$(tmux list-windows -F '#{window_active} #{window_id} #W')"
local -r repo_path="$(ncd workspace)/${repo}" local -r window_name=":${repo}"
if [[ -z "$(tmux show-option -w -qv '@ncd_repo_window')" ]] && __ncd_is_inside_workspace_repo; then # current window is untagged and already inside workspace rpeo if [[ "${PWD}" != "${repo_path}" ]]; then tmux send-keys "cd ${repo_path} && clear" Enter fi tmux rename-window "${window_name}" tmux set-option -w '@ncd_repo_window' "${repo}" else # current window is already tagged or outside workspace local window_id="$(tmux new-window -n "${window_name}" -c "${repo_path}" -P -F '#{window_id}')" tmux set-option -w -t "${window_id}" '@ncd_repo_window' "${repo}" fi}
ncd_k8s_namespaces() { kubectl get namespaces --output=json | jq -r '.items[].metadata.name'}
ncd_k8s_pods() { local -r namespace="${1:-$(ncd k8s namespaces | fzf --header=Namespace)}" kubectl get pods --namespace="${namespace}" --output=json | jq -r '.items[].metadata.name'}
ncd_k8s_containers() { local -r namespace="${1:-$(ncd k8s namespaces | fzf --header=Namespace)}" local -r pod="${2:-$(ncd k8s pods "${namespace}" | fzf --header=Pod)}" kubectl get pods --namespace="${namespace}" --output=json | jq -r ".items[] | select(.metadata.name == \"${pod}\") | .spec.containers[].name"}
__k8s_select_resources() { local namespace="" pod="" container="" local pods_blob
while (("$#")); do case "${1}" in --namespace) namespace="$(kubectl get namespaces --output=json | jq -r '.items[].metadata.name' | fzf --header=Namespace -q "${NCD_K8S_NAMESPACE:-}" -1)" assert_val "${namespace}" "missing namespace"
export NCD_K8S_NAMESPACE="${namespace}" shift ;; --pod) assert_val "${namespace}" "namespace needs to be selected first"
pods_blob="$(kubectl get pods --namespace="${namespace}" --output=json)"
pod="$(printf "%s" "${pods_blob}" | jq -r '.items[].metadata.name' | fzf --header=Pod -q "${NCD_K8S_POD}" -1)" assert_val "${pod}" "missing pod"
export NCD_K8S_POD="${pod}" shift ;; --container) assert_val "${pod}" "pod needs to be selected first"
container="$(printf "%s" "${pods_blob}" | jq -r ".items[] | select(.metadata.name == \"${pod}\") | .spec.containers[].name" | fzf --header=Container -q "${NCD_K8S_CONTAINER}" -1)" assert_val "${container}" "missing container"
export NCD_K8S_CONTAINER="${container}" shift ;; *) shift ;; esac done}
ncd_k8s_cp() { __k8s_select_resources --namespace --pod --container
local src="${1}" dest="${2}"
if [[ "${src:0:1}" = ":" ]]; then kubectl cp "${NCD_K8S_POD}${src}" --namespace "${NCD_K8S_NAMESPACE}" "${dist}" -c "${NCD_K8S_CONTAINER}" elif [[ "${dest:0:1}" = ":" ]]; then kubectl cp "${src}" --namespace "${NCD_K8S_NAMESPACE}" "${NCD_K8S_POD}${dist}" -c "${NCD_K8S_CONTAINER}" else exit 1 fi}
ncd_k8s_exec() { __k8s_select_resources --namespace --pod --container
local cmd="${NCD_K8S_EXEC_CMD:-}" if test -z "${cmd}"; then read -e -p "Command: " -i "/bin/bash" cmd else cmd="/bin/bash -c '${cmd}'" fi assert_val "${cmd}" "missing command"
kubectl exec --namespace "${NCD_K8S_NAMESPACE}" "${NCD_K8S_POD}" -c "${NCD_K8S_CONTAINER}" -it -- ${cmd}}
ncd_k8s_logs() { __k8s_select_resources --namespace --pod --container
kubectl logs --namespace "${NCD_K8S_NAMESPACE}" "${NCD_K8S_POD}" -c "${NCD_K8S_CONTAINER}" "$@"}
ncd_k8s_create-token() { local service_account="${1:-$(kubectl get serviceAccounts -o json | jq -r '.items[].metadata.name' | fzf)}"
assert_val "${service_account}" "missing param: <service_account>"
kubectl create token "${service_account}"}
ncd_k8s_copy-token() { ncd k8s create-token "$@" | copy-to-clipboard}
ncd_k8s_add-context() { local -r cluster_name="${1:-$(aws eks list-clusters --output=json | jq -r '.clusters[]' | fzf)}" assert_val "${cluster_name}" "missing cluster name"
local -r region="us-east-1"
aws eks --region "${region}" update-kubeconfig --name "${cluster_name}" --alias "${cluster_name}"}
ncd_k8s_current-context() { kubectl config current-context}
# ncd k8sncd_k8s() { assert_command kubectl
export NCD_K8S_NAMESPACE=${NCD_K8S_NAMESPACE:-} NCD_K8S_POD=${NCD_K8S_POD:-} NCD_K8S_CONTAINER=${NCD_K8S_CONTAINER:-} __ncd_cmd "k8s" "$@"}
# ncd es aqsncd_es_aqs() { local endpoint="${1}" shift
local args=() for arg in "$@"; do if echo "${arg}" | grep -s "\s"; then args+=("'${arg//\'/\\\'}'") else args+=("${arg}") fi done
cat <<EOF >/tmp/ncd_es_aqs_script#!/bin/bash
rm -f /tmp/ncd_es_aqs_script
if ! type curl >/dev/null 2>&1; then apt-get -qq update >/dev/null 2>&1 apt-get -qq install curl >/dev/null 2>&1fi
export ES_HOST=\$(cat /opt/article-query/conf/article-query.json | grep OPENSEARCH_2_HOST | cut -d'"' -f4)
curl "http://\${ES_HOST}/${endpoint}" ${args[@]}EOF
chmod +x /tmp/ncd_es_aqs_script
export NCD_K8S_NAMESPACE=article-query NCD_K8S_POD=article-query- NCD_K8S_CONTAINER=article-query
ncd k8s cp /tmp/ncd_es_aqs_script :/tmp/ncd_es_aqs_script
NCD_K8S_EXEC_CMD="/tmp/ncd_es_aqs_script" ncd k8s exec}
# ncd esncd_es() { __ncd_cmd "es" "$@"}
# ncd git pr-syncncd_git_pr-sync() { assert_command gh
local -r pr_link="${1:-}" assert_val "${pr_link}" "missing pr link" shift
if ! [[ "${pr_link}" =~ ^https://github.com/newscred/cmp-client/pull/.* ]]; then echo "invalid pr link: ${pr_link}" exit 1 fi
local -r pr_number="${pr_link##*/}" local -r sync_pr_branch="sync-pr-${pr_number}" local -r current_branch="$(git branch --show-current)"
local -r pr_blob="$(gh pr view "${pr_link}" --json mergedAt,number,title,body)"
if [[ "${current_branch}" != "${sync_pr_branch}" ]]; then local commits_blob
if [[ "$(echo "${pr_blob}" | jq -r '.mergedAt')" = "null" ]]; then read -p "PR not merged yet. Are you sure to continue? [yN] " -n 1 -r case ${REPLY} in y | Y) echo ;; *) exit 1 ;; esac
commits_blob="$(gh api graphql --paginate -f query=' query { repository(owner: "newscred", name:"cmp-client") { pullRequest(number: '${pr_number}') { commits(first: 100) { nodes { commit { oid message } } } } } } ' --jq ".data.repository.pullRequest.commits.nodes[] | map({ commit: .oid, message: .message })")" else local -r merged_at="$(echo "${pr_blob}" | jq -r '.mergedAt')" local -r since="$(date --date="${merged_at} -30 minutes" -u +"%Y-%m-%dT%H:%M:%SZ")" local -r until="$(date --date="${merged_at} +90 minutes" -u +"%Y-%m-%dT%H:%M:%SZ")"
local -r jq_query=".data.repository.defaultBranchRef.target.history.nodes | map(select(.associatedPullRequests.nodes[0].number == ${pr_number})) | reverse | map({ commit: .oid, message: .message })" commits_blob="$(gh api graphql --paginate -f query=' query ($since: GitTimestamp!, $until: GitTimestamp!, $endCursor: String) { repository(name: "cmp-client", owner: "newscred") { defaultBranchRef { name target { ... on Commit { id history(first: 100, after: $endCursor, since: $since, until: $until) { nodes { message oid associatedPullRequests(first: 1) { nodes { title permalink number } } } pageInfo { endCursor hasNextPage } } } } } } }' -F since="${since}" -F until="${until}" --jq "${jq_query}")" fi
if [[ -z "${commits_blob}" ]] || [[ "${commits_blob}" = "[]" ]]; then echo "Failed to extract commits" exit 1 fi
echo "Switching to cmp-client:master" git checkout master echo echo "Fetching latest commits..." ncd git sync echo
echo "Switching to cmp-client-stable:master" git checkout stable-master echo echo "Fetching latest commits..." ncd git sync echo
echo "Creating branch: ${sync_pr_branch}" git checkout -B "${sync_pr_branch}" echo
echo "Cherry-Picking Commits:" echo "${commits_blob}" | jq -r '.[] | "* " + .commit[0:10] + " " + .message' echo git cherry-pick $(echo "${commits_blob}" | jq -r '.[] | .commit' | xargs) echo fi
echo "Pushing branch: ${sync_pr_branch}" git push -f -u stable-origin "${sync_pr_branch}"
echo "Creating PR..." local -r sync_pr_title="sync(#${pr_number}) $(echo "${pr_blob}" | jq -r '.title')" local -r ticket_link="$(echo "${pr_blob}" | jq -r '.body' | grep "TICKET:" | cut -d' ' -f2)" local -r sync_pr_body="$(cat .github/PULL_REQUEST_TEMPLATE.md | sed -e "s/^TICKET:.*$/TICKET: $(echo ${ticket_link} | sed -e 's/\//\\\//g')/" | sed -e ":a;N;\$!ba;s/\(Related PRs:[^-]*\)-[^\n]*/\1- Sync $(echo ${pr_link} | sed -e 's/\//\\\//g')/")" gh --repo newscred/cmp-client-stable pr create --title "${sync_pr_title}" --body "${sync_pr_body}" echo}
ncd_git_default-branch() { for input in "newscred/HEAD" "origin/HEAD"; do local output="" output="$(git rev-parse --abbrev-ref "${input}" 2>/dev/null || true)" if test -z "${output}" || test "${output}" = "${input}"; then git fetch "${input%/*}" --quiet local output="$(git remote set-head "${input%/*}" --auto 2>/dev/null || true)" if test -n "${output}"; then echo "${output}" | cut -d" " -f4 return fi else echo "${output#*/}" return fi done}
# ncd git syncncd_git_sync() { local current_branch current_branch="$(git branch --show-current)"
case "${current_branch}" in master | main) git pull newscred "${current_branch}" git push ;; stable-master) git fetch stable-newscred master git reset --hard stable-newscred/master git push -f -u stable-origin stable-master:master ;; *) ncd git rebase --sync ;; esac}
# ncd git rebasencd_git_rebase() { local current_branch current_branch="$(git branch --show-current)" local -r default_branch="$(ncd_git_default-branch)" if test -z "${default_branch}"; then exit 1 fi
local base_branch="${default_branch}" if [[ "${current_branch}" = sync-* ]]; then base_branch="stable-master" fi
local do_sync="false" while (("$#")); do case "$1" in --sync) do_sync="true" shift ;; *) shift ;; esac done
case "${current_branch}" in main | master | stable-master) echo "current on default branch: ${current_branch}" ;; *) if [[ "${do_sync}" = "true" ]]; then git checkout "${base_branch}" ncd git sync git checkout "${current_branch}" fi git rebase -i ${base_branch} ;; esac}
# ncd git prepare-remotencd_git_prepare-remote() { local -r curr_repo="$(basename "$(git rev-parse --show-toplevel)")"
local -A remote_url_map remote_url_map["newscred"]="git@github.com:newscred/${curr_repo}.git" remote_url_map["origin"]="git@github.com:${GITHUB_USERNAME}/${curr_repo}.git" if [[ "${curr_repo}" = "cmp-client" ]]; then remote_url_map["stable-newscred"]="git@github.com:newscred/${curr_repo}-stable.git" remote_url_map["stable-origin"]="git@github.com:${GITHUB_USERNAME}/${curr_repo}-stable.git" fi
local remote_url for remote in ${!remote_url_map[@]}; do remote_url="$(git remote get-url "${remote}" 2>/dev/null || true)" if test -z "${remote_url}"; then git remote add "${remote}" "${remote_url_map["${remote}"]}" elif test "${remote_url}" != "${remote_url_map["${remote}"]}"; then git remote set-url "${remote}" "${remote_url_map["${remote}"]}" fi printf "%15s %s\n" "${remote}" "$(git remote get-url "${remote}")" done}
# ncd gitncd_git() { __ncd_assert_inside_workspace_repo __ncd_cmd "git" "$@"}
# ncd py venv-initncd_py_venv-init() { if ! __ncd_is_inside_workspace_repo; then echo "not inside workspace repo" exit 1 fi
local venv_name="$(__ncd_py_venv_name)"
AUTOENV_FILE_ENTER="${AUTOENV_FILE_ENTER:-.autoenv.in.zsh}" if ! [[ -f "${AUTOENV_FILE_ENTER}" ]]; then touch "${AUTOENV_FILE_ENTER}" fi if ! cat "${AUTOENV_FILE_ENTER}" | grep -q "^export PYENV_VERSION"; then echo "export PYENV_VERSION=\"${venv_name}\"" >>"${AUTOENV_FILE_ENTER}" fi source "${AUTOENV_FILE_ENTER}"
AUTOENV_FILE_LEAVE="${AUTOENV_FILE_LEAVE:-.autoenv.out.zsh}" if ! [[ -f "${AUTOENV_FILE_LEAVE}" ]]; then touch "${AUTOENV_FILE_LEAVE}" fi if ! cat "${AUTOENV_FILE_LEAVE}" | grep -q "^unset PYENV_VERSION"; then echo "unset PYENV_VERSION" >>"${AUTOENV_FILE_LEAVE}" fi
__pyenv_init
if ! __pyenv_virtualenv_exists "${venv_name}"; then local python_version="" if [[ -f .python-version ]]; then python_version="$(cat .python-version | fzf --select-1)" fi python_version="$(__pyenv_list_versions | fzf --query "${python_version}" --select-1)" || true
read -e -p "(${venv_name}) Python Version: " -i "${python_version}" python_version if [[ -z "${python_version}" ]]; then exit 1 fi
if ! __pyenv_is_version_installed "${python_version}"; then pyenv install "${python_version}" fi
pyenv virtualenv "${python_version}" "${venv_name}" fi}
ncd_py_venv-remove() { if ! __ncd_is_inside_workspace_repo; then echo "not inside workspace repo" exit 1 fi
local venv_name="$(__ncd_py_venv_name)"
if __pyenv_virtualenv_exists "${venv_name}"; then pyenv virtualenv-delete "${venv_name}" fi}
__py_list_requirements() { local -r filename="${1}" cat "${filename}" | sed 's/#.*//' | grep -v "^$" | grep -v "^#" | grep "^.*==.*"}
__py_install_requirements() { local -r filename="${1}" if command_exists parallel; then __py_list_requirements "${filename}" | parallel pip install {} else __py_list_requirements "${filename}" | xargs -n1 pip install fi true}
# ncd py try-installncd_py_try-install() { __pyenv_init __ncd_assert_py_repo_venv
if [[ -f requirements-common.txt ]]; then __py_install_requirements requirements-common.txt fi
if [[ -f requirements-dev.txt ]]; then __py_install_requirements requirements-dev.txt fi
if [[ -f requirements-explicit.txt ]] && [[ -f requirements.txt ]]; then __py_install_requirements requirements.txt elif [[ -f requirements.in ]] && [[ -f requirements.txt ]]; then __py_install_requirements requirements.txt fi}
# ncd pyncd_py() { assert_command pip assert_command pyenv
__ncd_assert_inside_workspace_repo __ncd_cmd "py" "$@"}
ncd() { local -r cmd="${1:-}" assert_val "${cmd}" "missing command" shift
local -r func="ncd_${cmd//:/_}" assert_func "${func}" "invalid command: ${cmd}"
$func "$@"}
assert_command awsassert_command fzfassert_command jq
ncd "$@"