Skip to content

Commit

Permalink
Initial release.
Browse files Browse the repository at this point in the history
  • Loading branch information
kamildzi committed Nov 1, 2024
0 parents commit 887156e
Show file tree
Hide file tree
Showing 8 changed files with 330 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Kamil D.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ArchEtcUpdate
===================
A simple tool for management `*.pacsave` and `*.pacnew` files for Arch-like systems.\
It's build in bash in order to keep required dependencies at minimum level.

# Configuration
Please see `config.sh`. You will need to set up your diff tool.

# Usage
Just run the script:
```bash
./run.sh
```
32 changes: 32 additions & 0 deletions config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash
# @K.Dziuba
# Etc Update for Arch-like systems - Config File


# ===========================
# General settings
# ===========================

# Please choose your favourite diff-tool.
# Examples: vimdiff, meld, kompare, kdiff3.
# ( This is the application that is going to be used to compare and manage changes in files ).
diffToolApp='meld'


# ===================================
# Auth settings
# ===================================
# enable root authentication types
# -----------------------------------

# enable for sudo authentication (recommended)
authWithSudo='yes'

# enable for polkit/pkexec authentication (GUI only)
# authWithPkexec='yes'

# enable for zensu authentication
# authWithZensu='yes'

# enable for gksu authentication
# authWithGksu='yes'
23 changes: 23 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash
# @K.Dziuba
# Etc-update script for Arch-based Linux distributions.
# It allows to find and manage pacsave and pacnew files easily.

# ==============
# Paths config
# ==============
SCRIPT_ROOT=$( readlink -f $( dirname "${BASH_SOURCE[0]}" ) )
cd "$SCRIPT_ROOT"

# ==============
# Sources
# ==============
source config.sh
source src/EtcUpdate.sh

# ==================
# Invoke the script
# ==================
autoSetAuthPrefix
set -eu
main
23 changes: 23 additions & 0 deletions src/Common/CommonOps.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash
# Functions for common use - General Operations

# Check last operation result.
# Exit on failure
checkLastOp() {
if [[ $? != 0 ]]; then
printError " ... FAILED!"
exit 1
else
printSuccess " ... OK"
fi
}

# Ask whether shall we continue
askContinue() {
printText "\nContinue? [Y/n] "
read ans
if [[ $ans != 'Y' ]]; then
printNotice "Aborted by user"
exit 0
fi
}
102 changes: 102 additions & 0 deletions src/EtcUpdate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/usr/bin/env bash
# @K.Dziuba
# Etc Update for Arch-like systems

# source files
for file in `find ./src/*/* -name '*.sh' `; do
source $file
done

# main function
main() {
init
initDiffTool

printNotice "--- Processing pacsave files ---"
for file in `$authPrefix find /etc -name '*.pacsave'`; do
if [[ -f ${file::-8} ]]; then
# original file exists -> let's see the difference
processEtcFile $file ${file::-8}
else
# original file do not exist -> let's ask for removal
printWarning "Unused pacsave found: \n${file}."
askForFileRemoval ${file}
fi
done

printNotice "--- Processing pacnew files ---"
for file in `$authPrefix find /etc -name '*.pacnew'`; do
processEtcFile $file ${file::-7}
done

# all done
printSuccess "" "Done!"
}

# Process single file from etc. Run diff-tool app, then ask for removal.
# $1 - a new file that came to /etc (".pacsave" or ".pacnew" file)
# $2 - a current file (a file that is currently in-use and should be updated)
processEtcFile() {
if [[ -z $1 || ! -f $1 ]]; then
printError "Fle A -> Not a file! Got: $1"
exit 2
fi

if [[ -z $2 || ! -f $2 ]]; then
printError "File B -> Not a file! Got: $2"
exit 2
fi

# get params
newFile=$1
inUseFile=$2

# update
printText "Comparing: "
printText "> A: $newFile"
printText "> B: $inUseFile"
$authPrefix $diffToolApp "$newFile" "$inUseFile"
checkLastOp

# ask for removal
askForFileRemoval ${newFile}
}

