syncr tool #2

Merged
strix merged 3 commits from syncr into main 2025-03-04 22:34:49 +01:00
62 changed files with 2385 additions and 414 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
.data/
target/

View file

@ -1,12 +0,0 @@
# Raine's Dotfiles
## Crates
Everything is a crate.
If something is distro specific you should follow the following naming scheme:
`crate.<DISTRO>.sh`
## Using it
Just use the script tbh
```sh
curl -L https://via.ixvd.net/sh | sh
```

View file

@ -1,12 +0,0 @@
super_apply() {
pacman -Syyu --noconfirm
pacman -S --needed --noconfirm sudo reflector
if ! grep -q "Reflector" /etc/pacman.d/mirrorlist; then
cp /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist.bak
reflector -c NL -f 10 --threads 4 --save /etc/pacman.d/mirrorlist
else
echo "err: reflector already executed -- skipping..."
fi
cp files/pacman.conf /etc/pacman.conf
}

View file

@ -1,5 +0,0 @@
super_apply() {
apt update -y
apt install -y netselect-apt sudo
netselect-apt
}

View file

@ -1,11 +0,0 @@
#!/bin/sh
describe="Install stuff on the system!"
scripts="@distro @self"
super_apply() {
if [ -f /usr/lib/security/pam_wheel.so ] && ! grep -q "# pam_wheel.so added" /etc/pam.d/su; then
echo "auth sufficient pam_wheel.so trust use_uid" > /etc/pam.d/su
echo "# pam_wheel.so added" > /etc/pam.d/su
fi
}

View file

@ -1,31 +0,0 @@
[options]
HoldPkg = pacman glibc yay
Architecture = auto
Color
CheckSpace
ParallelDownloads = 5
SigLevel = Required DatabaseOptional
LocalFileSigLevel = Optional
#[testing]
#Include = /etc/pacman.d/mirrorlist
[core]
Include = /etc/pacman.d/mirrorlist
[extra]
Include = /etc/pacman.d/mirrorlist
#[community-testing]
#Include = /etc/pacman.d/mirrorlist
[community]
Include = /etc/pacman.d/mirrorlist
#[multilib-testing]
#Include = /etc/pacman.d/mirrorlist
[multilib]
Include = /etc/pacman.d/mirrorlist

View file

@ -1,18 +0,0 @@
#!/bin/sh
describe="setup ssh"
apply() {
[ -e "$HOME/.ssh/authorized_keys" ] || ln files/authorized_keys $HOME/.ssh/authorized_keys
[ -e "$HOME/.ssh/config" ] || ln files/config $HOME/.ssh/config
if ! [ -f "$HOME/.ssh/id_rsa" ]; then
echo "Creating new ssh key for this device..."
ssh-keygen -f $HOME/.ssh/id_rsa -p "" -q
echo "Adding key to authorized_keys..."
cat $HOME/.ssh/id_rsa.pub > $HOME/.ssh/authorized_keys
fi
}
undo() {
echo "Undoing ssh keys is not supported, please do this manually."
}

View file

@ -1 +0,0 @@
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCeNQfnbyyF3sht43vH5BcXDPca8nWu6bKPVGvAlWBOq4Av8ME2IQgwVe9nJ05r73ZY02/Vdqc01a8wyK5Hmw0XlPL0Cn6wc9QoiscOvq5lMUK87S2tr3EVLGkgl8o7nmVuWgLewyojiORjM02P1PZEiFhKPXVEQFxU0dFz9QtpAdm0u78Xn2HTukHpXSv44R3XDDMFZ3Ek/XRuS6J9dZVxGkgCLQhK8kpfbxuiYxaRC7MHgGlYuxjLuZ6P4i+V+SSSShfCGdm6U9bgeIAwftN6a8Pc9+OsBeZGSUrGjZjRlD35q0a7fbpoS8pKTfbwgf/ijYeu3JmAQUlY+H959mIpg4H9XOgRrKVJSYwx5/BGuhmWgVy6HIYpXCQfEbLE7QDmwC2C430KzAH6jCcrRNyurIUCuO4iq9dwoQTzboMccOK79S2Z+1B5fYgS3BZgaiTUBSME2G2FriM6utgleiBnvFu/p7oH2I8ZHL/aVcSWAw0gbzsr7ADywAuiDNZk18c= strix@ryuk

View file

@ -1,11 +0,0 @@
#!/bin/sh
pkgs="i3 i3lock i3status libpulse brightnessctl xss-lock dex maim dmenu gnome-keyring feh picom"
super_apply() {
pacman -S --needed --noconfirm $pkgs
}
super_undo() {
pacman -R --noconfirm $pkgs
}

View file

@ -1,11 +0,0 @@
#!/bin/sh
pkgs="i3 i3lock i3status libpulse-mainloop-glib brightnessctl xss-lock dex maim dmenu gnome-keyring feh picom"
super_apply() {
apt install -y $pkgs
}
super_undo() {
apt remove -y $pkgs
}

View file

