Last active
November 6, 2024 19:29
-
-
Save ridhwaans/c6bf520d0267fab8af8cffd2991a9021 to your computer and use it in GitHub Desktop.
(test) A collection of bash scripts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -e | |
| function hello(){ | |
| echo "hello world!" | |
| } | |
| function pull_scripts(){ | |
| mkdir -p "$HOME/Source" && curl -sfSL "https://gist.githubusercontent.com/ridhwaans/c6bf520d0267fab8af8cffd2991a9021/raw/scripts-test.sh" -o "$HOME/Source/scripts.sh" | |
| chmod +x "$HOME/Source/scripts.sh" | |
| source "$HOME/Source/scripts.sh" | |
| } | |
| function autokey-github(){ | |
| if [ "$#" -ne 2 ]; then | |
| echo "Usage: autokey-github <key_title> <gh_token>" | |
| return 1 | |
| fi | |
| local KEY_TITLE=$1 | |
| local GH_TOKEN=$2 | |
| defaults=("id_dsa" "id_ecdsa" "id_ed25519" "id_rsa") | |
| if [[ ! " ${defaults[*]} " =~ " ${KEY_TITLE} " ]]; then | |
| echo "SSH key pair doesn't use a default name. IdentityFile must be configured in ~/.ssh/config" | |
| fi | |
| ssh-keygen -t ed25519 -C $KEY_TITLE -f ~/.ssh/$KEY_TITLE | |
| PUBLIC_KEY=$(cat ~/.ssh/$KEY_TITLE.pub) | |
| eval "$(ssh-agent -s)" | |
| ssh-add ~/.ssh/$KEY_TITLE | |
| RESPONSE=$(curl -s -H "Authorization: token ${GH_TOKEN}" \ | |
| -X POST --data-binary "{\"title\":\"${KEY_TITLE}\",\"key\":\"${PUBLIC_KEY}\"}" \ | |
| https://api.github.com/user/keys) | |
| KEY_ID=$(echo $RESPONSE | grep -o '\"id.*' | grep -o "[0-9]*" | grep -m 1 "[0-9]*") | |
| echo "Public key deployed to remote service" | |
| ssh -T [email protected] || true | |
| } | |
| function switch_remote() { | |
| current_url=$(git config --get remote.origin.url) | |
| if [[ $current_url == https://* ]]; then | |
| ssh_url=${current_url/https:\/\//git@} | |
| ssh_url=${ssh_url%.git}.git | |
| git remote set-url origin $ssh_url | |
| echo "Remote URL updated to SSH:" | |
| echo "$ssh_url" | |
| else | |
| echo "Remote URL is already using SSH:" | |
| echo "$current_url" | |
| fi | |
| } | |
| function cherry-pick() { | |
| local default_branch=$(git remote show origin | grep 'HEAD branch' | cut -d ':' -f 2 | xargs) | |
| local your_branch_name="$1" | |
| local your_branch_backup="${your_branch_name}-backup" | |
| local exclusive_commits=$(git log ${your_branch_backup} ^master --pretty=%H || true) | |
| git branch -m "$your_branch_backup" "${your_branch_backup}" || true | |
| git switch $default_branch | |
| git switch -c "$your_branch_name" | |
| if [[ -n "$exclusive_commits" ]]; then | |
| for commit_sha in $exclusive_commits; do | |
| git cherry-pick --no-commit "$commit_sha" || true | |
| done | |
| fi | |
| } | |
| function rm-submodule() { | |
| if [ -z "$1" ]; then | |
| echo "Cannot find path to submodule. Exiting." | |
| exit 1 | |
| fi | |
| git submodule deinit -f $1 || true | |
| rm -rf .git/modules/$1 || true | |
| git rm -f $1 || true | |
| } | |
| function update-submodules() { | |
| git submodule update --init --recursive --remote | |
| } | |
| function pull-recursive() { | |
| _inner() { | |
| local submodule_path=$1 | |
| pull-recursive "$submodule_path" | |
| } | |
| local repo_path=${1:-$(git rev-parse --show-toplevel)} | |
| local default_branch=$(git remote show origin | grep 'HEAD branch' | cut -d ':' -f 2 | xargs) | |
| if [ -n "$(git -C "$repo_path" status --porcelain)" ]; then | |
| git -C "$repo_path" stash save "Stashed changes before switching branches" || true | |
| fi | |
| cd "$repo_path" || return | |
| git checkout "$default_branch" | |
| git pull | |
| git checkout - | |
| [ -n "$(git -C "$repo_path" stash list)" ] && git -C "$repo_path" stash pop || true | |
| git submodule foreach --recursive _inner | |
| } | |
| function touchsh() { | |
| touch "$1" | |
| chmod +x "$1" | |
| } | |
| function reopen_in_container() { | |
| local IMAGE_NAME="${1:-"base"}" | |
| local CONTAINER_NAME="${2:-"instance"}" | |
| if [ "$(docker inspect -f '{{.State.Running}}' $CONTAINER_NAME 2>/dev/null)" = "true" ]; then | |
| echo "Container $CONTAINER_NAME is already running." | |
| else | |
| if [ "$(docker inspect -f '{{.State.Status}}' $CONTAINER_NAME 2>/dev/null)" = "exited" ]; then | |
| docker start -i $CONTAINER_NAME | |
| else | |
| echo "Building Docker image..." | |
| if [ -f Dockerfile ]; then | |
| docker build --no-cache -t "$IMAGE_NAME" . > build_log.txt 2>&1 | |
| else | |
| echo "Dockerfile not found. Exiting." | |
| exit 1 | |
| fi | |
| docker run -it --name $CONTAINER_NAME $IMAGE_NAME | |
| fi | |
| fi | |
| docker exec -it "$CONTAINER_NAME" /bin/bash | |
| } | |
| function cleanup_container() { | |
| local IMAGE_NAME="${1:-"base"}" | |
| local CONTAINER_NAME="${2:-"instance"}" | |
| if docker ps -a --format '{{.Names}}' | grep -q "$CONTAINER_NAME"; then | |
| docker stop "$CONTAINER_NAME" && docker rm "$CONTAINER_NAME" | |
| else | |
| echo "Container $CONTAINER_NAME not found." | |
| fi | |
| if docker images --format '{{.Repository}}:{{.Tag}}' | grep -q "$IMAGE_NAME"; then | |
| docker rmi "$IMAGE_NAME" | |
| else | |
| echo "Image $IMAGE_NAME not found." | |
| fi | |
| } | |
| function clean_docker() { | |
| docker stop $(docker ps -aq) || true | |
| docker rm $(docker ps -aq) || true | |
| docker volume prune -f || true | |
| docker rmi $(docker images -aq) || true | |
| docker builder prune -a -f || true | |
| } | |
| function purge_repo_workflows(){ | |
| if [ "$#" -ne 2 ]; then | |
| echo "Usage: purge_repo_workflows <owner> <repo>" | |
| return 1 | |
| fi | |
| local owner="${1:-"ridhwaans"}" | |
| local repo="${2:-""}" | |
| local workflow_runs | |
| workflow_runs=$(gh run list --repo "$owner/$repo" --json databaseId --jq '.[].databaseId' || true) | |
| for run_number in $workflow_runs; do | |
| gh run cancel "$run_number" --repo "$owner/$repo" || true | |
| gh run delete "$run_number" --repo "$owner/$repo" || true | |
| done | |
| } | |
| function recreate_codespace(){ | |
| if [ "$#" -ne 2 ]; then | |
| echo "Usage: recreate_codespace <repo-name> <codespace-name>" | |
| return 1 | |
| fi | |
| local repo_name="$1" | |
| local codespace_name="$2" | |
| gh codespace list --json name --limit 1 | jq -r '.[0].name' | xargs -I {} gh codespace delete {} || true | |
| sleep 10 | |
| gh repo create "$repo_name" --public | |
| gh repo clone "$repo_name" | |
| cd "$repo_name" | |
| gh codespace create --name "$codespace_name" --build | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment