chore: moves flatpaks back to flatrefs sinces it's more reliable

fix: symlinks now work properly in YAML even on different machines
This commit is contained in:
2024-08-27 18:22:18 +02:00
parent 3f5b980ddc
commit e0ae4b0736
30 changed files with 312 additions and 94 deletions

View File

@ -55,47 +55,51 @@ groups() {
}
ensure_symlink() {
source=$(cat $HOME/dotfiles/config/config.yaml | shyaml get-values config.symlinks.$1.source) &>/dev/null
target=$(cat $HOME/dotfiles/config/config.yaml | shyaml get-value config.symlinks.$1.target) &>/dev/null
local source
local target
# Based on the OS_TYPE, get the correct source
# Fetch target from YAML
target=$(shyaml get-value "config.symlinks.$1.target" < "$HOME/dotfiles/config/config.yaml") 2>/dev/null
# Fetch source from YAML based on OS
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
source=$(cat $HOME/dotfiles/config/config.yaml | shyaml get-value config.symlinks.$1.sources.linux) &>/dev/null
source=$(shyaml get-value "config.symlinks.$1.sources.linux" < "$HOME/dotfiles/config/config.yaml") 2>/dev/null
elif [[ "$OSTYPE" == "darwin"* ]]; then
source=$(cat $HOME/dotfiles/config/config.yaml | shyaml get-value config.symlinks.$1.sources.macos) &>/dev/null
source=$(shyaml get-value "config.symlinks.$1.sources.macos" < "$HOME/dotfiles/config/config.yaml") 2>/dev/null
fi
# Try adding a default source if the OS_TYPE specific source is empty
# Fall back to generic source if OS-specific source is empty
if [ -z "$source" ]; then
source=$(cat $HOME/dotfiles/config/config.yaml | shyaml get-value config.symlinks.$1.source) &>/dev/null
source=$(shyaml get-value "config.symlinks.$1.source" < "$HOME/dotfiles/config/config.yaml") 2>/dev/null
fi
# If this is still empty, last attempt, let's try use the hostname of the machine
# Attempt to use the hostname of the machine if source is still empty
if [ -z "$source" ]; then
source=$(cat $HOME/dotfiles/config/config.yaml | shyaml get-value config.symlinks.$1.sources.$(hostname)) &>/dev/null
source=$(shyaml get-value "config.symlinks.$1.sources.$(hostname)" < "$HOME/dotfiles/config/config.yaml") 2>/dev/null
fi
# If this is still empty, we can't continue and should throw up an error
# Error out if source is still empty
if [ -z "$source" ]; then
printfe "%s\n" "red" " - No valid source defined for $1"
return
fi
check_or_make_symlink $source $target
# Let's check if there was a chmod defined for the symlink
chmod=$(cat $HOME/dotfiles/config/config.yaml | shyaml get-value config.symlinks.$1.chmod 2>/dev/null)
# Expand ~ with $HOME
source="${source/#\~/$HOME}"
target="${target/#\~/$HOME}"
if [ -n "$chmod" ]; then
# Let's see if the current target has the correct chmod
current_chmod=$(stat -c %a $target)
if [ "$current_chmod" != "$chmod" ]; then
printfe "%s" "yellow" " - Changing chmod of $target to $chmod"
# Replace ~ with $HOME in target
target=$(echo $target | sed "s|~|$HOME|g")
chmod $chmod $target
else
return
# Call the function to check or make the symlink
check_or_make_symlink "$source" "$target"
# Check if there is a chmod defined for the target file
desired_chmod=$(shyaml get-value "config.symlinks.$1.chmod" < "$HOME/dotfiles/config/config.yaml" 2>/dev/null)
if [ -n "$desired_chmod" ]; then
# Check if the current source file has the correct chmod
current_chmod=$(stat -c %a "$source") # Check permissions of source file, since that's what chmod affects.
if [ "$current_chmod" != "$desired_chmod" ]; then
printfe "%s\n" "yellow" " - Changing chmod of $source to $desired_chmod"
chmod "$desired_chmod" "$source"
fi
fi
}
@ -167,11 +171,8 @@ pipxpkgs() {
}
flatpakpkgs() {
printfe "%s\n" "cyan" "Ensuring Flatpak remotes are added..."
source $HOME/dotfiles/bin/helpers/flatpak_packages.sh
ensure_remotes_added
printfe "%s\n" "cyan" "Ensuring Flatpak packages are installed..."
source $HOME/dotfiles/bin/helpers/flatpak_packages.sh
ensure_flatpak_packages_installed
}

View File

