2
0
mirror of https://github.com/deajan/osync synced 2024-11-05 12:01:02 +00:00
osync/CODING_STYLE.TXT

186 lines
5.8 KiB
Plaintext
Raw Normal View History

2016-08-29 15:39:56 +00:00
Coding style used for my bash projects (v2.4 Sep 2016)
2015-09-08 14:08:14 +00:00
2015-11-12 00:37:43 +00:00
++++++ Header
2015-09-09 07:38:05 +00:00
Always use the following header
2015-11-12 00:37:43 +00:00
2015-09-09 07:38:05 +00:00
----BEGIN HEADER
#!/usr/bin/env bash
PROGRAM="program-name" # Long description
2016-08-22 08:31:47 +00:00
AUTHOR="(C) 20XX-20YY by Orsiris de Jong"
2015-09-09 07:38:05 +00:00
CONTACT="http://www.example.com me@example.com"
PROGRAM_BUILD=YYYYMMDDVV
## Optional instructions
----END HEADER
Using bind style versionning:
YYYYMMDDVV (Year, Month, Day, Revision): Example: 2015012402 = 2nd revision of 24 Jan 2015
2015-11-12 00:37:43 +00:00
#!/usr/bin/env bash instead of #!/bin/bash
Change old scripts with
for i in $(grep -r '#!/bin/bash' * |cut -f1 -d':'); do sed -i 's&#!/bin/bash&#!/usr/bin/env bash&g' $i; done
type instead of type -p for bash test (other shells don't know -p)
2016-08-22 08:31:47 +00:00
2015-11-12 00:37:43 +00:00
++++++ Indentation
2015-09-08 14:08:14 +00:00
Using tabs
Transform old shell scripts using unexpand command
2015-11-12 00:37:43 +00:00
++++++ Comments
2015-09-08 14:08:14 +00:00
2015-09-10 19:42:11 +00:00
Some command # comment
## Some comment on a new line
2015-09-08 14:08:14 +00:00
################################################# Some separation
2015-11-12 00:37:43 +00:00
++++++ Work comments
2015-09-09 07:38:05 +00:00
2016-08-22 08:31:47 +00:00
Whenever there is some idea to postpone, use #TODO(priority):[dev-name:] some remark
Priority can be critical, high, medium, low, verylow. No release can happen if there are TODOs other than low or verylow.
Example: #TODO(high):deajan: need to do something
A "work in progress" marker must be left on the line a dev is working when it's work isn't finished). Marker is #WIP:dev-name: some remark
2015-09-10 19:42:11 +00:00
dev-name is mandatory if more than one person is coding
2016-08-22 08:31:47 +00:00
Example: #WIP:deajan: missing function something
2015-09-09 07:38:05 +00:00
2015-11-12 00:37:43 +00:00
++++++ Variables
2015-09-09 07:38:05 +00:00
2016-08-22 08:31:47 +00:00
All local variables names have each first letter of the words uppercase and all others lowercase, except for the first word where all letters are lowercase
Example: someLongVariable
All global variables are full upercase, separated by _
Example: EXEC_TIME
All environment variables (verbose, silent, debug, etc) have prefix _ and are full upercase, separated by _
Example: _PARANOIA_DEBUG
2015-09-09 07:38:05 +00:00
2016-08-29 15:39:56 +00:00
Exec time variables that can take boolean values should use true and false instead of 1 and 0.
2015-11-12 00:37:43 +00:00
++++++ Functions
2015-09-08 14:08:14 +00:00
2016-08-22 08:31:47 +00:00
All function names should begin with an uppercase letter for every word, the other letters should be lowercase
Example: SomeFunctionThatRocks
2015-09-08 14:08:14 +00:00
2016-08-22 08:31:47 +00:00
Bash does not provide any checks against missing function arguments. Also, missing quotes can lead to an inconsistent number of arguments.
Most functions should have a first line that calls the special function __CheckArguments, which checks the number of given arguments for a function in order
2016-08-28 09:30:28 +00:00
to find possible problems. Number of arguments are given as first argument to __CheckArguments. May be a number or a range, eg 0-2 if the function takes optional arguments.
2016-08-22 08:31:47 +00:00
__CheckArguments will only trigger when the script is launched with _PARANOIA_DEBUG=yes. Also, it will only exist in the debug version.
Use the following convention for function definition:
function SomeFunction {
__CheckArguments 0 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG
...
2015-09-08 14:08:14 +00:00
}
2016-08-22 08:31:47 +00:00
Use sed ':a;N;$!ba;s/\n{\n/ {\n/g' to convert functions that have opening brackets on a new line.
2015-09-08 14:08:14 +00:00
2016-08-22 08:31:47 +00:00
If the function has arguments, use local variable names that are more readable than $1...$n. Explain via comments what those variables contain if needed.
Declare arguments before launching __CheckArguments:
function AnotherFunction {
local varName="${1}"
local otherVarName="${2}" # This variable contains stuff
__CheckArguments 2 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG
...
2015-09-08 14:08:14 +00:00
}
Functions should always have return status
2016-08-22 08:31:47 +00:00
function RandomFunction {
...
2015-09-08 14:08:14 +00:00
return $?
}
2015-11-12 00:37:43 +00:00
++++++ Sub functions
2015-09-08 14:08:14 +00:00
2016-08-22 08:31:47 +00:00
When a function is a subroutine of another function, it is called _SomethingAsSubFunction:
Example:
2015-09-08 14:08:14 +00:00
2016-08-22 08:31:47 +00:00
function _ApplyLocally
function _ApplyRemotely
function Apply
2015-09-09 12:50:21 +00:00
2016-08-22 08:31:47 +00:00
++++++ For and While statements
For and while statements will have the "do" part on the first line.
Example:
2015-09-09 12:50:21 +00:00
2016-08-22 08:31:47 +00:00
for i in "${var[@]}"; do
...
done
2015-09-09 12:50:21 +00:00
2016-08-22 08:31:47 +00:00
while [ $i -eq 1 ]; do
...
done
2015-09-09 12:50:21 +00:00
2015-11-12 00:37:43 +00:00
++++++ If statements
2015-09-08 14:08:14 +00:00
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
2015-11-12 00:37:43 +00:00
++++++ Logging
2015-09-08 14:08:14 +00:00
A logging function is available with the following levels of logging:
2016-08-22 08:31:47 +00:00
- DEBUG: Only log this when DEBUG flas is set in program. Any command forged for eval instruction should be logged by this.
2015-09-08 14:08:14 +00:00
- NOTICE: Standard messages
- WARN: Requires attention
- ERROR: Program produced an error but continues execution
- CRITICAL: Program execution is halted
2015-11-12 00:37:43 +00:00
++++++ Eval
2015-09-08 14:08:14 +00:00
2015-09-19 16:40:26 +00:00
Most commands should be logged to a tmp file.
The basic way of doing is:
2015-11-12 00:37:43 +00:00
cmd='"something '$somevar'" > some_file 2>&1'
2015-09-19 16:40:26 +00:00
eval $cmd &
2015-11-12 00:37:43 +00:00
WaitForTaskCompletion $! 0 0 $FUNCNAME
2015-09-19 16:40:26 +00:00
Remote commands should exist as:
cmd=$SSH_CMD' "some; commands \"'$VARIABLE'\" some; other; commands" > some_file 2>&1'
2015-11-12 00:37:43 +00:00
++++++ File variables
2015-09-19 16:40:26 +00:00
All eval cmd should exit their content to a file called "$RUNDIR/osync.$FUNCNAME.$SCRIPT_PID"
2016-08-22 08:31:47 +00:00
Dots are used instead of '_' so variables can be separated with a forbidden char in variable names, so the separtors apply as wished.
2015-09-08 14:08:14 +00:00
2015-11-12 00:37:43 +00:00
++++++ Finding code errors
Use shellcheck.net now and then (ignore SC2086 in our case)
Use a low tech approach to find uneven number of quotes per line
tr -cd "'\n" < my_bash_file.sh | awk 'length%2==1 {print NR, $0}'
tr -cd "\"\n" < my_bash_file.sh | awk 'length%2==1 {print NR, $0}'
++++++ ofunctions
As obackup and osync share alot of common functions, ofunctions.sh will host all shared code.
Dev programs n_osync.sh and n_obackup.sh will source ofunctions.sh
Release programs will still include ofunctions.sh in order to enhance ease of use.
Ofunctions are defined like:
#__FUNC:FunctionName
function FunctionName {
}
#__ENDFUNC
These functions are inserted into code that has placeholders like #__FUNC:FuncName
2016-08-22 08:31:47 +00:00
+++++++ Exit codes
Normal exit code = 0
Run with errors exit code = 1
Run with warnings exit code = 2
Wrong shell exit code = 127
Usage function exit code = 128