@ -1,26 +0,0 @@
#!/bin/sh
describe="Installs i3"
scripts="@distro @self"
apply() {
[ -d "$HOME/.config/i3" ] || mkdir -p $HOME/.config/i3
[ -d "$HOME/.config/i3status" ] || mkdir -p $HOME/.config/i3status
[ -e "$HOME/.config/i3/config" ] || ln files/config $HOME/.config/i3/config
[ -e "$HOME/.config/i3status/config" ] || ln files/status_config $HOME/.config/i3status/config
[ -e "$HOME/.config/picom.conf" ] || ln files/picom.conf $HOME/.config/picom.conf
}
undo() {
rm $HOME/.config/i3/config
rm $HOME/.config/i3status/config
}
super_apply() {
[ -d "/etc/X11/xorg.conf.d" ] || mkdir -p /etc/X11/xorg.conf.d/
cp files/40-proper-touchpad.conf /etc/X11/xorg.conf.d/40-proper-touchpad.conf
}
super_undo() {
rm /etc/X11/xorg.conf.d/40-proper-touchpad.conf
}

View file

@ -1,12 +0,0 @@
#!/bin/sh
pkgs="i3 i3lock i3status pulseaudio-devel brightnessctl xss-lock dex maim dmenu gnome-keyring feh picom"
super_apply() {
xbps-install -y $pkgs
}
super_undo() {
xbps-remove -y $pkgs
}

View file

@ -1,7 +0,0 @@
super_apply() {
pacman -S --needed --noconfirm zsh
}
super_undo() {
pacman -R --noconfirm zsh
}

View file

@ -1,7 +0,0 @@
super_apply() {
apt install -y zsh
}
super_undo() {
apt remove -y zsh
}

View file

@ -1,7 +0,0 @@
super_apply() {
apt install -y zsh
}
super_undo() {
apt remove -y zsh
}

View file