@ -2,28 +2,19 @@
source $HOME/dotfiles/bin/helpers/functions.sh
ensure_remotes_added() {
flatpak_remotes=($(cat $DOTFILES_CONFIG | shyaml get-values config.packages.flatpak.remotes))
for remote in $flatpak_remotes; do
printfe "%s\n" "green" " - Ensuring remote $remote"
flatpak remote-add --if-not-exists flathub $remote
done
}
ensure_flatpak_packages_installed() {
flatpak_packages=($(cat $DOTFILES_CONFIG | shyaml get-values config.packages.flatpak.apps))
flatpak_packages=($(ls $HOME/dotfiles/config/flatpaks/ | sed 's/.flatpakref//g'))
for package in $flatpak_packages; do
if ! flatpak list | grep -q $package; then
printfe "%s" "yellow" " - Installing $package"
result=$(flatpak install --user -y flathub $package 2>&1)
printfe "%s\n" "cyan" " - Installing $package..."
flatpak install -y flathub $package
if [ $? -ne 0 ]; then
printfe "%s\n" "red" "Failed to install $package: $result"
if [ $? -eq 0 ]; then
printfe "%s\n" "green" " - $package installed successfully"
else
printfe "%s\n" "red" " - $package failed to install"
fi
clear_line
printfe "%s\n" "green" " - $package installed"
else
printfe "%s\n" "green" " - $package is already installed"
fi
@ -34,7 +25,7 @@ print_flatpak_status() {
printfe "%s" "cyan" "Checking Flatpak packages..."
clear_line
flatpak_packages=($(cat $DOTFILES_CONFIG | shyaml get-values config.packages.flatpak.apps))
flatpak_packages=($(ls $HOME/dotfiles/config/flatpaks/ | sed 's/.flatpakref//g'))
count=$(echo $flatpak_packages | wc -w)
installed=0

View File

@ -151,42 +151,44 @@ add_to_hosts() {
fi
}
# Function to check if $1 is a path to a symlink, if not it will make a symlink to $2
# In case there's a file at the location of the symlink, it will backup the file
# Parameters
# $1: file to check
# $2: link location
resolve_path() {
echo "$(cd "$(dirname "$1")"; pwd)/$(basename "$1")"
}
check_or_make_symlink() {
SOURCE=$1
TARGET=$2
SOURCE="$1"
TARGET="$2"
# Take any ~ and replace it with $HOME
SOURCE=$(echo $SOURCE | sed "s|~|$HOME|g")
TARGET=$(echo $TARGET | sed "s|~|$HOME|g")
SOURCE="${SOURCE/#\~/$HOME}"
TARGET="${TARGET/#\~/$HOME}"
SOURCE=$(resolve_path "$SOURCE")
TARGET=$(resolve_path "$TARGET")
# 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
if [ -L "$TARGET" ]; then
if [ "$(readlink "$TARGET")" != "$SOURCE" ]; then
printfe "%s\n" "yellow" " - Symlink $TARGET exists but points to the wrong location"
printfe "%s\n" "yellow" " Expected: $SOURCE"
printfe "%s\n" "yellow" " Actual: $(readlink $TARGET)"
printfe "%s\n" "yellow" " Actual: $(readlink "$TARGET")"
printfe "%s\n" "yellow" " Fixing symlink"
rm $TARGET
mkdir -p $(dirname $TARGET)
ln -s $SOURCE $TARGET
rm "$TARGET"
mkdir -p "$(dirname "$TARGET")"
ln -s "$SOURCE" "$TARGET"
printfe "%s\n" "green" " Created symlink $TARGET -> $SOURCE"
return
fi
fi
# If target is a file and it's not a symlink, we should back it up
if [ -f $TARGET ] && [ ! -L $TARGET ]; then
if [ -f "$TARGET" ] && [ ! -L "$TARGET" ]; then
printfe "%s\n" "yellow" " - File $TARGET exists, backing up and creating symlink"
mv $TARGET $TARGET.bak
mv "$TARGET" "$TARGET.bak"
fi
# If the target is already a symlink, and it points to the correct location, we should return and be happy
if [ -L $TARGET ]; then
if [ -L "$TARGET" ]; then
printfe "%s" "green" " - OK: "
printfe "%-30s" "blue" "$SOURCE"
printfe "%s" "cyan" " -> "
@ -195,11 +197,11 @@ check_or_make_symlink() {
fi
# Create the symlink
mkdir -p $(dirname $TARGET)
ln -s $SOURCE $TARGET
mkdir -p "$(dirname "$TARGET")"
ln -s "$SOURCE" "$TARGET"
# Check if the symlink was created successfully
if [ ! -L $TARGET ]; then
if [ ! -L "$TARGET" ]; then
printfe "%s\n" "red" " - Failed to create symlink $TARGET -> $SOURCE"
return
fi