refactor: v3

This commit is contained in:
Didier Slof 2023-02-19 21:02:08 +01:00
parent f05a04bf19
commit 0d8090e3c8
Signed by: didier
GPG key ID: 01E71F18AA4398E5
26 changed files with 55 additions and 242 deletions

View file

@ -1,15 +1,9 @@
# Dotfiles (v2)
# Faulty's Dotfiles
Version: 3
This repo has a simple sync script with a few features:
- file sync (not symlinked anymore)
- package install
- after install commands
- deploy script
---
## Deployment
To import the dotfiles run:
`curl --proto '=https' --tlsv1.2 -sSf https://git.faulty.nl/didier/dotfiles/-/raw/main/lib/deploy.sh | sh`
## Notes
- `gpackage.list`
This is a list with packages only installed if the `-g` or `--graphical` flag is used.
## file sync
files in `./files` are synced across the system.
files in `./files/home` are synced to the home of the user running the script.
files in `./files/root` are synced with sudo to the root of the script.

View file

@ -1,6 +0,0 @@
^. ./sync.conf && $PM_COMMAND $PM_UPGRADE $PM_NOCONFIRM || printf ""
^[ -d /tmp/yay ] && rm -rf /tmp/yay || printf ""
command -v makepkg && (command -v yay || (git clone https://aur.archlinux.org/yay /tmp/yay && (cd /tmp/yay && makepkg --noconfirm -si))) || printf ""
^[ -d /tmp/pfetch ] && rm -rf /tmp/pfetch || printf ""
^(git clone https://github.com/dylanaraps/pfetch.git /tmp/pfetch && cd /tmp/pfetch && (chmod a+x ./pfetch && $SYS_SUDO mv ./pfetch /usr/local/pfetch)) || printf ""
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended || echo "zsh not installed"

21
comp.sh
View file

@ -1,21 +0,0 @@
#!/bin/sh
# #################### #
# Compatibility Script #
# #################### #
if [ ! -z "$1" ]; then
cmd=$1; shift
fi
case $cmd in
debian|ubuntu|apt|deb)
PM_COMMAND=apt \
PM_NOCONFIRM=-y \
PM_INSTALL=install \
PM_UPDATE=update \
PM_UPGRADE=upgrade \
PM_REMOVE=purge \
./sync.sh $@
;;
*|''|arch) ./sync.sh $cmd $@ ;;
esac

View file

@ -1,4 +1,3 @@
alacritty
sway
wlogout
kanshi

View file

@ -1,7 +0,0 @@
FROM python:3.10-alpine
RUN apk add git
RUN git clone https://git.faulty.nl/didier/dotfiles /app
WORKDIR /app
ENTRYPOINT ["python3", "/app/lib/docker.py"]

View file

@ -1,18 +0,0 @@
#!/bin/sh
REPO_LIST="https://git.faulty.nl/didier/dotfiles"
DOTFILES_DIR="$HOME/.local/dotfiles"
clone() {
for repo in $REPO_LIST; do
git clone "$repo" "$DOTFILES_DIR" && return 0
done
}
main() {
[ -d "$DOTFILES_DIR" ] || clone
cd "$DOTFILES_DIR" || exit 1
./comp.sh "$@"
}
main "$@"

View file

@ -1,31 +0,0 @@
# any HTTP request returns the contents of: ./lib/deploy.sh
# If the deploy script is older then a day, do a git pull
from http.server import BaseHTTPRequestHandler, HTTPServer
import subprocess
import os
import time
class handler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type','text/plain')
self.end_headers()
if (time.time() - os.path.getmtime('./lib/deploy.sh') > 86400) or self.path == '/pull':
subprocess.call(['git', 'pull'])
with open('./lib/deploy.sh') as f:
self.wfile.write(bytes(f.read(), 'utf-8'))
self.wfile.write(bytes("# Git pull: " + str(time.time() - os.path.getmtime('./lib/deploy.sh')) + " seconds ago", 'utf-8'))
def main():
try:
server = HTTPServer(('', 80), handler)
print('started httpserver...')
server.serve_forever()
except KeyboardInterrupt:
print('^C received, shutting down the web server')
server.socket.close()
if __name__ == '__main__':
main()

View file

@ -1,32 +0,0 @@
#!/bin/sh
. ./sync.conf || . ../sync.conf
DRY_RUN=${DRY_RUN:-no}
bool() {
case "$1" in
1|true|yes|on|TRUE|YES|ON) return 0 ;;
*) return 1 ;;
esac
}
run() {
# Check if command starts with NO_RUN ($*) and remove it
if echo "$*" | grep -q "^NO_RUN"; then
return 0
fi
if bool "$DRY_RUN"; then
echo "DRY: + $*"
else
echo "+ $*"
"$@"
fi
}
is_graphical() {
command -v $PREF_TERM > /dev/null && return 0 || return 1
}
command -v $SYS_SUDO > /dev/null || SYS_SUDO="NO_RUN"

View file

@ -0,0 +1,6 @@
#!/bin/sh
# This file only installs what is needed for the sync to perform...
$SUDO $PM $PM_INSTALL $PM_NOCONFIRM \
rsync

8
scripts/10-copy-files.sh Normal file
View file

@ -0,0 +1,8 @@
#!/bin/sh
HOME_FILES=${HOME_FILES:-./files/home}
ROOT_FILES=${ROOT_FILES:-./files/root}
# overwrite files but preserve files surrounding
rsync -av $HOME_FILES/ $HOME
$SUDO rsync -av $ROOT_FILES/ /

View file