@ -1,31 +0,0 @@
#!/bin/sh
describe="Install zsh and oh-my-zsh!"
scripts="@distro @self"
super_apply() {
usermod $USER --shell /bin/zsh
}
super_undo() {
usermod $USER --shell /bin/bash
}
apply() {
if [ ! -d "$HOME/.oh-my-zsh" ]; then
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended
PL_DIR=${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
[ -d "$PL_DIR" ] || git clone https://github.com/zsh-users/zsh-autosuggestions $PL_DIR
PL_DIR=${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
[ -d "$PL_DIR" ] || git clone https://github.com/zsh-users/zsh-syntax-highlighting.git $PL_DIR
fi
[ -f "$HOME/.zshrc" ] && unlink $HOME/.zshrc
[ -e "$HOME/.zshrc" ] || ln files/.zshrc $HOME/.zshrc
}
undo() {
unlink $HOME/.zshrc
rm -rf $HOME/.oh-my-zsh
}

View file

@ -1,10 +0,0 @@
#!/bin/sh
super_apply() {
xbps-install -y zsh
}
super_undo() {
xbps-remove -y zsh
}

View file

@ -1,7 +0,0 @@
super_apply() {
pacman -S --needed --noconfirm neovim
}
super_undo() {
echo "we never uninstall vim -_-"
}

View file

@ -1,7 +0,0 @@
super_apply() {
apt install -y neovim
}
super_undo() {
echo "we never uninstall vim -_-"
}

View file

@ -1,15 +0,0 @@
#!/bin/sh
describe="Install vim and it's stuff"
scripts="@distro @self"
apply() {
[ -d "$HOME/.config/nvim" ] || mkdir -p "$HOME/.config/nvim"
[ -e "$HOME/.config/nvim/init.vim" ] || ln files/init.vim $HOME/.config/nvim/init.vim
[ -e "$HOME/.ideavimrc" ] || ln files/.ideavimrc $HOME/.ideavimrc
}
undo() {
unlink $HOME/.config/nvim/init.vim
unlink $HOME/.ideavimrc
}

View file

@ -1,7 +0,0 @@
super_apply() {
xbps-install -y neovim
}
super_undo() {
echo "we never uninstall vim -_-"
}

View file

@ -1,7 +0,0 @@
super_apply() {
pacman -S --needed --noconfirm alacritty
}
super_undo() {
pacman -R --noconfirm alacritty
}

View file

@ -1,7 +0,0 @@
super_apply() {
apt install -y alacritty
}
super_undo() {
apt remove -y alacritty
}

View file

@ -1,13 +0,0 @@
#!/bin/sh
describe="Installs alacritty and configs"
scripts="@distro @self"
apply() {
[ -d "$HOME/.config/alacritty" ] || mkdir -p $HOME/.config/alacritty
[ -e "$HOME/.config/alacritty/alacritty.yml" ] || ln files/alacritty.yml $HOME/.config/alacritty/alacritty.yml
}
undo() {
unlink $HOME/.config/alacritty/alacritty.yml
}

View file

@ -1,7 +0,0 @@
super_apply() {
xbps-install -y alacritty
}
super_undo() {
xbps-remove -y alacritty
}

View file

@ -1,6 +0,0 @@
cursor:
style:
shape: 'Block'
blinking: 'On'
blink_interval: 500

View file

@ -1,9 +0,0 @@
#!/bin/sh
describe="Setup git"
scripts="@distro @self"
apply() {
git config --global user.name Strix
git config --global user.email strix@saluco.nl
}

28
crates/common/crate.toml Normal file
View file

@ -0,0 +1,28 @@
[crate]
name = "common"
description = "A versatile crate for managing dotfiles and system configurations."
[[packages]]
name = "git"
description = "Version control system"
[[packages]]
name = "nvim"
description = "Text editor for creating and editing files"
distro_name_mapping = { pacman = "neovim" }
[[packages]]
name = "zsh"
description = "Shell designed for interactive use"
[[actions.command]]
user = "root"
command = "sh ./scripts/pam_wheel.sh"
description = "Pam wheel setup"
[[actions.command]]
command = "git config --global user.name Strix && git config --global user.email strix@saluco.nl"
description = "git setup"
[metadata]
repository = "https://git.saluco.nl/strix/dotfiles"

View file

@ -0,0 +1,4 @@
if [ -f /usr/lib/security/pam_wheel.so ] && ! grep -q "# pam_wheel.so added" /etc/pam.d/su; then
echo "auth sufficient pam_wheel.so trust use_uid" > /etc/pam.d/su
echo "# pam_wheel.so added" > /etc/pam.d/su
fi

62
crates/i3/crate.toml Normal file
View file

@ -0,0 +1,62 @@
[crate]
name = "i3"
description = "install and setup i3"
[[packages]]
name = "i3"
[[packages]]
name = "i3lock"
[[packages]]
name = "i3status"
[[packages]]
name = "libpulse"
require = ["distro:arch"]
[[packages]]
name = "brightnessctl"
[[packages]]
name = "xss-lock"
[[packages]]
name = "dex"
[[packages]]
name = "maim"
[[packages]]
name = "dmenu"
[[packages]]
name = "gnome-keyring"
[[packages]]
name = "feh"
[[packages]]
name = "picom"
[[actions.link]]
src = "files/i3config"
dest = "~/.config/i3/config"
[[actions.link]]
src = "files/i3status_config"
dest = "~/.config/i3status/config"
[[actions.link]]
src = "files/picom.conf"
dest = "~/.config/picom.conf"
[[actions.command]]
user = "root"
command = "[ -d '/etc/X11/xorg.conf.d' ] || mkdir -p /etc/X11/xorg.conf.d/"
description = "ensure /etc/X11/xorg.conf.d"
[[actions.command]]
user = "root"
command = "cp files/touchpad.conf /etc/X11/xorg.conf.d/40-touchpad.conf"
description = "copy touchpad config to /etc/X11/xorg.conf.d"

View file

@ -1,5 +1,5 @@
#######################
## Raine's i3 config ##
## Strix' i3 config ##
## Mar 22, 2023 ##
## mutation: 1m ##
#######################

10
crates/ssh/crate.toml Normal file
View file

@ -0,0 +1,10 @@
[crate]
name = "ssh"
description = "fixes the ssh files"
[[actions.command]]
command = "ls -lah"
[[actions.link]]
src = "./config"
dest = "~/.ssh/config"

15
crates/vim/crate.toml Normal file
View file

@ -0,0 +1,15 @@
[crate]
name = "vim"
description = "install & configure vim"
[[packages]]
name = "neovim"
[[actions.link]]
src = "./ideavimrc"
dest = "~/.ideavimrc"
[[actions.link]]
src = "./init.vim"
dest = "~/.config/nvim/init.vim"

14
crates/zsh/crate.toml Normal file
View file

@ -0,0 +1,14 @@
[crate]
name = "zsh"
description = "install & configure zsh"
[[packages]]
name = "zsh"
[[actions.command]]
command = "sh ./setup-omzsh.sh"
description = "oh-my-zsh setup"
[[actions.link]]
src = "./zshrc"
dest = "~/.zshrc"

10
crates/zsh/setup-omzsh.sh Normal file
View file

@ -0,0 +1,10 @@
if [ ! -d "$HOME/.oh-my-zsh" ]; then
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended
PL_DIR=${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
[ -d "$PL_DIR" ] || git clone https://github.com/zsh-users/zsh-autosuggestions $PL_DIR
PL_DIR=${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
[ -d "$PL_DIR" ] || git clone https://github.com/zsh-users/zsh-syntax-highlighting.git $PL_DIR
fi
[ -f "$HOME/.zshrc" ] && unlink $HOME/.zshrc

89
dot
View file

@ -1,89 +0,0 @@
#!/bin/sh
ask=0
is_function() {
type "$1" 2>/dev/null | sed "s/$1//" | grep -qwi function
}
curr_distro() {
cat /etc/os-release | grep -G "^ID=" | sed 's/ID=//'
}
include() {
[ -f "$1" ] || return 1
. $1
return 0
}
func() {
if is_function $(echo "super_$2"); then
[ "${DO_SUDO:-yes}" = "yes" ] || return 0
ecmd=". $1 && super_$2"
[ "$(id -u)" = "0" ] && sh -c "$ecmd" || sudo sh -c "$ecmd"
unset ecmd
fi
is_function $2 && $2
}
# only run this *in crate dir*
run_crate() {
enabled=1
include ./crate.sh || exit 1
if [ -n "$describe" ]; then
echo "desc($(basename $PWD)): $describe"
fi
if [ $enabled -ne 1 ]; then
return
fi
cmd=$1
scripts=${scripts:-"@self @distro"}
for s in $scripts; do
echo "exec($(basename $PWD)): $s/$cmd"
case $s in
@self)
func ./crate.sh $cmd
;;
@distro)
unset -f super_$cmd
unset -f $cmd
include ./crate.$(curr_distro).sh
func ./crate.$(curr_distro).sh $cmd
unset -f super_$cmd
unset -f $cmd
include ./crate.sh
;;
*)
sh $s
;;
esac
done
}
echo "# details:"
echo "# user: $USER (${UID:-$(id -u)})"
echo "# groups: $(groups)"
echo "# distro: $(curr_distro)"
cmd=$1
if [ -z "$cmd" ]; then
echo "usage: $0 <command> [...args]"
exit 1
else
shift
fi
for c in ${@:-$(ls crates)}; do
[ -d crates/*$c ] || exit 1
cd crates/*$c
case $cmd in
a | apply)
run_crate apply
;;
u | undo)
run_crate undo
;;
*) exit 1 ;;
esac
cd ../..
done

View file

@ -1,14 +0,0 @@
#!/bin/sh
# this script is meant to be ran when .dotfiles is not present.
#
# example:
# curl https://git.saluco.nl/strix/dotfiles/raw/branch/main/remote_script.sh | sh
set -e
HOME=${HOME:-/home/${USER:-$(whomai)}}
[ -d "$HOME/.dotfiles" ] || git clone https://git.saluco.nl/strix/dotfiles $HOME/.dotfiles
cd $HOME/.dotfiles
./dot a

View file

@ -1,13 +0,0 @@
#!/bin/sh
set -e
echo "Downloading newest package..."
curl -Lo /tmp/discord.deb "https://discord.com/api/download?platform=linux&format=deb"
cd /tmp
echo "Extracting package..."
xdeb discord.deb
echo "Installing package..."
sudo xbps-install -R /tmp/binpkgs discord
rm -rf /tmp/binpkgs
cd -

1332
sync-runner/Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

23
sync-runner/Cargo.toml Normal file
View file

@ -0,0 +1,23 @@
[package]
name = "sync-runner"
version = "0.1.0"
edition = "2021"
[dependencies]
clap = { version = "4.5.29", features = ["derive"] }
colored = "3.0.0"
dbus = "0.9.7"
execute = "0.2.13"
fern = "0.7.1"
git2 = "0.20.0"
glob = "0.3.2"
hex = "0.4.3"
lazy_static = "1.5.0"
log = "0.4.26"
regex = "1.11.1"
resolve-path = "0.1.0"
serde = { version = "1.0.218", features = ["serde_derive"] }
sha2 = "0.10.8"
shellexpand = "3.1.0"
toml = "0.8.20"
whoami = "1.5.2"

10
sync-runner/src/action.rs Normal file
View file

@ -0,0 +1,10 @@
use clap::Parser;
#[derive(Parser, Debug)]
pub enum Action {
/// Sync your device with dotfiles repository
Sync {
#[arg(short, long)]
config_path: Option<String>,
},
}

View file

@ -0,0 +1,7 @@
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
pub struct Daemon {
/// interval in minutes
pub interval: u64,
}

View file

@ -0,0 +1,21 @@
#![allow(unused)]
use std::{collections::HashMap, fs::read_to_string};
use serde::{Deserialize, Serialize};
mod daemon;
#[derive(Serialize, Deserialize, Debug)]
pub struct Config {
pub title: String,
pub daemon: daemon::Daemon,
pub source: HashMap<String, crate::source::Source>
}
impl Config {
pub fn parse(path: &str) -> Result<Config, Box<dyn std::error::Error>> {
Ok(toml::from_str::<Config>(&read_to_string(path)?)?)
}
}

View file

@ -0,0 +1,102 @@
use std::{
collections::HashMap,
fs::{self, remove_file},
process::{Command, Stdio},
time::Duration,
};
use dbus::{
arg::Variant,
blocking::{BlockingSender, Connection},
Message,
};
use log::{debug, error, info, trace};
use resolve_path::PathResolveExt;
use serde::{Deserialize, Serialize};
use crate::tags::Tag;
#[derive(Serialize, Deserialize, Debug)]
pub struct Actions {
/// command actions
#[serde(rename = "command")]
pub commands: Option<Vec<CommandAction>>,
/// link actions
#[serde(rename = "link")]
pub links: Option<Vec<LinkAction>>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct CommandAction {
#[serde(default = "whoami::username")]
user: String,
command: String,
pub description: Option<String>,
pub require: Option<Vec<Tag>>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct LinkAction {
pub src: String,
pub dest: String,
overwrite: Option<bool>,
}
impl CommandAction {
pub fn new<S: Into<String>>(command: S, user: S) -> Self {
Self {
command: command.into(),
user: user.into(),
description: None,
require: None,
}
}
pub fn run(&self) -> Result<i32, Box<dyn std::error::Error>> {
trace!("running \"{}\" as {}...", &self.command, &self.user);
if self.user != whoami::username() {
Ok(Command::new("sudo")
.arg("-u")
.arg(&self.user)
.arg("--")
.arg("sh")
.arg("-c")
.arg(&self.command)
.status()?
.code()
.unwrap_or(1))
} else {
Ok(Command::new("sh")
.arg("-c")
.arg(&self.command)
.status()?
.code()
.unwrap_or(1))
}
}
}
impl LinkAction {
pub fn link(&self) -> Result<(), Box<dyn std::error::Error>> {
trace!(
"linking from {:?} to {:?}...",
&self.src.resolve(),
&self.dest.resolve()
);
if let Ok(existing) = fs::read_link(&self.dest.resolve()) {
if existing == self.src.resolve() {
debug!("link OK");
return Ok(());
} else {
if self.overwrite.unwrap_or(false) == true {
debug!("removing {}...", self.dest);
remove_file(self.dest.resolve())?;
} else {
return Err("Destination is linked to a different path".into());
}
}
}
std::os::unix::fs::symlink(&self.src.resolve(), &self.dest.resolve())?;
Ok(())
}
}

View file

@ -0,0 +1,27 @@
use std::fmt::Display;
use serde::{Deserialize, Serialize};
use super::{action::Actions, package::Package};
#[derive(Serialize, Deserialize, Debug)]
pub struct CrateManifest {
#[serde(rename = "crate")]
pub crate_info: CrateInfo,
pub packages: Option<Vec<Package>>,
pub actions: Option<Actions>,
pub metadata: Option<Metadata>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct CrateInfo {
pub name: String,
pub description: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Metadata {
pub homepage: Option<String>,
pub repository: Option<String>,
pub issues: Option<String>,
}

View file

@ -0,0 +1,62 @@
use log::{error, info, warn};
use manifest::CrateManifest;
use package::PackageManager;
use serde::{Deserialize, Serialize};
pub mod action;
pub mod manifest;
pub mod package;
pub struct Crate {
pub manifest: CrateManifest,
}
impl Crate {
pub fn from_toml_str(string: &str) -> Result<Self, toml::de::Error> {
Ok(Crate {
manifest: toml::from_str::<manifest::CrateManifest>(string)?,
})
}
pub fn install_packages(&self) -> bool {
if let Some(packages) = &self.manifest.packages {
info!("Installing packages...");
let pkgs: Vec<String> = packages
.iter()
.map(|p| p.get_correct_package_name())
.collect();
info!(target: "item", "pkgs: {}", pkgs.join(", "));
if let Some(pm) = PackageManager::get_available() {
pm.install(pkgs).is_ok()
} else {
false
}
} else {
false
}
}
pub fn run_actions(&self) -> Result<(), Box<dyn std::error::Error>> {
if let Some(actions) = &self.manifest.actions {
if let Some(commands) = &actions.commands {
for command in commands {
info!(
"Running {}...",
&command.description.clone().unwrap_or("action".to_string())
);
command.run()?;
}
}
if let Some(links) = &actions.links {
for link in links {
info!("Link {} -> {}...", link.src, link.dest);
if let Err(e) = link.link() {
error!("could not link: {e}");
continue;
}
}
}
}
Ok(())
}
}

View file

@ -0,0 +1,78 @@
use std::{collections::HashMap, io};
use log::{error, info};
use serde::{Deserialize, Serialize};
use super::action::CommandAction;
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Hash)]
pub enum PackageManager {
#[serde(rename = "pacman")]
Pacman,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Package {
pub name: String,
pub distro_name_mapping: Option<HashMap<PackageManager, String>>,
}
impl PackageManager {
pub fn install(&self, packages: Vec<String>) -> Result<bool, Box<dyn std::error::Error>> {
match &self {
PackageManager::Pacman => {
CommandAction::new(
format!("pacman -S --noconfirm {}", packages.join(" ")),
"root".to_string(),
)
.run()?;
}
}
Ok(true)
}
pub fn get_available() -> Option<Self> {
Self::from_str(match whoami::distro().as_str() {
"Manjaro Linux" => "pacman",
_ => "unknown"
})
}
pub fn from_str(distro: &str) -> Option<Self> {
match distro {
"pacman" => Some(PackageManager::Pacman),
_ => None,
}
}
}
impl Package {
pub fn get_correct_package_name(&self) -> String {
if let Some(pm) = PackageManager::get_available() {
if let Some(mappings) = &self.distro_name_mapping {
if let Some(name) = mappings.get(&pm) {
name.to_string()
} else {
self.name.clone()
}
} else {
self.name.clone()
}
} else {
self.name.clone()
}
}
pub fn install(&self) -> Result<bool, Box<dyn std::error::Error>> {
if let Some(pm) = PackageManager::get_available() {
pm.install(vec![self.get_correct_package_name()])?;
} else {
error!("no package manager found...");
return Err(Box::new(io::Error::new(
io::ErrorKind::NotFound,
"package manager not found",
)));
}
Ok(false)
}
}

View file

@ -0,0 +1,64 @@
use std::env;
use colored::Colorize;
use fern::Dispatch;
use log::{Record, SetLoggerError};
fn format_regular<S: Into<String>>(log: S, record: &Record) -> String {
let log = log.into();
let line_prefix = |line: String, extend: bool| {
let prefix = if extend {
match record.level() {
log::Level::Trace => " ]".bright_blue(),
log::Level::Debug => " ?".green(),
log::Level::Info => " >".blue(),
log::Level::Warn => " #".yellow(),
log::Level::Error => " !".red(),
}.to_string()
} else {
match record.level() {
log::Level::Trace => "[TRACE]".bright_blue().italic(),
log::Level::Debug => "??".green(),
log::Level::Info => "=>".blue(),
log::Level::Warn => "##".yellow(),
log::Level::Error => "!!".red().bold()
}.to_string()
};
return format!("{} {}", prefix, line);
};
let mut lines = log.lines().peekable();
let mut output = match lines.peek() {
Some(_line) => line_prefix(lines.next().unwrap().to_string(), false),
None => return "".to_string(),
};
for line in lines {
output.push_str(&*format!("\n{}", line_prefix(line.to_string(), true)));
}
output
}
pub fn setup_logger() -> Result<(), SetLoggerError> {
Dispatch::new()
.format(|out, message, record| {
match record.metadata().target() {
// command output logging
"command:stdout" => out.finish(format_args!("{} {}", ">>".cyan(), message.to_string())),
"command:stderr" => out.finish(format_args!("{} {}", ">>".red(), message.to_string())),
// this target means, it's an item and not a log.
"item" => out.finish(format_args!("{} {}", "*".blue(), message.to_string())),
// default logging
_ => out.finish(format_args!("{}", format_regular(message.to_string(), record))),
}
})
.level(
env::var("SYNCR_LOG_LEVEL")
.unwrap_or_else(|_| "info".to_string())
.parse()
.unwrap_or(log::LevelFilter::Info),
)
.chain(std::io::stdout())
.apply()
}

87
sync-runner/src/main.rs Normal file
View file

@ -0,0 +1,87 @@
use std::{
env::set_current_dir,
fs::{exists, read_to_string, File},
path::{absolute, Path},
process::{exit, Stdio},
};
use action::Action;
use cfg::Config;
use clap::Parser;
use colored::Colorize;
use execute::{command_args, Execute};
use log::{debug, error, info, trace, warn};
use prelude::abspath;
mod action;
mod cfg;
mod crates;
mod logging;
mod prelude;
mod source;
mod tags;
fn main() -> Result<(), Box<dyn std::error::Error>> {
logging::setup_logger()?;
info!(target: "item", "user: {}", whoami::username());
info!(target: "item", "distro: {}", whoami::distro());
let action = Action::parse();
match action {
Action::Sync { config_path } => {
trace!("fetching config dir... {config_path:?}");
if let Some(config_path) = abspath(&config_path.unwrap_or("~/.syncr".into())) {
trace!("setting config dir as cwd... {config_path}");
set_current_dir(config_path)?;
}
let config = Config::parse(&abspath("./syncr.toml").unwrap())?;
info!("syncing \"{}\"...", config.title.bold());
info!("updating sources...");
let mut available_sources = vec![];
for (name, source) in &config.source {
debug!("checking {name}...");
if !source.available() {
warn!("source \"{name}\" unavailable.");
} else {
info!("source \"{name}\" available!");
available_sources.push(source);
}
}
if available_sources.len() == 0 {
error!("{}", "sync impossible; no sources.".bold());
exit(1);
}
let oldpwd = absolute(".")?;
for source in available_sources {
// cd to source dir
source.go_to_dir()?;
for (mut path, c) in source.get_crates()? {
path.pop();
set_current_dir(absolute(path)?)?;
info!("Syncing crate: {}...", c.manifest.crate_info.name);
c.install_packages();
if let Err(e) = c.run_actions() {
error!("action failed: {e}");
}
set_current_dir(&oldpwd)?; // i hate this but im lazy okay
source.go_to_dir()?;
}
}
set_current_dir(oldpwd)?;
info!("Completed sync.");
}
_ => {
println!("{action:#?}");
}
}
Ok(())
}

View file

@ -0,0 +1,9 @@
use std::path::{Path, PathBuf};
use std::env;
pub fn abspath(p: &str) -> Option<String> {
let exp_path = shellexpand::full(p).ok()?;
let can_path = std::fs::canonicalize(exp_path.as_ref()).ok()?;
can_path.into_os_string().into_string().ok()
}

View file

@ -0,0 +1,245 @@
use std::{
env::current_dir,
fs::{self, create_dir_all, exists},
io::Write,
path::{Path, PathBuf},
};
use git2::{build::RepoBuilder, Repository};
use log::{debug, info, trace, warn};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256, Sha512};
use crate::prelude::abspath;
#[derive(Serialize, Deserialize, Debug)]
pub struct Git {
pub url: String,
#[serde(default = "default_branch")]
pub branch: String,
}
fn default_branch() -> String {
"main".to_string()
}
impl Git {
fn url_hash(&self) -> String {
let mut hasher = Sha256::new();
hasher.update(&self.url);
let hash = hasher.finalize();
hex::encode(hash)
}
fn branch_hash(&self) -> String {
let mut hasher = Sha256::new();
hasher.update(&self.branch);
let hash = hasher.finalize();
hex::encode(hash)
}
pub fn repository_path_str(&self) -> String {
format!(".data/git/{}.{}", self.url_hash(), self.branch_hash())
}
pub fn repository_path(&self) -> Result<PathBuf, std::io::Error> {
fs::canonicalize(self.repository_path_str())
}
pub fn exists_on_fs(&self) -> bool {
self.repository_path().is_ok()
}
pub fn clone_repository(&self) -> Result<Repository, git2::Error> {
RepoBuilder::new()
.branch(&self.branch)
.clone(&self.url, Path::new(&self.repository_path_str()))
}
pub fn repository(&self) -> Result<Repository, git2::Error> {
if !self.exists_on_fs() {
create_dir_all(self.repository_path_str()).unwrap();
}
match Repository::open(self.repository_path().unwrap()) {
Ok(r) => Ok(r),
Err(_) => self.clone_repository(),
}
}
fn up_to_date(&self) -> Result<bool, Box<dyn std::error::Error>> {
debug!("checking repo up to date...");
let repo = self.repository()?;
let mut remote = repo.find_remote("origin")?;
// Fetch latest references from remote
remote.fetch(&[self.branch.clone()], None, None)?;
let fetch_head = repo.refname_to_id(&format!("refs/remotes/origin/{}", self.branch))?;
let local_head = repo.refname_to_id(&format!("refs/heads/{}", self.branch))?;
Ok(fetch_head == local_head)
}
pub fn update(&self) -> Result<bool, Box<dyn std::error::Error>> {
if self.up_to_date()? {
return Ok(true);
}
debug!("updating repository...");
let repository = self.repository()?;
let mut remote = repository.find_remote("origin")?;
let mut cb = git2::RemoteCallbacks::new();
cb.transfer_progress(|stats| {
if stats.received_objects() == stats.total_objects() {
print!(
"resolving deltas {}/{}\r",
stats.indexed_deltas(),
stats.total_deltas()
);
} else if stats.total_objects() > 0 {
print!(
"received {}/{} objects ({}) in {} bytes\r",
stats.received_objects(),
stats.total_objects(),
stats.indexed_objects(),
stats.received_bytes()
);
}
std::io::stdout().flush().unwrap();
true
});
let mut fo = git2::FetchOptions::new();
fo.remote_callbacks(cb);
// Always fetch all tags.
// Perform a download and also update tips
fo.download_tags(git2::AutotagOption::All);
info!("fetching {}...", remote.name().unwrap());
remote.fetch(&[self.branch.clone()], Some(&mut fo), None)?;
let fetch_head = repository.find_reference("FETCH_HEAD")?;
do_merge(
&repository,
&self.branch,
repository.reference_to_annotated_commit(&fetch_head)?,
)?;
Ok(true)
}
pub fn ensure(&self) -> Result<&Self, Box<dyn std::error::Error>> {
if self.exists_on_fs() {
self.update();
} else {
self.clone_repository()?;
}
Ok(self)
}
}
fn do_merge<'a>(
repo: &'a Repository,
remote_branch: &str,
fetch_commit: git2::AnnotatedCommit<'a>,
) -> Result<(), git2::Error> {
// 1. do a merge analysis
let analysis = repo.merge_analysis(&[&fetch_commit])?;
// 2. Do the appopriate merge
if analysis.0.is_fast_forward() {
info!("doing a fast forward...");
// do a fast forward
let refname = format!("refs/heads/{}", remote_branch);
match repo.find_reference(&refname) {
Ok(mut r) => {
fast_forward(repo, &mut r, &fetch_commit)?;
}
Err(_) => {
// The branch doesn't exist so just set the reference to the
// commit directly. Usually this is because you are pulling
// into an empty repository.
repo.reference(
&refname,
fetch_commit.id(),
true,
&format!("Setting {} to {}", remote_branch, fetch_commit.id()),
)?;
repo.set_head(&refname)?;
repo.checkout_head(Some(
git2::build::CheckoutBuilder::default()
.allow_conflicts(true)
.conflict_style_merge(true)
.force(),
))?;
}
};
} else if analysis.0.is_normal() {
// do a normal merge
let head_commit = repo.reference_to_annotated_commit(&repo.head()?)?;
normal_merge(&repo, &head_commit, &fetch_commit)?;
} else {
info!("nothing to do...");
}
Ok(())
}
fn fast_forward(
repo: &Repository,
lb: &mut git2::Reference,
rc: &git2::AnnotatedCommit,
) -> Result<(), git2::Error> {
let name = match lb.name() {
Some(s) => s.to_string(),
None => String::from_utf8_lossy(lb.name_bytes()).to_string(),
};
let msg = format!("fast-forward: setting {} to id: {}", name, rc.id());
info!("{}", msg);
lb.set_target(rc.id(), &msg)?;
repo.set_head(&name)?;
repo.checkout_head(Some(
git2::build::CheckoutBuilder::default()
// For some reason the force is required to make the working directory actually get updated
// I suspect we should be adding some logic to handle dirty working directory states
// but this is just an example so maybe not.
.force(),
))?;
Ok(())
}
fn normal_merge(
repo: &Repository,
local: &git2::AnnotatedCommit,
remote: &git2::AnnotatedCommit,
) -> Result<(), git2::Error> {
let local_tree = repo.find_commit(local.id())?.tree()?;
let remote_tree = repo.find_commit(remote.id())?.tree()?;
let ancestor = repo
.find_commit(repo.merge_base(local.id(), remote.id())?)?
.tree()?;
let mut idx = repo.merge_trees(&ancestor, &local_tree, &remote_tree, None)?;
if idx.has_conflicts() {
warn!("merge conficts detected...");
repo.checkout_index(Some(&mut idx), None)?;
return Ok(());
}
let result_tree = repo.find_tree(idx.write_tree_to(repo)?)?;
// now create the merge commit
let msg = format!("Merge: {} into {}", remote.id(), local.id());
let sig = repo.signature()?;
let local_commit = repo.find_commit(local.id())?;
let remote_commit = repo.find_commit(remote.id())?;
// Do our merge commit and set current branch head to that commit.
let _merge_commit = repo.commit(
Some("HEAD"),
&sig,
&sig,
&msg,
&result_tree,
&[&local_commit, &remote_commit],
)?;
// Set working tree to match head.
repo.checkout_head(None)?;
Ok(())
}

View file

@ -0,0 +1,78 @@
use std::{
env::{current_dir, set_current_dir},
fs::{create_dir_all, exists, read_to_string},
path::PathBuf,
};
use log::{debug, info, trace};
use serde::{Deserialize, Serialize};
use crate::{crates::Crate, prelude::abspath};
pub mod git;
#[derive(Serialize, Deserialize, Debug)]
#[serde(default)]
pub struct Source {
interval: u64,
git: Option<git::Git>,
dir: Option<String>,
}
impl Default for Source {
fn default() -> Self {
Source {
interval: 60,
git: None,
dir: None,
}
}
}
impl Source {
pub fn available(&self) -> bool {
if let Some(git) = &self.git {
trace!("checking git...");
return git.ensure().is_ok();
}
if let Some(dir) = &self.dir {
return exists(dir).is_ok();
}
false
}
pub fn go_to_dir(&self) -> Result<(), Box<dyn std::error::Error>> {
if let Some(git) = &self.git {
if PathBuf::from(git.repository_path_str()) == current_dir()? {
return Ok(());
}
let dir = git.ensure()?.repository_path()?;
trace!(
"setting git dir as cwd... ({}@{}, {})",
git.url,
git.branch,
dir.display()
);
set_current_dir(dir)?;
}
if let Some(path) = &self.dir {
set_current_dir(path)?;
}
Ok(())
}
pub fn get_crates(&self) -> Result<Vec<(PathBuf, Crate)>, Box<dyn std::error::Error>> {
let mut crates = vec![];
// get crates (read dir, crates/*/crate.toml)
for crate_file in glob::glob("crates/*/crate.toml").expect("err") {
match crate_file {
Ok(cd) => {
debug!("found {}", cd.display());
crates.push((cd.clone(), Crate::from_toml_str(&read_to_string(cd)?)?))
}
_ => continue,
}
}
Ok(crates)
}
}

