Coding style compliance

pull/36/head
deajan 9 years ago
parent 4bfe74de59
commit c4388c242b

@ -6,8 +6,13 @@ KNOWN ISSUES
RECENT CHANGES RECENT CHANGES
-------------- --------------
- Added LSB info to init script for Debian based distros - Improved Logging
!- Updated osync to be fully compliant with coding style
- Uploaded coding style manifest
- Integrated new realpath emulation from https://github.com/mkropat/sh-realpath - Integrated new realpath emulation from https://github.com/mkropat/sh-realpath
v0-v1.0x - Jun 2013 - Sep 2015
- Added LSB info to init script for Debian based distros
- 22 Jul. 2015: Osync v1.00a released - 22 Jul. 2015: Osync v1.00a released
- Small improvements in osync-batch.sh time management - Small improvements in osync-batch.sh time management
- Improved various logging on error - Improved various logging on error

@ -0,0 +1,66 @@
Coding style used for my bash projects (v2 Sep 2015)
1. Indentation
Using tabs
Transform old shell scripts using unexpand command
2. Comments
# Some comment
## Some important comment for the next function
################################################# Some separation
3. Functions
Every word in a function begins with an uppercase (ex: SomeFunctionDoesThings)
Define functions this way. Use sed ':a;N;$!ba;s/\n{\n/ {\n/g' to adapt when opening bracket is on a new line.
function something {
}
If function has some arguments, use local variable names that are more readable than $1...$n. Explain via comments what those variables contain if needed.
function anotherthing {
local var_name="${1}"
local other_var_name="${2}" # This variable contains stuff
}
Functions should always have return status
function thirdthing {
some_command
return $?
}
3.1 Sub functions
When a function is a subroutine of another function, it is called _SomethingAsSubFunction
4. If statements
If statements will be fully written (word "if" must be used). then is written on the same line.
(Use sed ':a;N;$!ba;s/]\n\t*then/]; then/g' to convert files to this format... Replace "],new line, zero or more tabs, then" by "; then")
if [ something ]; then
stuff
else
other stuff
fi
5. Logging
A logging function is available with the following levels of logging:
- DEBUG: Only log this when DEBUG flas is set in program. Any command forged for eval should be logged by this.
- NOTICE: Standard messages
- WARN: Requires attention
- ERROR: Program produced an error but continues execution
- CRITICAL: Program execution is halted
6. Eval
The eval command should always contain 2>1&.
There's a special case where this is needed:
eval "some;commands" 2>> "$LOG_FILE" in order to get error messages into the log.

@ -27,13 +27,15 @@ Some users report osync to work on MacOS X too. Microsoft Windows is supported v
## Installation ## Installation
Please note that development of version 1.1 has begun. Stable release is v1.01.
Osync has been designed to not delete any data, but rather make backups of conflictual files or soft deletes. Osync has been designed to not delete any data, but rather make backups of conflictual files or soft deletes.
Nevertheless, you should always have a neat backup of your data before trying a new sync tool. Nevertheless, you should always have a neat backup of your data before trying a new sync tool.
You can download the latest stable release of Osync at www.netpower.fr/osync You can download the latest stable release of Osync at www.netpower.fr/osync
You may also get the last development version at https://github.com/deajan/osync with the following command You may also get the last development version at https://github.com/deajan/osync with the following command
$ git clone https://github.com/deajan/osync $ git clone -b "v1.01" https://github.com/deajan/osync
$ sh install.sh $ sh install.sh
Osync will install itself to /usr/local/bin and an example configuration file will be installed to /etc/osync Osync will install itself to /usr/local/bin and an example configuration file will be installed to /etc/osync

@ -6,14 +6,12 @@ SCRIPT_BUILD=2015090801
## Tested on RHEL / CentOS 6 & 7 and Mint 17 ## Tested on RHEL / CentOS 6 & 7 and Mint 17
## Please adapt this to fit your distro needs ## Please adapt this to fit your distro needs
if [ "$(whoami)" != "root" ] if [ "$(whoami)" != "root" ]; then
then
echo "Must be run as root." echo "Must be run as root."
exit 1 exit 1
fi fi
if [ ! -d /etc/osync ] if [ ! -d /etc/osync ]; then
then
mkdir /etc/osync mkdir /etc/osync
fi fi