@ -0,0 +1,23 @@
#!/bin/sh
PACKAGE_LIST=${PACKAGE_LIST:-./package.list}
GRAPHICAL_PACKAGE_LIST=${GRAPHICAL_PACKAGE_LIST:-./gpackage.list}
_APPLICATIONS=""
while read -r pkg; do
[ -z "$pkg" ] && continue
[ "$(echo "$pkg" | cut -c1)" = "#" ] && continue
_APPLICATIONS="$_APPLICATIONS $pkg"
done < "$PACKAGE_LIST"
if [ "${GRAPHICAL:-no}" = "yes" ]; then
if [ -f "$GRAPHICAL_PACKAGE_LIST" ]; then
while read -r pkg; do
[ -z "$pkg" ] && continue
[ "$(echo "$pkg" | cut -c1)" = "#" ] && continue
_APPLICATIONS="$_APPLICATIONS $pkg"
done < "$GRAPHICAL_PACKAGE_LIST"
fi
fi
$SUDO $PM $PM_INSTALL $PM_NOCONFIRM $_APPLICATIONS

View file

@ -1,16 +0,0 @@
# PM - Package Manager Options
PM_COMMAND=${PM_COMMAND:-pacman}
PM_NOCONFIRM=${PM_NOCONFIRM:-"--noconfirm"}
PM_INSTALL=${PM_INSTALL:--S}
PM_REMOVE=${PM_REMOVE:-R}
PM_UPDATE=${PM_UPDATE:--Sy}
PM_UPGRADE=${PM_UPGRADE:--Syyu}
# SYS - System Options
SYS_SUDO=${SYS_SUDO:-"sudo"}
SYS_ROOT=${SYS_ROOT:-"/"}
# PREF - Preferences
PREF_TERM=${PREF_TERM:-alacritty}
PREF_TERM_SET_TITLE=${PREF_TERM_SET_TITLE:--t}
PREF_TERM_RUN_CMD=${PREF_TERM_RUN_CMD:--e}

108
sync.sh
View file

@ -1,104 +1,18 @@
#!/bin/sh
SYNC_FILES=${SYNC_FILES:-yes}
INSTALL_PKGS=${INSTALL_PKGS:-yes}
RUN_CMDS=${RUN_CMDS:-yes}
PULL_GIT=${PULL_GIT:-yes}
TIME_TRACK=${TIME_TRACK:-no}
SUDO=${SUDO:-sudo}
ARGS="$@"
. ./lib/lib.sh
set -e # Exit on error
_install_pkgs() {
run $SYS_SUDO $PM_COMMAND $PM_UPDATE $PM_NOCONFIRM
_APPLICATIONS=""
while read -r pkg; do
[ -z "$pkg" ] && continue
[ "$(echo "$pkg" | cut -c1)" = "#" ] && continue
_APPLICATIONS="$_APPLICATIONS $pkg"
done <"$1"
run $SYS_SUDO $PM_COMMAND $PM_INSTALL $PM_NOCONFIRM $_APPLICATIONS
}
_sync_files() {
#copy all files and folders from ./files to /
#except for ./files/%HOME
for item in ./files/*; do
[ "$item" = "./files/%HOME" ] && continue
# e.g. ./files/etc -> /etc
run mkdir -p "$SYS_ROOT/$(basename "${item#./files/}")"
run $SYS_SUDO cp -r "$item/." "$SYS_ROOT/${item#./files/}"
done
}
_run_cmds() {
while read -r cmd; do
if [ "$(echo "$cmd" | cut -c1)" = "^" ]; then
run $SYS_SUDO sh -c "$(echo "$cmd" | cut -c2-)"
else
run sh -c "$cmd"
fi
done <./command.list
}
ARGS="$@"
_skip_next_arg="n"
arg_handler() {
for arg in $ARGS; do
bool $_skip_next_arg && continue
case $arg in
+files) SYNC_FILES=yes ;;
-files) SYNC_FILES=no ;;
+pkgs) INSTALL_PKGS=yes ;;
-pkgs) INSTALL_PKGS=no ;;
+cmds) RUN_CMDS=yes ;;
-cmds) RUN_CMDS=no ;;
+git) PULL_GIT=yes ;;
-git) PULL_GIT=no ;;
+) SYNC_FILES=yes; INSTALL_PKGS=yes; RUN_CMDS=yes; PULL_GIT=yes ;;
-) SYNC_FILES=no; INSTALL_PKGS=no; RUN_CMDS=no; PULL_GIT=no ;;
+*)
echo "# Unknown argument: $arg"
exit 1
;;
-*)
echo "# Unknown argument: $arg"
exit 1
;;
esac
done
}
PM=${PM:-pacman}
PM_INSTALL=${PM_INSTALL:--S}
PM_NOCONFIRM=${PM_NOCONFIRM:---noconfirm}
main() {
arg_handler
echo "# hook: PULL_GIT"
bool "$PULL_GIT" && run git pull
echo "# hook: SYNC_FILES (/)"
bool "$SYNC_FILES" && run cp -a "./files/%HOME/." "$HOME"
echo "# hook: SYNC_FILES"
bool "$SYNC_FILES" && _sync_files
echo "# hook: INSTALL_PKGS"
bool "$INSTALL_PKGS" && _install_pkgs ./package.list
echo "# hook: RUN_CMDS"
bool "$RUN_CMDS" && _run_cmds
return 0
for script in ./scripts/* ; do
echo "# exec $script"
. $script
done
}
echo "# exec: start."
if bool $TIME_TRACK; then
time main "$@"
else
main "$@"
fi
echo "# exec: done."
main "$@"
echo "# OK"