This commit is contained in:
2024-10-30 21:55:48 +01:00
parent 19a1d70af1
commit 93e963cd65
47 changed files with 1007 additions and 1016 deletions

View File

@ -1,163 +0,0 @@
#!/usr/bin/env bash
source $HOME/dotfiles/bin/helpers/functions.sh
add_brave_repo() {
# Check if we have a brave-browser-release.list file already, if not then create one
if [ ! -f /etc/apt/sources.list.d/brave-browser-release.list ]; then
sudo curl -fsSLo /usr/share/keyrings/brave-browser-archive-keyring.gpg https://brave-browser-apt-release.s3.brave.com/brave-browser-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/brave-browser-archive-keyring.gpg] https://brave-browser-apt-release.s3.brave.com/ stable main"|sudo tee /etc/apt/sources.list.d/brave-browser-release.list
result=$(sudo apt update 2>&1)
if [ $? -ne 0 ]; then
printfe "%s\n" "red" " - Failed to add Brave repository"
printfe "%s\n" "yellow" "$result"
exit 1
fi
printfe "%s\n" "yellow" " - Added Brave repository"
else
printfe "%s\n" "green" " - Brave repository already added"
fi
}
add_1password_repo() {
# Check if we have a 1password.list file already, if not then create one
if [ ! -f /etc/apt/sources.list.d/1password.list ]; then
curl -sS https://downloads.1password.com/linux/keys/1password.asc | sudo gpg --dearmor --output /usr/share/keyrings/1password-archive-keyring.gpg
echo 'deb [arch=amd64 signed-by=/usr/share/keyrings/1password-archive-keyring.gpg] https://downloads.1password.com/linux/debian/amd64 stable main' | sudo tee /etc/apt/sources.list.d/1password.list
sudo mkdir -p /etc/debsig/policies/AC2D62742012EA22/
curl -sS https://downloads.1password.com/linux/debian/debsig/1password.pol | sudo tee /etc/debsig/policies/AC2D62742012EA22/1password.pol
sudo mkdir -p /usr/share/debsig/keyrings/AC2D62742012EA22
curl -sS https://downloads.1password.com/linux/keys/1password.asc | sudo gpg --dearmor --output /usr/share/debsig/keyrings/AC2D62742012EA22/debsig.gpg
result=$(sudo apt update 2>&1)
if [ $? -ne 0 ]; then
printfe "%s\n" "red" " - Failed to add 1Password repository"
printfe "%s\n" "yellow" "$result"
exit 1
fi
printfe "%s\n" "yellow" " - Added 1Password repository"
else
printfe "%s\n" "green" " - 1Password repository already added"
fi
}
add_vscode_repo() {
# Check if we have a vscode.list file already, if not then create one
if [ ! -f /etc/apt/sources.list.d/vscode.list ]; then
wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > packages.microsoft.gpg
sudo install -D -o root -g root -m 644 packages.microsoft.gpg /etc/apt/keyrings/packages.microsoft.gpg
echo "deb [arch=amd64,arm64,armhf signed-by=/etc/apt/keyrings/packages.microsoft.gpg] https://packages.microsoft.com/repos/code stable main" |sudo tee /etc/apt/sources.list.d/vscode.list > /dev/null
rm -f packages.microsoft.gpg
result=$(sudo apt update 2>&1)
if [ $? -ne 0 ]; then
printfe "%s\n" "red" " - Failed to add VSCode repository"
printfe "%s\n" "yellow" "$result"
exit 1
fi
printfe "%s\n" "yellow" " - Added VSCode repository"
else
printfe "%s\n" "green" " - VSCode repository already added"
fi
}
ensure_repositories() {
add_brave_repo
add_1password_repo
add_vscode_repo
repos=($(cat $DOTFILES_CONFIG | shyaml get-values config.packages.apt.repos))
for repo in "${repos[@]}"; do
repo_name=$(echo $repo | cut -d ":" -f 2)
# Go through sources.list.d and check if there's a file containing part of URIs: https://ppa.launchpad.net/$repo_name
# We have to check the files not the file names since the file names are not always the same as the repo_name
result=$(grep -r "$repo_name" /etc/apt/sources.list.d/*)
if [ -z "$result" ]; then
printfe "%s\n" "yellow" " - Adding $repo_name repository..."
clear_line
sudo add-apt-repository -y $repo
if [ $? -ne 0 ]; then
printfe "%s\n" "red" " - Failed to add $repo_name repository"
exit 1
else
printfe "%s\n" "green" " - $repo_name repository added successfully"
fi
else
printfe "%s\n" "green" " - $repo_name repository already added"
fi
done
}
ensure_apt_packages_installed() {
apt_packages=($(cat $DOTFILES_CONFIG | shyaml get-values config.packages.apt.apps | tr '\n' ' '))
# Check if apt_packages array contains duplicates
if [ $(echo $apt_packages | tr ' ' '\n' | sort | uniq -d | wc -l) -ne 0 ]; then
printfe "%s\n" "red" "The apt_packages array contains duplicates"
printfe "%s\n" "yellow" "Duplicates:"
printfe "%s\n" "blue" $(echo $apt_packages | tr ' ' '\n' | sort | uniq -d)
exit 1
fi
for package in "${apt_packages[@]}"; do
pkg_status=$(dpkg -s $package 2> /dev/null | grep "Status" | cut -d " " -f 4)
# If pkg_status is `installed` then we don't need to install the package, otherwise if it's empty then the package is not installed
if [ -z $pkg_status ]; then
ensure_sudo_privileges "In order to install $package, please provide your password:"
printfe "%s" "yellow" " - Installing $package..."
clear_line
result=$(sudo apt install -y $package 2>&1)
if [ $? -ne 0 ]; then
printfe "%s\n" "red" " - Failed to install $package"
printfe "%s\n" "yellow" "$result"
exit 1
else
printfe "%s\n" "green" " - $package installed successfully"
fi
else
printfe "%s\n" "green" " - $package is already installed"
fi
done
}
print_apt_status() {
printfe "%s" "cyan" "Checking APT packages..."
clear_line
apt_packages=($(cat $DOTFILES_CONFIG | shyaml get-values config.packages.apt.apps))
# count entries in packages
count=$(echo $apt_packages | wc -w)
installed=0
for package in "${apt_packages[@]}"; do
pkg_status=$(dpkg -s $package 2> /dev/null | grep "Status" | cut -d " " -f 4)
if [ "$pkg_status" = "installed" ]; then
installed=$((installed + 1))
else
if [ "$verbose" = true ]; then
printfe "%s\n" "red" "$package is not installed"
fi
fi
done
printfe "%s" "cyan" "APT"
if [ $installed -eq $count ]; then
printfe "%s" "green" " $installed/$count "
else
printfe "%s" "red" " $installed/$count "
fi
printfe "%s\n" "cyan" "packages installed"
}

View File

@ -1,34 +0,0 @@
#!/usr/bin/env bash
ensure_docker_installed() {
# if docker is already installed, skip the installation
if [ -x "$(command -v docker)" ]; then
printfe "%s\n" "green" " - Docker is already installed"
return
fi
# Add Docker's official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# Add Docker's repository
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io
# Check if it successfully installed
if [ -x "$(command -v docker)" ]; then
printfe "%s\n" "green" " - Docker is installed"
else
printfe "%s\n" "red" " - Docker is not installed"
printfe "%s\n" "red" " Something went wrong while installing Docker, investigate the issue"
exit 1
fi
sudo usermod -aG docker $USER
sudo systemctl start docker
sudo systemctl enable docker
}

View File

@ -7,8 +7,7 @@ println() {
}
is_wsl() {
unameres=$(uname -a | grep -i "microsoft" | wc -l)
if [ -n "$unameres" ]; then
if [ -f "/proc/sys/fs/binfmt_misc/WSLInterop" ]; then
return 0
else
return 1
@ -142,11 +141,87 @@ add_to_hosts() {
fi
}
function exesudo ()
{
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##
#
# LOCAL VARIABLES:
#
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##
#
# I use underscores to remember it's been passed
local _funcname_="$1"
local params=( "$@" ) ## array containing all params passed here
local tmpfile="/dev/shm/$RANDOM" ## temporary file
local content ## content of the temporary file
local regex ## regular expression
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##
#
# MAIN CODE:
#
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##
#
# WORKING ON PARAMS:
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# Shift the first param (which is the name of the function)
unset params[0] ## remove first element
# params=( "${params[@]}" ) ## repack array
#
# WORKING ON THE TEMPORARY FILE:
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
content="#!/bin/bash\n\n"
#
# Write the params array
content="${content}params=(\n"
regex="\s+"
for param in "${params[@]}"
do
if [[ "$param" =~ $regex ]]
then
content="${content}\t\"${param}\"\n"
else
content="${content}\t${param}\n"
fi
done
content="$content)\n"
echo -e "$content" > "$tmpfile"
#
# Append the function source
echo "#$( type "$_funcname_" )" >> "$tmpfile"
#
# Append the call to the function
echo -e "\n$_funcname_ \"\${params[@]}\"\n" >> "$tmpfile"
#
# DONE: EXECUTE THE TEMPORARY FILE WITH SUDO
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sudo bash "$tmpfile"
rm "$tmpfile"
}
resolve_path() {
echo "$(cd "$(dirname "$1")"; pwd)/$(basename "$1")"
}
check_or_make_symlink() {
source /home/menno/dotfiles/bin/helpers/functions.sh
SOURCE="$1"
TARGET="$2"
@ -157,6 +232,22 @@ check_or_make_symlink() {
SOURCE=$(resolve_path "$SOURCE")
TARGET=$(resolve_path "$TARGET")
# Check if we have permissions to create the symlink
if [ ! -w "$(dirname "$TARGET")" ]; then
# Check if link exists
if [ -L "$TARGET" ]; then
# Check if it points to the correct location
if [ "$(readlink "$TARGET")" != "$SOURCE" ]; then
exesudo check_or_make_symlink "$SOURCE" "$TARGET"
return
fi
else
# Link doesn't exist but we don't have permissions to create it, so we should try to create it with sudosudo
exesudo check_or_make_symlink "$SOURCE" "$TARGET"
fi
return
fi
# If target is already a symlink, we should check if it points to the correct location
if [ -L "$TARGET" ]; then
if [ "$(readlink "$TARGET")" != "$SOURCE" ]; then

View File

@ -1,119 +0,0 @@
#!/usr/bin/env bash
source $HOME/dotfiles/bin/helpers/functions.sh
# Ensure jq is installed
if ! command -v jq &> /dev/null; then
echo "jq could not be found, please install it."
exit 1
fi
# Parse JSON file
shortcuts_file=~/dotfiles/gnome/keyboard-shortcuts.json
if [ ! -f "${shortcuts_file}" ]; then
echo "Shortcuts file not found: ${shortcuts_file}"
exit 1
fi
shortcuts=$(jq -r '.shortcuts' "${shortcuts_file}")
ensure_swhkd() {
shortcuts_keys=($(cat "$DOTFILES_CONFIG" | shyaml keys config.keybinds))
file_contents=""
for key in "${shortcuts_keys[@]}"; do
shortcut=$(cat "$DOTFILES_CONFIG" | shyaml get-value config.keybinds.$key.shortcut)
command=$(cat "$DOTFILES_CONFIG" | shyaml get-value config.keybinds.$key.command)
file_contents="${file_contents}\n${shortcut}\n ${command}\n\n"
printfe "%s\n" "green" " - Ensuring swhkd shortcut ${shortcut} command ${command}"
done
echo -e "${file_contents}" > $HOME/.config/swhkdrc
}
ensure_keyboard_shortcuts() {
if is_wsl; then
printfe "%s\n" "yellow" "Running in WSL, skipping keyboard shortcuts."
return
fi
printfe "%s\n" "green" " - Setting up swhkd configuration..."
ensure_swhkd
# If swhkd is running, kill it
if pgrep -x "swhkd" > /dev/null; then
printfe "%s\n" "yellow" " - swhkd is running, killing it..."
sudo pkill swhkd
fi
# Same for swhks
if pgrep -x "swhks" > /dev/null; then
printfe "%s\n" "yellow" " - swhks is running, killing it..."
sudo pkill swhks
fi
# Start swhkd
printfe "%s\n" "green" " - starting swhkd..."
printfe "%s\n" "yellow" " Note: this will likely show a password prompt, please enter your password"
screen -dmS swhkd bash -c "$HOME/dotfiles/bin/actions/hotkey-daemon.sh"
# Check if this is gnome DESKTOP_SESSION is gnome, if not we can stop here
# The next part is just for setting up custom shortcuts in GNOME
if [ "$XDG_CURRENT_DESKTOP" != "GNOME" ]; then
return
fi
# Retrieve current custom keybindings
existing_bindings=$(gsettings get org.gnome.settings-daemon.plugins.media-keys custom-keybindings | tr -d "[]'")
new_bindings=()
index=0
# Iterate over parsed JSON shortcuts
for key_combination in $(echo "$shortcuts" | jq -r 'keys[]'); do
command=$(echo "$shortcuts" | jq -r ".[\"$key_combination\"]")
printfe '%s\n' "green" " - Ensuring GNOME shortcut ${key_combination} command ${command}"
custom_binding="/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom${index}/"
new_bindings+=("$custom_binding")
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:"${custom_binding}" name "${key_combination} to run ${command}"
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:"${custom_binding}" command "${command}"
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:"${custom_binding}" binding "${key_combination}"
dconf write "${custom_binding}enabled" true
((index++))
done
new_bindings_string=$(printf "'%s', " "${new_bindings[@]}")
new_bindings_string="[${new_bindings_string%, }]"
gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings "${new_bindings_string}"
}
print_keyboard_shortcuts_status() {
printfe "%s" "cyan" "Checking keyboard shortcuts..."
clear_line
# Retrieve current custom keybindings
existing_bindings=$(gsettings get org.gnome.settings-daemon.plugins.media-keys custom-keybindings | tr -d "[]'")
existing_count=$(echo $existing_bindings | tr -cd , | wc -c)
# Iterate over parsed JSON shortcuts
for key_combination in $(echo "$shortcuts" | jq -r 'keys[]'); do
command=$(echo "$shortcuts" | jq -r ".[\"$key_combination\"]")
if [[ ! $existing_bindings =~ "custom${index}" ]]; then
printfe "%s\n" "red" " - Custom shortcut ${key_combination} is missing"
fi
((index++))
done
json_count=$(echo $shortcuts | jq 'keys | length')
printfe "%s" "cyan" "Keyboard shortcuts"
if [ $index -eq $json_count ]; then
printfe "%s" "green" " $index/$json_count "
else
printfe "%s" "red" " $index/$json_count "
fi
printfe "%s\n" "cyan" "shortcuts installed"
}

View File

@ -1,28 +0,0 @@
#!/usr/bin/env bash
source $HOME/dotfiles/bin/helpers/functions.sh
ensure_rust_installed() {
if [ -x "$(command -v rustc)" ]; then
printfe "%s\n" "green" " - Rust is already installed"
# Update Rust
printfe "%s" "yellow" " - Updating Rust..."
echo -en "\r"
rustup update
else
printfe "%s\n" "yellow" " - Installing Rust..."
echo -en "\r"
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
if [ $? -ne 0 ]; then
printfe "%s\n" "red" "Failed to install Rust"
exit 1
fi
rustup default stable
printfe "%s\n" "green" " - Rust installed successfully"
fi
}

View File

@ -1,42 +0,0 @@
#!/usr/bin/env bash
source $HOME/dotfiles/bin/helpers/functions.sh
ensure_user_groups() {
# Load yaml file
users=($(cat $DOTFILES_CONFIG | shyaml keys config.user_groups))
# For each user, ensure they are in the correct groups
for user in "${users[@]}"; do
# Ensure this user exists
if [[ ! $(id -u $user) ]]; then
printfe "%s\n" "red" " - User $user does not exist"
continue
fi
ensure_user_in_groups $user
done
}
ensure_user_in_groups() {
user=$1
groups=($(cat $DOTFILES_CONFIG | shyaml get-values config.user_groups.$user))
printfe "%s\n" "cyan" " - For user $user..."
# For each group, ensure the user is in it
for group in "${groups[@]}"; do
# Check if the group exists at all, otherwise skip
if [[ ! $(getent group $group) ]]; then
printfe "%s\n" "red" " Group $group does not exist"
continue
fi
if [[ ! $groups == *$group* ]]; then
printfe "%s\n" "green" " Adding $user to group $group"
sudo usermod -aG $group $user
else
printfe "%s\n" "green" " $user is already in group $group"
fi
done
}

View File

@ -1,81 +0,0 @@
#!/usr/bin/env bash
source $HOME/dotfiles/bin/helpers/functions.sh
load_vscode_extensions() {
# Clear the array before populating it
arr=()
while IFS= read -r line; do
arr+=("$line")
done < <(jq -r '.[]' ~/dotfiles/vscode/extensions.json)
# Export the array
export extensionList=("${arr[@]}")
}
ensure_vscode_extensions_installed() {
if is_wsl; then
printfe "%s\n" "yellow" "Running in WSL, skipping VSCode extensions."
return
fi
# Load extensions list from jq in ~/dotfiles/vscode/extensions.json
load_vscode_extensions
for extension in "${extensionList[@]}"; do
result=$(code --list-extensions | grep -E "^${extension}$")
if [ -z "$result" ]; then
printfe "%s" "yellow" " - Installing $extension..."
code --install-extension $extension
if [ $? -ne 0 ]; then
printfe "%s\n" "red" " - Failed to install $extension"
exit 1
fi
printfe "%s\n" "green" " - Installed $extension"
else
printfe "%s\n" "green" " - $extension is already installed"
fi
done
}
print_vsc_status() {
printfe "%s" "cyan" "Checking VSCode extensions..."
clear_line
load_vscode_extensions
count_installed_extensions=0
# Loop through each extension and check if it's installed
for extension in "${extensionList[@]}"; do
result=$(code --list-extensions | grep -E "^${extension}$")
if [ -z "$result" ]; then
if [ "$verbose" = true ]; then
printfe "%s" "yellow" "Extension $extension is not installed\n"
fi
else
count_installed_extensions=$((count_installed_extensions + 1))
fi
done
if [ "$verbose" = true ]; then
printfe "%s\n" "yellow" "Expected extensions:"
for ext in "${extensionList[@]}"; do
printfe "%s\n" "blue" "$ext"
done
printfe "%s\n" "yellow" "Installed extensions:"
while IFS= read -r installed_ext; do
printfe "%s\n" "blue" "$installed_ext"
done < <(code --list-extensions)
fi
count=${#extensionList[@]}
printfe "%s" "cyan" "VSCode"
if [ $count_installed_extensions -eq $count ]; then
printfe "%s" "green" " $count_installed_extensions/$count "
else
printfe "%s" "red" " $count_installed_extensions/$count "
fi
printfe "%s\n" "cyan" "extensions installed"
}

View File

@ -1,72 +0,0 @@
#!/usr/bin/env python3
import requests
import os
import json
from datetime import datetime
def get_weather():
response = requests.get("https://wttr.in/beverwijk?n")
return response.text.strip()
def get_moon_phase():
response = requests.get("https://wttr.in/moon?format=F")
return response.text.strip()
def read_prompt(file_path):
with open(file_path, 'r') as file:
return file.read()
def get_hostname():
with open("/etc/hostname", 'r') as file:
return file.read().strip()
def replace_wildcards(prompt, weather, moon_phase, time, date, hostname):
prompt = prompt.replace("$TIME", time)
prompt = prompt.replace("$DATE", date)
prompt = prompt.replace("$HOSTNAME", hostname)
prompt = prompt.replace("$WEATHER", weather)
prompt = prompt.replace("$MOON_PHASE", moon_phase)
return prompt
def main():
home_directory = os.path.expanduser('~')
AI_ENDPOINT = "https://api.openai.com/v1/chat/completions"
OPENAI_API_KEY = ""
PROMPT_FILE_PATH = home_directory + "/dotfiles/bin/resources/welcome_prompt.txt"
# Load api key from disk
with open(home_directory + "/dotfiles/secrets/openai_api_key.secret", 'r') as file:
OPENAI_API_KEY = file.read().strip()
weather = get_weather()
moon_phase = get_moon_phase()
hostname = get_hostname()
time = datetime.now().strftime("%H:%M")
date = datetime.now().strftime("%A, %d %B %Y")
openai_prompt = read_prompt(PROMPT_FILE_PATH)
prompt = replace_wildcards(openai_prompt, weather, moon_phase, time, date, hostname)
data = {
"max_tokens": 200,
"messages": [
{"role": "system", "content": prompt},
],
"model": "gpt-4o-mini",
}
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {OPENAI_API_KEY}"
}
response = requests.post(AI_ENDPOINT, headers=headers, data=json.dumps(data))
response_data = response.json()
completion = response_data['choices'][0]['message']['content']
print(completion)
if __name__ == "__main__":
main()