From 3729e762a6b7e6cf4efac173e61099b4614aff32 Mon Sep 17 00:00:00 2001 From: Patrick Kollitsch Date: Fri, 29 Nov 2024 08:49:42 +0700 Subject: [PATCH] feat: initiate binaries submodule --- .gitmodules | 3 + bin | 1 + bin/cronjobs/backup.sh | 54 ---------- bin/cronjobs/crontab.crontab | 4 - bin/cronjobs/filespace-discord.sh | 31 ------ bin/cronjobs/update-npm.sh | 48 --------- bin/helpers/cargo-install | 161 ------------------------------ bin/helpers/install-dev.mjs | 106 -------------------- bin/helpers/install-dev.toml | 7 -- bin/helpers/set_max_listeners.cjs | 7 -- bin/helpers/setup-repositories.sh | 112 --------------------- bin/helpers/window-sizer | 44 -------- bin/lib/placeholder | 0 bin/repo/release/.env.template | 4 - bin/repo/release/postrelease | 59 ----------- bin/repo/release/prerelease | 74 -------------- 16 files changed, 4 insertions(+), 711 deletions(-) create mode 160000 bin delete mode 100755 bin/cronjobs/backup.sh delete mode 100644 bin/cronjobs/crontab.crontab delete mode 100755 bin/cronjobs/filespace-discord.sh delete mode 100755 bin/cronjobs/update-npm.sh delete mode 100755 bin/helpers/cargo-install delete mode 100644 bin/helpers/install-dev.mjs delete mode 100644 bin/helpers/install-dev.toml delete mode 100644 bin/helpers/set_max_listeners.cjs delete mode 100755 bin/helpers/setup-repositories.sh delete mode 100755 bin/helpers/window-sizer delete mode 100644 bin/lib/placeholder delete mode 100644 bin/repo/release/.env.template delete mode 100755 bin/repo/release/postrelease delete mode 100755 bin/repo/release/prerelease diff --git a/.gitmodules b/.gitmodules index 133c7ad6..a51aa956 100644 --- a/.gitmodules +++ b/.gitmodules @@ -5,3 +5,6 @@ url = keybase://private/davidsneighbour/protected path = dotbot url = https://github.com/anishathalye/dotbot ignore = dirty +[submodule "bin"] + path = bin + url = git@github.com:davidsneighbour/binaries.git diff --git a/bin b/bin new file mode 160000 index 00000000..2c80f999 --- /dev/null +++ b/bin @@ -0,0 +1 @@ +Subproject commit 2c80f9996632a8bcc7aabee490060fc858f1d18f diff --git a/bin/cronjobs/backup.sh b/bin/cronjobs/backup.sh deleted file mode 100755 index a5518a27..00000000 --- a/bin/cronjobs/backup.sh +++ /dev/null @@ -1,54 +0,0 @@ -#!/bin/bash - -# Check if the current user is not root -if [ "$(id -u)" -ne 0 ]; then - echo "This script must be run as root. Exiting." - exit 1 -fi - -su patrick -c "notify-send -u critical \ - \"Backup\" \ - \"starting at $(date)\"" - -# Paths to backup -backup_files="/home /etc /root /boot /opt" -# backup_files="/etc" - -# Backup destination -dest="/media/patrick/Glacier/System" - -# Date variables -day=$(date +%A) -day_num=$(date +%-d) -month_num=$(date +%m) -hostname=$(hostname -s) - -# Week of the month calculation -week_num=$(((day_num - 1) / 7 + 1)) -week_file="${hostname}-week${week_num}.tgz" - -# Month calculation -month_file="${hostname}-month$((month_num % 2 + 1)).tgz" - -# Archive filename decision -archive_file="${hostname}-${day}.tgz" -[[ "${day_num}" == 1 ]] && archive_file=${month_file} -[[ "${day}" == "Saturday" ]] && archive_file=${week_file} - -# Backup process -echo "Backing up ${backup_files} to ${dest}/${archive_file}" -date -# shellcheck disable=SC2086 -tar czf "${dest}/${archive_file}" ${backup_files} || { - echo "Backup failed" - exit 1 -} -echo -e "\nBackup finished" -date - -# File sizes -ls -lh "${dest}/" - -su patrick -c "notify-send -u critical \ - \"Backup\" \ - \"starting at $(date)\"" diff --git a/bin/cronjobs/crontab.crontab b/bin/cronjobs/crontab.crontab deleted file mode 100644 index 2bff5380..00000000 --- a/bin/cronjobs/crontab.crontab +++ /dev/null @@ -1,4 +0,0 @@ -# m h dom mon dow command -0 0 * * * /home/patrick/github.com/davidsneighbour/dotfiles/bin/cronjobs/update-npm.sh >> /home/patrick/cron.log 2>&1 - -30 */4 * * * /home/patrick/github.com/davidsneighbour/dotfiles/bin/cronjobs/filespace-discord.sh >> /home/patrick/cron.log 2>&1 diff --git a/bin/cronjobs/filespace-discord.sh b/bin/cronjobs/filespace-discord.sh deleted file mode 100755 index a47cbc4c..00000000 --- a/bin/cronjobs/filespace-discord.sh +++ /dev/null @@ -1,31 +0,0 @@ -#!/bin/bash - -FILE=~/.env -if [ -f "${FILE}" ]; then - #echo "exporting .env" - set -a # export all variables created next - # this routine ranges through a folder of files that we don't explicitly know (@davidsneighbour) - # see https://github.com/koalaman/shellcheck/wiki/SC1090 - # shellcheck source=/dev/null - source "${FILE}" - set +a # stop exporting -fi - -# This variable holds the usage disk space in percents -CURRENT=$(df / | grep / | awk '{ print $5}' | sed 's/%//g') - -# The threshold where the alert will be sent. -THRESHOLD=75 - -if [ "${CURRENT}" -gt "${THRESHOLD}" ]; then - - PROJECT_NAME="Behemoth" - - # shellcheck disable=SC2154 - curl --location --request POST "${DISCORD_WEBHOOK}" \ - --form "content=\":floppy_disk: The disk space for ${PROJECT_NAME} is critical. - Used: ${CURRENT}%. - Please clean up some space.\" " \ - --form 'username="Disk Space Alert"' - -fi diff --git a/bin/cronjobs/update-npm.sh b/bin/cronjobs/update-npm.sh deleted file mode 100755 index 34a4ee67..00000000 --- a/bin/cronjobs/update-npm.sh +++ /dev/null @@ -1,48 +0,0 @@ -#!/bin/bash -# see https://kollitsch.dev/blog/2023/update-npm-packages-in-all-available-nvm-environments/ - -echo "##########################################################################" -echo "starting update-npm.sh" -# shellcheck disable=SC2005 -echo "$(date)" -echo "##########################################################################" - -curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash - -# exit if any command fails -set -e - -# load nvm -NVM_DIR="" -if [ -z "${XDG_CONFIG_HOME-}" ]; then - NVM_DIR="${HOME}/.nvm" -else - NVM_DIR="${XDG_CONFIG_HOME}/nvm" -fi - -export NVM_DIR -# shellcheck source=/dev/null -[ -s "${NVM_DIR}/nvm.sh" ] && \. "${NVM_DIR}/nvm.sh" - -for DIRNAME in /home/patrick/.nvm/versions/node/*/; do - - DIR=$(basename "${DIRNAME}") - nvm use "${DIR}" - - # if [[ $DIR == 'references' ]]; then - # continue - # fi - - # update global npm packages - # `npm ls -g` to list globally installed packages - nvm install --reinstall-packages-from=default --latest-npm 'lts/*' - -done - -echo "##########################################################################" -echo "done with update-npm.sh" -# shellcheck disable=SC2005 -echo "$(date)" -echo "##########################################################################" - -nvm use diff --git a/bin/helpers/cargo-install b/bin/helpers/cargo-install deleted file mode 100755 index 575bbe60..00000000 --- a/bin/helpers/cargo-install +++ /dev/null @@ -1,161 +0,0 @@ -#!/bin/sh - -set -e - -help() { - cat <<'EOF' -Install a binary release of a Rust crate hosted on GitHub - -Usage: - install.sh [options] - -Options: - -h, --help Display this message - --git SLUG Get the crate from "https://github/$SLUG" - -f, --force Force overwriting an existing binary - --crate NAME Name of the crate to install (default ) - --tag TAG Tag (version) of the crate to install (default ) - --target TARGET Install the release compiled for $TARGET (default <`rustc` host>) - --to LOCATION Where to install the binary (default ~/.cargo/bin) -EOF -} - -say() { - echo "install.sh: $1" -} - -say_err() { - say "$1" >&2 -} - -err() { - if [ -n "$td" ]; then - rm -rf "$td" - fi - - say_err "ERROR $1" - exit 1 -} - -need() { - if ! command -v "$1" >/dev/null 2>&1; then - err "need $1 (command not found)" - fi -} - -force=false -while test $# -gt 0; do - case $1 in - --crate) - crate=$2 - shift - ;; - --force | -f) - force=true - ;; - --git) - git=$2 - shift - ;; - --help | -h) - help - exit 0 - ;; - --tag) - tag=$2 - shift - ;; - --target) - target=$2 - shift - ;; - --to) - dest=$2 - shift - ;; - *) ;; - esac - shift -done - -# Dependencies -need basename -need curl -need install -need mkdir -need mktemp -need tar - -# Optional dependencies -if [ -z "$crate" ] || [ -z "$tag" ] || [ -z "$target" ]; then - need cut -fi - -if [ -z "$tag" ]; then - need rev -fi - -if [ -z "$target" ]; then - need grep - need rustc -fi - -if [ -z "$git" ]; then - # Markdown-style backticks - # shellcheck disable=SC2016 - err 'must specify a git repository using `--git`. Example: `install.sh --git japaric/cross`' -fi - -url="https://github.com/$git" -say_err "GitHub repository: $url" - -if [ -z "$crate" ]; then - crate=$(echo "$git" | cut -d'/' -f2) -fi - -say_err "Crate: $crate" - -url="$url/releases" - -if [ -z "$tag" ]; then - latest_url=$url/latest - tag_url=$(curl -Ls -o /dev/null -w "%{url_effective}" "$latest_url") - tag=$(echo "$tag_url" | rev | cut -d'/' -f1 | rev) - if [ -z "$tag" ]; then - err "Failed to get tag from $latest_url" - fi - say_err "Tag: latest ($tag)" -else - say_err "Tag: $tag" -fi - -if [ -z "$target" ]; then - target=$(rustc -Vv | grep host | cut -d' ' -f2) -fi - -say_err "Target: $target" - -if [ -z "$dest" ]; then - dest="$HOME/.cargo/bin" -fi - -say_err "Installing to: $dest" - -url="$url/download/$tag/$crate-$tag-$target.tar.gz" - -td=$(mktemp -d || mktemp -d -t tmp) -curl -sL "$url" | tar xz -f - -C "$td" - -for f in "$td"/*; do - test -x "$f" || continue - test -f "$f" || continue - - if [ -e "$dest/$(basename "$f")" ] && [ "$force" = false ]; then - err "$f already exists in $dest" - else - mkdir -p "$dest" - install -m 755 "$f" "$dest" - fi -done - -rm -rf "$td" diff --git a/bin/helpers/install-dev.mjs b/bin/helpers/install-dev.mjs deleted file mode 100644 index 2555aa47..00000000 --- a/bin/helpers/install-dev.mjs +++ /dev/null @@ -1,106 +0,0 @@ -#!/usr/bin/env node - -import { dirname, join } from 'path'; -import { fileURLToPath } from 'url'; -import fs from 'fs'; -import toml from 'toml'; -import { intro, outro, isCancel, cancel, text, confirm, select, multiselect, spinner } from '@clack/prompts'; -import shell from 'shelljs'; - -// Function to display help -function showHelp() { - console.log(` -Usage: node runPrograms.mjs [options] - -Options: - --config Path to the TOML configuration file (default: ./install-dev.toml) - --help Show this help message and exit - `); - process.exit(0); -} - -// Get the directory name of the current module -const __filename = fileURLToPath(import.meta.url); -const __dirname = dirname(__filename); - -// Parse command-line arguments -const args = process.argv.slice(2); -let configPath = join(__dirname, 'install-dev.toml'); -if (args.includes('--help')) { - showHelp(); -} -if (args.includes('--config')) { - const configIndex = args.indexOf('--config'); - if (configIndex !== -1 && args[configIndex + 1]) { - configPath = args[configIndex + 1]; - } else { - console.error('Error: --config option requires a path argument.'); - process.exit(1); - } -} - -// Load and parse the TOML configuration file -let config; -try { - const tomlContent = fs.readFileSync(configPath, 'utf8'); - config = toml.parse(tomlContent); -} catch (error) { - console.error('Error reading or parsing the TOML file:', error.message); - process.exit(1); -} - -async function main() { - intro('Install Dev Environment'); - - // Prepare the selection options from the config - const options = config.tool.map(function (tool, index) { - return ({ - label: tool.label, - value: tool.exec, - }); - }); - - // Display the multi-select prompt - const selectedIndices = await multiselect({ - message: 'Select programs to run:', - options, - }); - - if (isCancel(selectedIndices)) { - cancel('Operation cancelled.'); - process.exit(0); - } - - if (selectedIndices.length === 0) { - outro('No programs selected.'); - process.exit(0); - } - - // Confirm the selected programs - const confirmRun = await confirm({ - message: 'Do you want to run the selected programs?', - }); - - if (!confirmRun) { - outro('Operation cancelled.'); - process.exit(0); - } - - // Run the selected programs - const s = spinner(); - s.start('Executing'); - for (const command of selectedIndices) { - if (shell.exec(command, { stdio: 'inherit' }).code !== 0) { - console.error(`Error executing: ${command}`); - process.exit(1); - } - } - s.stop('Done'); - - outro('All selected programs executed successfully.'); -} - -main().catch((error) => { - console.error('An unexpected error occurred:', error.message); - process.exit(1); -}); diff --git a/bin/helpers/install-dev.toml b/bin/helpers/install-dev.toml deleted file mode 100644 index 8901569e..00000000 --- a/bin/helpers/install-dev.toml +++ /dev/null @@ -1,7 +0,0 @@ -[[tool]] -label = "Rust and Cargo (install)" -exec = "curl https://sh.rustup.rs -sSf | sh -s -- -y" - -[[tool]] -label = "Bun (install/update)" -exec = "curl -fsSL https://bun.sh/install | bash" diff --git a/bin/helpers/set_max_listeners.cjs b/bin/helpers/set_max_listeners.cjs deleted file mode 100644 index 426e3d3b..00000000 --- a/bin/helpers/set_max_listeners.cjs +++ /dev/null @@ -1,7 +0,0 @@ -// Set the maximum number of listeners globally -require('events').EventEmitter.defaultMaxListeners = 20; - -// fixes the following warning -// (node:1238552) MaxListenersExceededWarning: Possible EventEmitter memory leak -// detected. 11 close listeners added to [TLSSocket]. Use -// emitter.setMaxListeners() to increase limit diff --git a/bin/helpers/setup-repositories.sh b/bin/helpers/setup-repositories.sh deleted file mode 100755 index 3913bfab..00000000 --- a/bin/helpers/setup-repositories.sh +++ /dev/null @@ -1,112 +0,0 @@ -#!/bin/bash - -usage() { - echo "Usage: $0 [--output-dir OUTPUT_DIR] [--debug]" - echo " --output-dir Directory where repositories will be cloned (optional, default is current directory)." - echo " --debug Enables debug mode which saves API responses as JSON files." - exit 1 -} - -check_dependencies() { - for cmd in curl jq git; do - command -v "${cmd}" >/dev/null 2>&1 || { - echo >&2 "The script requires ${cmd} but it's not installed. Aborting." - exit 1 - } - done -} - -parse_arguments() { - while [[ "$#" -gt 0 ]]; do - case $1 in - --output-dir) - output_dir="${2%/}" - shift - ;; - --debug) - debug_mode="true" - ;; - *) - echo "Unknown parameter passed: $1" - usage - ;; - esac - shift - done -} - -initialize() { - if [ -z "${output_dir}" ]; then - output_dir="." - fi - mkdir -p "${output_dir}" || exit - cd "${output_dir}" || exit - - if [ -f ~/.env ]; then - # shellcheck source=/dev/null - source ~/.env - else - echo "Error: .env file not found in home directory." - exit 2 - fi - - if [ -z "${GITHUB_DEV_TOKEN}" ]; then - echo "Error: GitHub development token not set in .env file." - exit 3 - fi -} - -fetch_and_clone_repos() { - local page=1 all_repos_fetched=false response repo_count - while [ "${all_repos_fetched}" = false ]; do - response=$(curl -sH "Authorization: token ${GITHUB_DEV_TOKEN}" "https://api.github.com/user/repos?type=all&per_page=100&page=${page}") - if ! echo "${response}" | jq . >/dev/null 2>&1; then - echo "Failed to parse JSON, or got an error from GitHub:" - echo "${response}" - exit 4 - fi - - if [ "${debug_mode}" = "true" ]; then - echo "${response}" >"debug_response_page_${page}.json" - fi - - repo_count=$(echo "${response}" | jq -r '. | length') - if [ "${repo_count}" -eq 0 ]; then - all_repos_fetched=true - echo "No more repositories to clone." - break - fi - - clone_repositories "${response}" - ((page++)) - done -} - -clone_repositories() { - echo "$1" | jq -r '.[] | .name, .ssh_url' | while - read -r repo_name - read -r ssh_url - do - if [ -z "${ssh_url}" ]; then - echo "A repository without a clone URL was encountered: ${repo_name}" - continue - fi - if [ -d "${output_dir}/${repo_name}" ]; then - echo "Directory ${output_dir}/${repo_name} already exists, skipping clone." - continue - fi - echo "Cloning ${ssh_url} into ${output_dir}/${repo_name}" - git clone "${ssh_url}" "${output_dir}/${repo_name}" - done -} - -main() { - check_dependencies - debug_mode="false" - parse_arguments "$@" - initialize - fetch_and_clone_repos - echo "Repositories cloned in ${output_dir}" -} - -main "$@" diff --git a/bin/helpers/window-sizer b/bin/helpers/window-sizer deleted file mode 100755 index 808a792d..00000000 --- a/bin/helpers/window-sizer +++ /dev/null @@ -1,44 +0,0 @@ -#!/bin/bash - -# default width and height -DEFAULT_WIDTH=1200 -DEFAULT_HEIGHT=600 - -# check for CLI arguments -if [ $# -gt 1 ]; then - WIDTH=$(echo "$1" | cut -d 'x' -f 1) - HEIGHT=$(echo "$1" | cut -d 'x' -f 2) -else - WIDTH=${DEFAULT_WIDTH} - HEIGHT=${DEFAULT_HEIGHT} -fi - -# validate that width and height are numbers -if ! [[ ${WIDTH} =~ ^[0-9]+$ ]] || ! [[ ${HEIGHT} =~ ^[0-9]+$ ]]; then - echo "Error: Width and height must be numbers." - exit 2 -fi - -# use xwininfo to allow the user to select a window and capture its ID -echo "Please click on a window to select it for resizing." -TARGET_WINDOW_INFO=$(xwininfo) -TARGET_WINDOW_ID=$(echo "${TARGET_WINDOW_INFO}" | grep "Window id:" | awk '{print $4}') -echo "${TARGET_WINDOW_ID}" -if [ -z "${TARGET_WINDOW_ID}" ]; then - echo "Error: No window selected or window ID not found." - exit 1 -fi - -# calculate new window position for centering -SCREEN_SIZE=$(xrandr | grep '*' | head -n 1 | awk '{print $1}') -SCREEN_WIDTH=$(echo "${SCREEN_SIZE}" | cut -d 'x' -f 1) -SCREEN_HEIGHT=$(echo "${SCREEN_SIZE}" | cut -d 'x' -f 2) -NEW_X=$(( (SCREEN_WIDTH / 2) - (WIDTH / 2) )) -NEW_Y=$(( (SCREEN_HEIGHT / 2) - (HEIGHT / 2) )) - -# 1) unmaximize the window, if it is maximized -wmctrl -i -r "${TARGET_WINDOW_ID}" -b remove,maximized_vert,maximized_horz -# 2) resize and reposition the window -wmctrl -i -r "${TARGET_WINDOW_ID}" -e "0,${NEW_X},${NEW_Y},${WIDTH},${HEIGHT}" -# 3) raise the window -wmctrl -i -a "${TARGET_WINDOW_ID}" diff --git a/bin/lib/placeholder b/bin/lib/placeholder deleted file mode 100644 index e69de29b..00000000 diff --git a/bin/repo/release/.env.template b/bin/repo/release/.env.template deleted file mode 100644 index cca3d985..00000000 --- a/bin/repo/release/.env.template +++ /dev/null @@ -1,4 +0,0 @@ -GITHUB_REPO="https://github.com/davidsneighbour/dotfiles/" - -GITHUB_REPOSLUG="davidsneighbour/dotfiles" -GITHUB_USER=davidsneighbour diff --git a/bin/repo/release/postrelease b/bin/repo/release/postrelease deleted file mode 100755 index 263e93bb..00000000 --- a/bin/repo/release/postrelease +++ /dev/null @@ -1,59 +0,0 @@ -#!/bin/bash - -set -e - -REQUIRED_TOOLS=( - git - sed - node - curl -) - -# check if all requirements are met -for TOOL in "${REQUIRED_TOOLS[@]}"; do - if ! command -v "${TOOL}" >/dev/null; then - echo "${TOOL} is required... " - exit 1 - fi -done - -VERSION=$(node -pe 'require("./package.json")["version"]') - -FILE=.env -if [ -f "$FILE" ]; then - echo "exporting .env" - set -a # export all variables created next - # this routine ranges through a folder of files that we don't explicitly know (@davidsneighbour) - # see https://github.com/koalaman/shellcheck/wiki/SC1090 - # shellcheck source=/dev/null - source "${FILE}" - set +a # stop exporting -else - echo "no .env file found" - exit 0 -fi - -# push everything up -git push origin main --follow-tags -git push origin --tags - -# create a release on GitHub -CHANGES=$(jq -Rs . /dev/null diff --git a/bin/repo/release/prerelease b/bin/repo/release/prerelease deleted file mode 100755 index c25e4111..00000000 --- a/bin/repo/release/prerelease +++ /dev/null @@ -1,74 +0,0 @@ -#!/bin/bash - -set -e - -REQUIRED_TOOLS=( - git - curl -) - -for TOOL in "${REQUIRED_TOOLS[@]}"; do - if ! command -v "${TOOL}" >/dev/null; then - echo "${TOOL} is required... " - exit 1 - fi -done - -FILE=.env -if [ -f "$FILE" ]; then - echo "exporting .env" - set -a # export all variables created next - # this routine ranges through a folder of files that we don't explicitly know (@davidsneighbour) - # see https://github.com/koalaman/shellcheck/wiki/SC1090 - # shellcheck source=/dev/null - source "${FILE}" - set +a # stop exporting -fi - -SCRIPTPATH="$( - cd "$(dirname "$0")" >/dev/null 2>&1 || exit - pwd -P -)" -CURPATH="$(pwd -P)" - -RE='[^0-9]*\([0-9]*\)[.]\([0-9]*\)[.]\([0-9]*\)\([0-9A-Za-z-]*\)' - -step="$1" -if [ -z "$1" ]; then - step="patch" -fi - -base=$(node -pe 'require("./package.json")["version"]') - -MAJOR=$(echo $base | sed -e "s#$RE#\1#") -MINOR=$(echo $base | sed -e "s#$RE#\2#") -PATCH=$(echo $base | sed -e "s#$RE#\3#") - -case "$step" in -major) - ((MAJOR += 1)) - ((MINOR = 0)) - ((PATCH = 0)) - ;; -minor) - ((MINOR += 1)) - ((PATCH = 0)) - ;; -patch) - ((PATCH += 1)) - ;; -esac - -RELEASE="$MAJOR.$MINOR.$PATCH" - -# update version in citation configuration -FILE=CITATION.cff -if test -f "$FILE"; then - sed -i "s/^version: .*/version: ${RELEASE}/" $FILE - DATE=$(date +%F) - sed -i "s/date-released: .*/date-released: ${DATE}/" $FILE - git add $FILE -fi - -RELEASE_NOTES="$(npx commit-and-tag-version --dry-run | awk 'BEGIN { flag=0 } /^---$/ { if (flag == 0) { flag=1 } else { flag=2 }; next } flag == 1')" -echo "${RELEASE_NOTES}" >changes.md