Browse code

Add hacking rules for shell scripts

This is an attempt to collect the rules that we live by in devstack
that are generally held. Writing these down help us figure out ways
to put them into bash8 over time. These are a starting point for
conversation.

Change-Id: Id2b750665871ebbeddf4694ba080c75d2f6f443e

Sean Dague authored on 2013/11/23 02:16:02
Showing 1 changed files
... ...
@@ -227,3 +227,51 @@ These scripts are executed serially by ``exercise.sh`` in testing situations.
227 227
   or graciously handle possible artifacts left over from previous runs if executed
228 228
   again.  It is acceptable to require a reboot or even a re-install of DevStack
229 229
   to restore a clean test environment.
230
+
231
+
232
+Bash Style Guidelines
233
+~~~~~~~~~~~~~~~~~~~~~
234
+Devstack defines a bash set of best practices for maintaining large
235
+collections of bash scripts. These should be considered as part of the
236
+review process.
237
+
238
+We have a preliminary enforcing script for this called bash8 (only a
239
+small number of these rules are enforced).
240
+
241
+Whitespace Rules
242
+----------------
243
+
244
+- lines should not include trailing whitespace
245
+- there should be no hard tabs in the file
246
+- indents are 4 spaces, and all indentation should be some multiple of
247
+  them
248
+
249
+Control Structure Rules
250
+-----------------------
251
+- then should be on the same line as the if
252
+- do should be on the same line as the for
253
+
254
+Example::
255
+
256
+  if [[ -r $TOP_DIR/local.conf ]]; then
257
+      LRC=$(get_meta_section_files $TOP_DIR/local.conf local)
258
+      for lfile in $LRC; do
259
+          if [[ "$lfile" == "localrc" ]]; then
260
+              if [[ -r $TOP_DIR/localrc ]]; then
261
+                  warn $LINENO "localrc and local.conf:[[local]] both exist, using localrc"
262
+              else
263
+                  echo "# Generated file, do not edit" >$TOP_DIR/.localrc.auto
264
+                  get_meta_section $TOP_DIR/local.conf local $lfile >>$TOP_DIR/.localrc.auto
265
+              fi
266
+          fi
267
+      done
268
+  fi
269
+
270
+Variables and Functions
271
+-----------------------
272
+- functions should be used whenever possible for clarity
273
+- functions should use ``local`` variables as much as possible to
274
+  ensure they are isolated from the rest of the environment
275
+- local variables should be lower case, global variables should be
276
+  upper case
277
+- function names should_have_underscores, NotCamelCase.