From 3f5b980ddcaa652ab7a65e4b7c65fcf5d9032819 Mon Sep 17 00:00:00 2001 From: Menno van Leeuwen Date: Tue, 27 Aug 2024 15:58:09 +0200 Subject: [PATCH] wip --- bin/actions/update.sh | 21 ++++++++ bin/helpers/functions.sh | 66 ++++++++++++++--------- config/config.yaml | 15 ++++-- config/ssh/authorized_keys/mennos-desktop | 2 + config/ssh/authorized_keys/mennos-laptop | 2 + 5 files changed, 77 insertions(+), 29 deletions(-) create mode 100644 config/ssh/authorized_keys/mennos-desktop create mode 100644 config/ssh/authorized_keys/mennos-laptop diff --git a/bin/actions/update.sh b/bin/actions/update.sh index c0fbc1d..f665451 100755 --- a/bin/actions/update.sh +++ b/bin/actions/update.sh @@ -70,6 +70,11 @@ ensure_symlink() { source=$(cat $HOME/dotfiles/config/config.yaml | shyaml get-value config.symlinks.$1.source) &>/dev/null fi + # If this is still empty, last attempt, let's try use the hostname of the machine + if [ -z "$source" ]; then + source=$(cat $HOME/dotfiles/config/config.yaml | shyaml get-value config.symlinks.$1.sources.$(hostname)) &>/dev/null + fi + # If this is still empty, we can't continue and should throw up an error if [ -z "$source" ]; then printfe "%s\n" "red" " - No valid source defined for $1" @@ -77,6 +82,22 @@ ensure_symlink() { 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) + + 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 + fi + fi } symlinks() { diff --git a/bin/helpers/functions.sh b/bin/helpers/functions.sh index 66c396f..a457bef 100755 --- a/bin/helpers/functions.sh +++ b/bin/helpers/functions.sh @@ -157,41 +157,57 @@ add_to_hosts() { # $1: file to check # $2: link location check_or_make_symlink() { - if [ ! -L $1 ]; then - if [ -f $1 ]; then - mv $1 $1.bak - printfe "%s\n" "yellow" " - Backed up $1 to $1.bak" - fi - mkdir -p $(dirname $1) - ln -s $2 $1 - printfe "%s\n" "green" " - Created symlink $2 -> $1" - return - fi + SOURCE=$1 + TARGET=$2 - # Confirm the symlink that already exists point to the correct location - if [ -L $1 ]; then - if [ "$(readlink $1)" != $2 ]; then - printfe "%s\n" "yellow" " - Symlink $1 exists but points to the wrong location" - printfe "%s\n" "yellow" " Expected: $2" - printfe "%s\n" "yellow" " Actual: $(readlink $1)" + # Take any ~ and replace it with $HOME + SOURCE=$(echo $SOURCE | sed "s|~|$HOME|g") + TARGET=$(echo $TARGET | sed "s|~|$HOME|g") + + # 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 + 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" " Fixing symlink" - rm $1 - mkdir -p $(dirname $1) - ln -s $2 $1 - printfe "%s\n" "green" " Created symlink $2 -> $1" + rm $TARGET + mkdir -p $(dirname $TARGET) + ln -s $SOURCE $TARGET + printfe "%s\n" "green" " Created symlink $TARGET -> $SOURCE" return fi fi - if [ ! -L $1 ]; then - printfe "%s\n" "red" " - Failed to create symlink $2 -> $1" + # If target is a file and it's not a symlink, we should back it up + if [ -f $TARGET ] && [ ! -L $TARGET ]; then + printfe "%s\n" "yellow" " - File $TARGET exists, backing up and creating symlink" + 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 + printfe "%s" "green" " - OK: " + printfe "%-30s" "blue" "$SOURCE" + printfe "%s" "cyan" " -> " + printfe "%-30s\n" "blue" "$TARGET" return fi - printfe "%s" "green" " - OK: " - printfe "%-30s" "blue" "$1" + # Create the symlink + mkdir -p $(dirname $TARGET) + ln -s $SOURCE $TARGET + + # Check if the symlink was created successfully + if [ ! -L $TARGET ]; then + printfe "%s\n" "red" " - Failed to create symlink $TARGET -> $SOURCE" + return + fi + + printfe "%s" "green" " - Added new symlink: " + printfe "%-30s" "blue" "$SOURCE" printfe "%s" "cyan" " -> " - printfe "%-30s\n" "blue" "$2" + printfe "%-30s\n" "blue" "$TARGET" } clear_line() { diff --git a/config/config.yaml b/config/config.yaml index 80c8f38..9e9ba38 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -17,6 +17,14 @@ config: source: ~/dotfiles/config/ssh/config.d target: ~/.ssh/config.d + ssh_authorized_keys: + sources: + mennos-laptop: ~/dotfiles/config/ssh/authorized_keys/mennos-laptop + mennos-desktop: ~/dotfiles/config/ssh/authorized_keys/mennos-desktop + homeserver-pc: ~/dotfiles/config/ssh/authorized_keys/homeserver-pc + target: ~/.ssh/authorized_keys + chmod: 600 + # Zshrc zshrc: source: ~/dotfiles/.zshrc @@ -56,7 +64,7 @@ config: command: alacritty screenshot: shortcut: Shift + Alt + 4 - command: cosmic-screenshot --interactive + command: flameshot gui missioncenter: shortcut: Ctrl + Shift + Escape command: flatpak run io.missioncenter.MissionCenter @@ -122,10 +130,10 @@ config: fd-find: procs: bottom: - swhkd: + Simple-Wayland-HotKey-Daemon: git_url: https://github.com/waycrate/swhkd.git binary: Simple-Wayland-HotKey-Daemon - swhkd: + swhks: git_url: https://github.com/waycrate/swhkd.git binary: swhks @@ -151,7 +159,6 @@ config: - vim - sl - jq -# - just - libglvnd-dev - libwayland-dev - libseat-dev diff --git a/config/ssh/authorized_keys/mennos-desktop b/config/ssh/authorized_keys/mennos-desktop new file mode 100644 index 0000000..a7083d7 --- /dev/null +++ b/config/ssh/authorized_keys/mennos-desktop @@ -0,0 +1,2 @@ +# This is the authrorized_keys file for the user mennos-desktop +ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIM+sKpcREOUjwMMSzEWAso6830wbOi8kUxqpuXWw5gHr \ No newline at end of file diff --git a/config/ssh/authorized_keys/mennos-laptop b/config/ssh/authorized_keys/mennos-laptop new file mode 100644 index 0000000..26b28a2 --- /dev/null +++ b/config/ssh/authorized_keys/mennos-laptop @@ -0,0 +1,2 @@ +# This is the authrorized_keys file for the user mennos-laptop +ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIM+sKpcREOUjwMMSzEWAso6830wbOi8kUxqpuXWw5gHr \ No newline at end of file