@ -18,8 +18,7 @@ MAX_EXECUTION_TIME=36000
MAX_RERUNS=3 MAX_RERUNS=3
## Log file path ## Log file path
if [ -w /var/log ] if [ -w /var/log ]; then
then
LOG_FILE=/var/log/osync-batch.log LOG_FILE=/var/log/osync-batch.log
else else
LOG_FILE=./osync-batch.log LOG_FILE=./osync-batch.log
@ -27,8 +26,7 @@ fi
# No need to edit under this line ############################################################## # No need to edit under this line ##############################################################
function Log function Log {
{
prefix="TIME: $SECONDS - " prefix="TIME: $SECONDS - "
echo -e "$prefix$1" >> "$LOG_FILE" echo -e "$prefix$1" >> "$LOG_FILE"
@ -38,8 +36,7 @@ function Log
fi fi
} }
function CheckEnvironment function CheckEnvironment {
{
## Osync executable full path can be set here if it cannot be found on the system ## Osync executable full path can be set here if it cannot be found on the system
if ! type -p osync.sh > /dev/null 2>&1 if ! type -p osync.sh > /dev/null 2>&1
then then
@ -58,20 +55,17 @@ function CheckEnvironment
fi fi
## Check for CONF_FILE_PATH ## Check for CONF_FILE_PATH
if [ ! -d "$CONF_FILE_PATH" ] if [ ! -d "$CONF_FILE_PATH" ]; then
then
Log "Cannot find conf file path $CONF_FILE_PATH" Log "Cannot find conf file path $CONF_FILE_PATH"
Usage Usage
fi fi
} }
function Batch function Batch {
{
## Get list of .conf files ## Get list of .conf files
for i in $(ls $CONF_FILE_PATH/*.conf) for i in $(ls $CONF_FILE_PATH/*.conf)
do do
if [ "$RUN" == "" ] if [ "$RUN" == "" ]; then
then
RUN="$i" RUN="$i"
else else
RUN=$RUN" $i" RUN=$RUN" $i"
@ -85,11 +79,9 @@ function Batch
for i in $RUN for i in $RUN
do do
$OSYNC_EXECUTABLE "$i" $opts $OSYNC_EXECUTABLE "$i" $opts
if [ $? != 0 ] if [ $? != 0 ]; then
then
Log "Run instance $(basename $i) failed" Log "Run instance $(basename $i) failed"
if [ "RUN_AGAIN" == "" ] if [ "RUN_AGAIN" == "" ]; then
then
RUN_AGAIN="$i" RUN_AGAIN="$i"
else else
RUN_AGAIN=$RUN_AGAIN" $i" RUN_AGAIN=$RUN_AGAIN" $i"
@ -104,8 +96,7 @@ function Batch
done done
} }
function Usage function Usage {
{
echo "$PROGRAM $PROGRAM_BUILD" echo "$PROGRAM $PROGRAM_BUILD"
echo $AUTHOR echo $AUTHOR
echo $CONTACT echo $CONTACT

@ -26,20 +26,17 @@ confdir=/etc/osync
pidfile=/var/run/$prog pidfile=/var/run/$prog
SCRIPT_BUILD=1304201502 SCRIPT_BUILD=1304201502
if [ ! -f $progpath/$progexec ] && [ ! -f $progexec ] if [ ! -f $progpath/$progexec ] && [ ! -f $progexec ]; then
then
echo "Cannot find $prog executable in $progpath nor in local path." echo "Cannot find $prog executable in $progpath nor in local path."
exit 1 exit 1
fi fi
if [ ! -w $(dirname $pidfile) ] if [ ! -w $(dirname $pidfile) ]; then
then
pidfile=./$prog pidfile=./$prog
fi fi
start() { start() {
if [ ! -f $confdir/*.conf ] if [ ! -f $confdir/*.conf ]; then
then
echo "Cannot find any configuration files in $confdir." echo "Cannot find any configuration files in $confdir."
exit 1 exit 1
fi fi
@ -48,11 +45,9 @@ start() {
for cfgfile in $confdir/*.conf for cfgfile in $confdir/*.conf
do do
if [ -f $progpath/$progexec ] if [ -f $progpath/$progexec ]; then
then
$progpath/$progexec $cfgfile --on-changes > /dev/null 2>&1 & $progpath/$progexec $cfgfile --on-changes > /dev/null 2>&1 &
elif [ -f ./$progexec ] elif [ -f ./$progexec ]; then
then
./$progexec $cfgfile --on-changes > /dev/null 2>&1 & ./$progexec $cfgfile --on-changes > /dev/null 2>&1 &
else else
echo "Cannot find $prog executable in $progpath" echo "Cannot find $prog executable in $progpath"
@ -62,8 +57,7 @@ start() {
pid=$! pid=$!
retval=$? retval=$?
if [ $? == 0 ] if [ $? == 0 ]; then
then
echo $pid > "$pidfile-$(basename $cfgfile)" echo $pid > "$pidfile-$(basename $cfgfile)"
echo "$prog successfully started for configuration file $cfgfile" echo "$prog successfully started for configuration file $cfgfile"
else else
@ -76,8 +70,7 @@ start() {
} }
stop() { stop() {
if [ ! -f $pidfile-* ] if [ ! -f $pidfile-* ]; then
then
echo "No running $prog instances found." echo "No running $prog instances found."
exit 1 exit 1
fi fi
@ -87,8 +80,7 @@ stop() {
if ps -p$(cat $pfile) > /dev/null 2>&1 if ps -p$(cat $pfile) > /dev/null 2>&1
then then
kill -TERM $(cat $pfile) kill -TERM $(cat $pfile)
if [ $? == 0 ] if [ $? == 0 ]; then
then
rm -f $pfile rm -f $pfile
echo "$prog instance $(basename $pfile) stopped." echo "$prog instance $(basename $pfile) stopped."
else else
@ -102,8 +94,7 @@ stop() {
} }
status() { status() {
if [ ! -f $pidfile-* ] if [ ! -f $pidfile-* ]; then
then
echo "Cannot find any running $prog instance." echo "Cannot find any running $prog instance."
exit 1 exit 1
fi fi

File diff suppressed because it is too large Load Diff

@ -19,14 +19,12 @@ CMD3=
LOG_FILE=~/.ssh/ssh_filter.log LOG_FILE=~/.ssh/ssh_filter.log
function Log function Log {
{
DATE=$(date) DATE=$(date)
echo "$DATE - $1" >> $LOG_FILE echo "$DATE - $1" >> $LOG_FILE
} }
function Go function Go {
{
eval $SSH_ORIGINAL_COMMAND eval $SSH_ORIGINAL_COMMAND
} }
@ -48,31 +46,24 @@ case ${SSH_ORIGINAL_COMMAND%% *} in
"mv") "mv")
Go ;; Go ;;
"$CMD1") "$CMD1")
if [ "$CMD1" != "" ] if [ "$CMD1" != "" ]; then
then
Go ;; Go ;;
fi fi
"$CMD2") "$CMD2")
if [ "$CMD2" != "" ] if [ "$CMD2" != "" ]; then
then
Go ;; Go ;;
fi fi
"$CMD3") "$CMD3")
if [ "$CMD3" != "" ] if [ "$CMD3" != "" ]; then
then
Go ;; Go ;;
fi fi
"sudo") "sudo")
if [ "$SUDO_EXEC" == "yes" ] if [ "$SUDO_EXEC" == "yes" ]; then
then if [[ "$SSH_ORIGINAL_COMMAND" == "sudo $RSYNC_EXECUTABLE"* ]]; then
if [[ "$SSH_ORIGINAL_COMMAND" == "sudo $RSYNC_EXECUTABLE"* ]]
then
Go Go
elif [[ "$SSH_ORIGINAL_COMMAND" == "sudo du"* ]] elif [[ "$SSH_ORIGINAL_COMMAND" == "sudo du"* ]]; then
then
Go Go
elif [[ "$SSH_ORIGINAL_COMMAND" == "sudo find"* ]] elif [[ "$SSH_ORIGINAL_COMMAND" == "sudo find"* ]]; then
then
Go Go
elif [[ "$SSH_ORIGINAL_COMMAND" == "sudo mkdir"* ]] elif [[ "$SSH_ORIGINAL_COMMAND" == "sudo mkdir"* ]]
then then
@ -89,22 +80,16 @@ case ${SSH_ORIGINAL_COMMAND%% *} in
elif [[ "$SSH_ORIGINAL_COMMAND" == "sudo mv"* ]] elif [[ "$SSH_ORIGINAL_COMMAND" == "sudo mv"* ]]
then then
Go Go
elif [[ "$SSH_ORIGINAL_COMMAND" == "sudo $CMD1"* ]] elif [[ "$SSH_ORIGINAL_COMMAND" == "sudo $CMD1"* ]]; then
then if [ "$CMD1" != "" ]; then
if [ "$CMD1" != "" ]
then
Go Go
fi fi
elif [[ "$SSH_ORIGINAL_COMMAND" == "sudo $CMD2"* ]] elif [[ "$SSH_ORIGINAL_COMMAND" == "sudo $CMD2"* ]]; then
then if [ "$CMD2" != "" ]; then
if [ "$CMD2" != "" ]
then
Go Go
fi fi
elif [[ "$SSH_ORIGINAL_COMMAND" == "sudo $CMD3"* ]] elif [[ "$SSH_ORIGINAL_COMMAND" == "sudo $CMD3"* ]]; then
then if [ "$CMD3" != "" ]; then
if [ "$CMD3" != "" ]
then
Go Go
fi fi
else else

Loading…
Cancel
Save