From c074c0d51a6e3c7924a2ddea3ea418729564a252 Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Wed, 18 Mar 2020 15:26:55 +0100 Subject: Bringing shellcheck into the mix MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Miquel Sabaté Solà --- CONTRIBUTING.org | 9 +++++++++ Makefile | 10 +++++++++- g.sh | 42 +++++++++++++++++++++++------------------- 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/CONTRIBUTING.org b/CONTRIBUTING.org index 9b9b07c..5c1a199 100644 --- a/CONTRIBUTING.org +++ b/CONTRIBUTING.org @@ -30,6 +30,15 @@ will just print a help message). This is done so when running the default make task this doesn't interrupt it. This task is already called by the default =test= one. +*** Shellcheck + +This project uses [[https://github.com/koalaman/shellcheck][shellcheck]] in order to validate the code style. In order to +run =shellcheck=, just perform: + +#+BEGIN_SRC bash +$ make shellcheck +#+END_SRC + ** Issue reporting I'm using [[https://github.com/mssola/writer-mode][Github]] in order to host the code. Thus, in order to report issues you diff --git a/Makefile b/Makefile index 01ec0ab..e086ae1 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ .PHONY: test -test: git-validation unit-test +test: git-validation shellcheck unit-test .PHONY: unit-test unit-test: @@ -13,3 +13,11 @@ ifeq (, $(shell which git-validation 2> /dev/null)) else @git-validation -q -range da5b6722c940..HEAD -travis-pr-only=false endif + +.PHONY: shellcheck +shellcheck: +ifeq (, $(shell which shellcheck 2> /dev/null)) + @echo "You don't have 'shellcheck' installed, consider installing it (see the CONTRIBUTING.org file)." +else + @shellcheck g.sh +endif diff --git a/g.sh b/g.sh index e310bd5..05434ff 100644 --- a/g.sh +++ b/g.sh @@ -1,3 +1,4 @@ +#!/bin/bash # Copyright (C) 2013-2020 Miquel Sabaté Solà # # This program is free software; you can redistribute it and/or modify @@ -36,34 +37,36 @@ __g_get_shortcuts() { local word="" __g_shortcuts=() - while read line; do - if [ ! -z "$line" ]; then + while read -r line; do + if [ -n "$line" ]; then if [ -z "$word" ]; then word=$line else - __g_shortcuts[$word]=$(eval echo $line) + __g_shortcuts[$word]=$(eval echo "$line") word="" fi fi - done < $__g_file + done < "$__g_file" } # Save the computed shortcuts into the __g_file. __g_save_shortcuts() { # Erase the contents of the __g_file. - :>$__g_file + :>"$__g_file" # Bash vs zsh if [ -n "$ZSH_VERSION" ]; then + # shellcheck disable=SC2154 keys="${(@i)__g_shortcuts}" else + # shellcheck disable=SC2124 keys="${!__g_shortcuts[@]}" fi # Finally write the hash into the __g_file. for i in $keys; do - echo "$i" >> $__g_file - echo "${__g_shortcuts[$i]}" >> $__g_file + echo "$i" >> "$__g_file" + echo "${__g_shortcuts[$i]}" >> "$__g_file" done } @@ -87,6 +90,7 @@ __g_join_path() { # Replacement for the non-standard `realpath` command. Implemented taken from # https://github.com/travis-ci/gimme. __g_realpath() { + # shellcheck disable=SC2005 [ -d "$1" ] && echo "$(cd "$1" && pwd)" || echo "$(cd "$(dirname "$1")" \ && pwd)/$(basename "$1")" } @@ -104,10 +108,10 @@ g() { declare -A __g_shortcuts # Make sure that the __g_file actually exists. - if [ ! -z "$GFILE" ]; then + if [ -n "$GFILE" ]; then __g_file="$GFILE" fi - touch $__g_file + touch "$__g_file" # Parse the command. case "$cmd" in @@ -125,21 +129,21 @@ HERE ;; add) if [ "$#" = "2" ]; then - path=`pwd` + path=$(pwd) else if [ "$#" = "3" ]; then - path=$3 + path="$3" else echo "usage: g add [path]" return 1 fi fi - if __g_is_keyword $2; then + if __g_is_keyword "$2"; then echo "Cannot use '$2': keyword." return 1 fi __g_get_shortcuts - __g_shortcuts[$2]=$(__g_realpath $path) + __g_shortcuts[$2]=$(__g_realpath "$path") __g_save_shortcuts ;; rm) @@ -148,7 +152,7 @@ HERE return 1 fi __g_get_shortcuts - unset __g_shortcuts[$2] + unset "__g_shortcuts[$2]" __g_save_shortcuts ;; list) @@ -158,7 +162,7 @@ HERE for i in "${!__g_shortcuts[@]}"; do str="$str $i" done - echo $str + echo "$str" else for i in "${!__g_shortcuts[@]}"; do echo -e "$i\t=> ${__g_shortcuts[$i]}" @@ -170,17 +174,17 @@ HERE # Split the path and check whether the first element is a shortcut or # not. - IFS='/' read -a path <<< $cmd - init=${path[0]} + IFS='/' read -r -a path <<< "$cmd" + init="${path[0]}" - if [ -z ${__g_shortcuts[$init]} ]; then + if [ -z "${__g_shortcuts[$init]}" ]; then echo -e "Unknown shortcut \`$init'.\n" __g_usage return 1 else # Expand the shortcut and append the remaining parts of the path. path[0]="${__g_shortcuts[$init]}" - cd $(__g_join_path ${path[@]}) + cd "$(__g_join_path "${path[@]}")" || return 1 fi ;; esac -- cgit v1.2.3