71
sync-runner/src/tags.rs Normal file
View file

@ -0,0 +1,71 @@
use serde::{Deserialize, Deserializer, Serialize, Serializer};
#[derive(Debug)]
pub struct Tag {
pub category: String,
pub value: String
}
impl<'de> Deserialize<'de> for Tag {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
// First, deserialize the string
let s: String = Deserialize::deserialize(deserializer)?;
// Split the string into category and value by ":"
let parts: Vec<&str> = s.splitn(2, ':').collect();
if parts.len() != 2 {
return Err(serde::de::Error::invalid_value(
serde::de::Unexpected::Str(&s),
&"a string in the format 'category:value'",
));
}
// Return a Tag with the split parts
Ok(Tag {
category: parts[0].to_string(),
value: parts[1].to_string(),
})
}
}
impl Serialize for Tag {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&format!("{}:{}", self.category, self.value))
}
}
impl Into<String> for Tag {
fn into(self) -> String {
self.category + " : " + &self.value
}
}
impl TryFrom<String> for Tag {
type Error = std::io::Error;
fn try_from(value: String) -> Result<Self, Self::Error> {
let parts: Vec<&str> = value.split(':').collect();
if parts.len() != 2 {
Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "could not match string to tag"))
} else {
Ok(Self {
category: parts[0].into(),
value: parts[1].into()
})
}
}
}
impl Tag {
pub fn distro() -> Tag {
Tag {
category: "distro".into(),
value: whoami::distro()
}
}
}

6
sync.sh Normal file
View file

@ -0,0 +1,6 @@
if ! [ -f /tmp/sync-runner ]; then
curl -O /tmp/sync-runner https://git.saluco.nl/repos/strix/releases/download/latest/sync-runner
chmod +x /tmp/sync-runner
fi
/tmp/sync-runner

17
syncr.toml Normal file
View file

@ -0,0 +1,17 @@
title = "strix's syncr config"
[daemon]
# interval
# discription: how often to check for new updates, this is the default for syncs
# you can define a custom interval for specific sources
# unit = minutes
interval = 60
[source.personal]
dir = "."
# [source.personal.git] # default is the uid
# url = "https://git.saluco.nl/strix/dotfiles.git"
# branch = "syncr"
# crate_dir = "./crates" # default
# cfg_toml = "./syncr.toml" # default