diff --git a/src/core/log.sh b/src/core/log.sh index 4d6d868..dee3383 100644 --- a/src/core/log.sh +++ b/src/core/log.sh @@ -12,19 +12,19 @@ clr () { lower () { local s="${1:-}" - printf '%s' "${s}" | tr '[:upper:]' '[:lower:]' + printf '%s' "${s,,}" } upper () { local s="${1:-}" - printf '%s' "${s}" | tr '[:lower:]' '[:upper:]' + printf '%s' "${s^^}" } cap () { local s="${1:-}" - printf '%s%s' "$(printf '%s' "${s:0:1}" | tr '[:lower:]' '[:upper:]')" "${s:1}" + printf '%s' "${s^}" } @@ -40,39 +40,36 @@ log () { return 0 } -info () { +tint () { + + local code="${1:-}" tag="${2:-}" + shift 2 >/dev/null 2>&1 || true - if clr; then printf '\033[96m[*]\033[0m %b\n' "$*" >&2 - else printf '[*] %b\n' "$*" >&2 + if clr; then printf '\033[%sm%s\033[0m %b\n' "${code}" "${tag}" "$*" >&2 + else printf '%s %b\n' "${tag}" "$*" >&2 fi return 0 } -succ () { +info () { - if clr; then printf '\033[32m[+]\033[0m %b\n' "$*" >&2 - else printf '[+] %b\n' "$*" >&2 - fi + tint 96 '[*]' "$*" - return 0 +} +succ () { + + tint 32 '[+]' "$*" } warn () { - if clr; then printf '\033[33m[!]\033[0m %b\n' "$*" >&2 - else printf '[!] %b\n' "$*" >&2 - fi - - return 0 + tint 33 '[!]' "$*" } err () { - if clr; then printf '\033[31m[-]\033[0m %b\n' "$*" >&2 - else printf '[-] %b\n' "$*" >&2 - fi - + tint 31 '[-]' "$*" return 1 } diff --git a/src/core/sys.sh b/src/core/sys.sh index 11e1e3d..e9f7154 100644 --- a/src/core/sys.sh +++ b/src/core/sys.sh @@ -1,9 +1,9 @@ has () { - [[ -n "${1:-}" ]] || { err "Missing binary name"; return; } + [[ -n "${1:-}" ]] || return 1 - command -v "${1}" >/dev/null 2>&1 + command -v -- "${1}" >/dev/null 2>&1 } need () { diff --git a/src/mod/github/base.sh b/src/mod/github/base.sh index a9cf209..518a1b9 100644 --- a/src/mod/github/base.sh +++ b/src/mod/github/base.sh @@ -23,9 +23,10 @@ repo () { [[ "${name}" == */* ]] && { out "${name}"; return; } - login="$(gh api user -q .login 2>/dev/null)" || { err "Failed to detect Github user"; return; } + login="$(gh api user -q .login 2>/dev/null)" \ + || { err "Failed to detect GitHub user"; return; } - [[ -n "${login}" ]] || { err "Failed to detect Github user"; return; } + [[ -n "${login}" ]] || { err "Failed to detect GitHub user"; return; } out "${login}/${name}" @@ -93,10 +94,9 @@ diffs () { need git || return isrepo || { err "Not a git repository"; return; } - local target="${1:-}" mode="${2:-}" + local target="${1:-}" shift >/dev/null 2>&1 || true - [[ "${mode}" == --* ]] && { shift >/dev/null 2>&1 || true; } case "${target}" in ""|work|working|unstaged) git diff "$@" ;; @@ -120,7 +120,7 @@ owner () { name="$(repo "${name}")" || return owner="${name%%/*}" else - owner="$(gh api user -q .login 2>/dev/null)" || { err "Failed to detect GitHub user"; return; } + owner="$(gh api user -q .login 2>/dev/null)" || { err "Failed to detect GitHub owner"; return; } fi [[ -n "${owner}" ]] || owner="${GITHUB_OWNER:-}" diff --git a/src/mod/github/branch.sh b/src/mod/github/branch.sh index 9f7f4f4..668ce2a 100644 --- a/src/mod/github/branch.sh +++ b/src/mod/github/branch.sh @@ -67,7 +67,7 @@ del-branch () { need git || return - local name="${1:-}" force="${2:-0}" current="" + local name="${1:-}" force="${2:-0}" current="" local_ok=0 remote_ok=0 shift >/dev/null 2>&1 || true [[ $# -gt 0 ]] && { shift >/dev/null 2>&1 || true; } @@ -77,11 +77,13 @@ del-branch () { [[ "${name}" != "${current}" ]] || { err "Cannot delete current branch: ${name}"; return; } - if bool "${force}" force f; then git branch -D "${name}" >/dev/null 2>&1 || true - else git branch -d "${name}" >/dev/null 2>&1 || true + if bool "${force}" force f; then git branch -D "${name}" >/dev/null 2>&1 && local_ok=1 + else git branch -d "${name}" >/dev/null 2>&1 && local_ok=1 fi - git push origin --delete "${name}" "$@" >/dev/null 2>&1 || true + git push origin --delete "${name}" "$@" >/dev/null 2>&1 && remote_ok=1 + + (( local_ok || remote_ok )) || { err "Branch not found: ${name}"; return; } succ "Branch deleted -> ${name}" @@ -90,9 +92,7 @@ del-branch () { current-branch () { need git || return - - git branch --show-current "$@" 2>/dev/null \ - || { err "Failed to detect current branch"; return; } + git branch --show-current "$@" 2>/dev/null || { err "Failed to detect current branch"; return; } } default-branch () { diff --git a/src/mod/github/cache.sh b/src/mod/github/cache.sh index 7a78870..b008f35 100644 --- a/src/mod/github/cache.sh +++ b/src/mod/github/cache.sh @@ -25,15 +25,15 @@ clear-cache () { need gh || return - local id="" out="" + local id="" caches="" local -a ids=() - out="$(gh cache list --limit 1000 --json id -q '.[].id' "$@")" \ + caches="$(gh cache list --limit 1000 --json id -q '.[].id' "$@")" \ || { err "Failed to list caches"; return; } - [[ -n "${out}" ]] || { warn "No caches found"; return; } + [[ -n "${caches}" ]] || { warn "No caches found"; return; } - mapfile -t ids <<< "${out}" + mapfile -t ids <<< "${caches}" for id in "${ids[@]}"; do diff --git a/src/mod/github/gist.sh b/src/mod/github/gist.sh index a126670..eac7e51 100644 --- a/src/mod/github/gist.sh +++ b/src/mod/github/gist.sh @@ -13,22 +13,22 @@ gists () { local limit="100" if [[ "${1:-}" =~ ^[0-9]+$ ]]; then + limit="${1}" shift + fi - gh gist list --limit "${limit}" "$@" | sed '/^[[:space:]]*$/d' | wc -l | tr -d ' ' + gh gist list --limit "${limit}" --json id -q 'length' "$@" } - gist-new () { need gh || return [[ $# -gt 0 ]] || { err "Missing gist file or content"; return; } - gh gist create "$@" \ - || { err "Failed to create gist"; return; } + gh gist create "$@" || { err "Failed to create gist"; return; } } gist-open () { diff --git a/src/mod/github/info.sh b/src/mod/github/info.sh index ed2df15..b000ad9 100644 --- a/src/mod/github/info.sh +++ b/src/mod/github/info.sh @@ -3,16 +3,14 @@ repo-info () { need gh || return - local name="${1:-}" + local name="${1:-}" fields="" shift >/dev/null 2>&1 || true name="$(repo "${name}")" || return - gh repo view "${name}" \ - --json \ - nameWithOwner,description,url,visibility,isPrivate,isFork,isArchived,stargazerCount,forkCount,\ - openGraphImageUrl,createdAt,updatedAt,pushedAt,defaultBranchRef,licenseInfo \ - "$@" 2>/dev/null \ + fields="nameWithOwner,description,url,visibility,isPrivate,isFork,isArchived,stargazerCount,forkCount,openGraphImageUrl,createdAt,updatedAt,pushedAt,defaultBranchRef,licenseInfo" + + gh repo view "${name}" --json "${fields}" "$@" 2>/dev/null \ || { err "Failed to get repository info: ${name}"; return; } } @@ -20,17 +18,14 @@ repo-health () { need gh || return - local name="${1:-}" + local name="${1:-}" fields="" shift >/dev/null 2>&1 || true name="$(repo "${name}")" || return - gh repo view "${name}" \ - --json \ - nameWithOwner,description,url,visibility,isPrivate,isFork,\ - isArchived,stargazerCount,forkCount,\ - createdAt,updatedAt,pushedAt,defaultBranchRef,licenseInfo \ - "$@" 2>/dev/null \ + fields="nameWithOwner,description,url,visibility,isPrivate,isFork,isArchived,stargazerCount,forkCount,createdAt,updatedAt,pushedAt,defaultBranchRef,licenseInfo" + + gh repo view "${name}" --json "${fields}" "$@" 2>/dev/null \ || { err "Failed to get repository health: ${name}"; return; } } @@ -222,18 +217,18 @@ discs () { } collabs () { - local out="" + local collab_items="" - out="$(collab-list "$@")" || return - printf '%s\n' "${out}" | sed '/^[[:space:]]*$/d' | wc -l | tr -d ' ' + collab_items="$(collab-list "$@")" || return + printf '%s\n' "${collab_items}" | sed '/^[[:space:]]*$/d' | wc -l | tr -d ' ' } contribs () { - local out="" + local contrib_items="" - out="$(contrib-list "$@")" || return - printf '%s\n' "${out}" | sed '/^[[:space:]]*$/d' | wc -l | tr -d ' ' + contrib_items="$(contrib-list "$@")" || return + printf '%s\n' "${contrib_items}" | sed '/^[[:space:]]*$/d' | wc -l | tr -d ' ' } languages () { @@ -243,12 +238,12 @@ languages () { } topics () { - local out="" + local topic_items="" - out="$(topic-list "$@")" || return - [[ -n "${out}" ]] || { out 0; return; } + topic_items="$(topic-list "$@")" || return + [[ -n "${topic_items}" ]] || { out 0; return; } - printf '%s\n' "${out}" | wc -l | tr -d ' ' + printf '%s\n' "${topic_items}" | wc -l | tr -d ' ' } stars () { @@ -264,6 +259,6 @@ stars () { } views () { - view-list "$@" --jq '.count' + view-list "" --jq '.count' "$@" } diff --git a/src/mod/github/issue.sh b/src/mod/github/issue.sh index 61d074a..6c6d1c1 100644 --- a/src/mod/github/issue.sh +++ b/src/mod/github/issue.sh @@ -8,7 +8,10 @@ issue-list () { if [[ -n "${name}" && "${name}" != --* && "${name}" != [0-9]* ]]; then shift >/dev/null 2>&1 || true - [[ "${state}" == --* ]] && state="open" || shift >/dev/null 2>&1 || true + + if [[ "${state}" == --* ]]; then state="open" + else shift >/dev/null 2>&1 || true + fi name="$(repo "${name}")" || return gh issue list --repo "${name}" --state "${state}" "$@" @@ -29,7 +32,10 @@ issues () { if [[ -n "${name}" && "${name}" != --* && "${name}" != [0-9]* ]]; then shift >/dev/null 2>&1 || true - [[ "${state}" == --* ]] && state="open" || shift >/dev/null 2>&1 || true + + if [[ "${state}" == --* ]]; then state="open" + else shift >/dev/null 2>&1 || true + fi name="$(repo "${name}")" || return gh issue list --repo "${name}" --state "${state}" --json number -q 'length' "$@" @@ -41,7 +47,6 @@ issues () { gh issue list --json number -q 'length' "$@" } - issue-open () { need gh || return @@ -122,3 +127,18 @@ issue-comment () { succ "Issue commented: ${id}" } +issue-delete () { + + need gh || return + + local id="${1:-}" + shift >/dev/null 2>&1 || true + + [[ -n "${id}" ]] || { err "Missing issue number or url"; return; } + + gh issue delete "${id}" "$@" \ + || { err "Failed to delete issue: ${id}"; return; } + + succ "Issue deleted: ${id}" + +} diff --git a/src/mod/github/prs.sh b/src/mod/github/prs.sh index 2b7383c..e65a0e0 100644 --- a/src/mod/github/prs.sh +++ b/src/mod/github/prs.sh @@ -8,7 +8,10 @@ pr-list () { if [[ -n "${name}" && "${name}" != --* && "${name}" != [0-9]* ]]; then shift >/dev/null 2>&1 || true - [[ "${state}" == --* ]] && state="open" || shift >/dev/null 2>&1 || true + + if [[ "${state}" == --* ]]; then state="open" + else shift >/dev/null 2>&1 || true + fi name="$(repo "${name}")" || return gh pr list --repo "${name}" --state "${state}" "$@" @@ -29,7 +32,10 @@ prs () { if [[ -n "${name}" && "${name}" != --* && "${name}" != [0-9]* ]]; then shift >/dev/null 2>&1 || true - [[ "${state}" == --* ]] && state="open" || shift >/dev/null 2>&1 || true + + if [[ "${state}" == --* ]]; then state="open" + else shift >/dev/null 2>&1 || true + fi name="$(repo "${name}")" || return gh pr list --repo "${name}" --state "${state}" --json number -q 'length' "$@" @@ -41,7 +47,6 @@ prs () { gh pr list --json number -q 'length' "$@" } - pr-open () { need gh || return diff --git a/src/mod/stack/path.sh b/src/mod/stack/path.sh index 8bd3a11..eb852de 100644 --- a/src/mod/stack/path.sh +++ b/src/mod/stack/path.sh @@ -28,6 +28,31 @@ cd ../../../../.. || return +} +.6 () { + + cd ../../../../../.. || return + +} +.7 () { + + cd ../../../../../../.. || return + +} +.8 () { + + cd ../../../../../../../.. || return + +} +.9 () { + + cd ../../../../../../../../.. || return + +} +.10 () { + + cd ../../../../../../../../../.. || return + } home () { @@ -62,13 +87,33 @@ www () { } work () { - local name="${1:-}" base="" + local name="${1:-}" dir="/var/www" + + if [[ -n "${name}" ]]; then + + if [[ -e "/var/www/${name}" ]]; then dir="/var/www/${name}" + elif [[ -e "/var/www/projects/${name}" ]]; then dir="/var/www/projects/${name}" + elif [[ -e "/var/www/tools/${name}" ]]; then dir="/var/www/tools/${name}" + fi - if [[ -d /var/www/work ]]; then base="/var/www/work" - else base="/var/www/projects" fi - mkdir -p -- "${base}/${name}" || return - cd -- "${base}/${name}" || return + cd -- "${dir}" || return + +} +project () { + + local name="${1:-}" dir="/var/www/projects" + [[ -n "${name}" ]] && dir="${dir}/${name}" + + cd -- "${dir}" || return + +} +tool () { + + local name="${1:-}" dir="/var/www/tools" + [[ -n "${name}" ]] && dir="${dir}/${name}" + + cd -- "${dir}" || return } diff --git a/test.sh b/test.sh new file mode 100644 index 0000000..2f08be9 --- /dev/null +++ b/test.sh @@ -0,0 +1 @@ +echo hello