# Ask for removal permission. If user approves it -> then (and only then!) remove the file.
# $1 - A file to remove
askForFileRemoval() {
printText "Remove the file: ${1}? [y/N] "
read ans
if [[ $ans == 'y' ]]; then
printNotice "Removing: ${1}"
$authPrefix rm ${1}
fi
}

# check and init diff-tool application
initDiffTool() {
printText "Using '${diffToolApp}' as diff-tool..."

if [[ ! -z ${diffToolApp} ]] && which ${diffToolApp} | read; then
diffToolApp=$(which ${diffToolApp})
printText ""
else
printError "... '${diffToolApp}' not found!"
exit 127
fi
}

# pre-run function
init() {
printInfo "[$0] Starting up ..."

# set up traps
trap terminate SIGINT SIGTERM ERR 0
}

# post-run / terminate function
terminate() {
printInfo "[$0] Terminating ..."
# ( possible post-run actions )
exit
}
78 changes: 78 additions & 0 deletions src/IO/Print.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env bash
# @K.Dziuba
# Message/Print functions

# Define colors (ANSI)
C_RED='\033[0;31m'
C_GREEN='\033[0;32m'
C_BLUE='\033[0;34m'
C_YELLOW='\033[1;33m'
C_PURPLE='\033[0;35m'
C_CYAN='\033[0;36m'
C_NC='\033[0m' # no-color

# Print error message
# $1, $2, ... - message text
printError() {
row=1
while (( "$#" )); do
if [[ row -eq '1' ]]; then
>&2 echo -e "${C_RED} [ERROR] $1${C_NC}"
else
>&2 echo -e "${C_RED} $1${C_NC}"
fi
shift
((row++))
done
}

# Print warning message
# $1, $2, ... - message text
printWarning() {
row=1
while (( "$#" )); do
if [[ row -eq '1' ]]; then
>&2 echo -e "${C_YELLOW} [WARNING] $1${C_NC}"
else
>&2 echo -e "${C_YELLOW} $1${C_NC}"
fi
shift
((row++))
done
}

# Print success message
# $1, $2, ... - message text
printSuccess() {
while (( "$#" )); do
echo -e " ${C_GREEN}$1${C_NC}"
shift
done
}

# Print notice message
# $1, $2, ... - message text
printNotice() {
while (( "$#" )); do
echo -e " ${C_PURPLE}$1${C_NC}"
shift
done
}

# Print info message
# $1, $2, ... - message text
printInfo() {
while (( "$#" )); do
echo -e " ${C_CYAN}$1${C_NC}"
shift
done
}

# Print plain text message
# $1, $2, ... - message text
printText() {
while (( "$#" )); do
echo -e " $1"
shift
done
}
38 changes: 38 additions & 0 deletions src/System/Auth.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env bash
# @K.Dziuba
# System/Auth functions

# sets the $authPrefix, if given binary exists
# $1 - binary file
setAuthPrefix() {
if which "$1" | read; then
authPrefix="$1"
else
printWarning "$1 not found! Skipping..."
fi
}

# uses setAuthPrefix() to auto-set the $authPrefix defined in the config
autoSetAuthPrefix() {
authPrefix=''

# Sudo support
if [[ $authWithSudo == 'yes' ]]; then
setAuthPrefix sudo
fi

# Zensu support
if [[ $authWithZensu == 'yes' ]]; then
setAuthPrefix zensu
fi

# Gksu support
if [[ $authWithGksu == 'yes' ]]; then
setAuthPrefix gksu
fi

# Polkit/pkexec support
if [[ $authWithPkexec == 'yes' ]]; then
setAuthPrefix pkexec
fi
}

0 comments on commit 887156e

Please sign in to comment.