pcdshub / engineering_tools

A repository of scripts, configuration useful for the PCDS team
Other
4 stars 26 forks source link

MNT: Shellchecking bash scripts #184

Closed KaushikMalapati closed 1 month ago

KaushikMalapati commented 6 months ago

Description

Very minor edits that don't change anything important.

I shellchecked the bash scripts in /scripts (making edits based on warnings, errors and info messages that shellcheck gave). Some notes and questions I have are:

I also found a tool called shfmt which we could add a precommit job for. Here is an example of the diff it would print for kinit_helper

-function kauth {
+function kauth
+{
     # If token doesn't exist, create it (will query for password)
-    if ! klist -s
-    then
-       while ! kinit -l 365d -r 365d
-       do
+    if ! klist -s; then
+        while ! kinit -l 365d -r 365d; do
             :
-       done
+        done
     else
-       kinit -R &> /dev/null
+        kinit -R &> /dev/null
     fi
     return 0
 }

-function afsauth {
+function afsauth
+{
     # need to be in g-pcds afs grup
-    if ! pts membership g-pcds 2>&1 | grep -q "$(whoami)"
-    then
+    if ! pts membership g-pcds 2>&1 | grep -q "$(whoami)"; then
         echo "You do not have permission to use afs. See https://confluence.slac.stanford.edu/display/PCDS/Onboarding+Staff+Members"
         return 1
     fi

     # afs should be used from psbuild servers
-    if [[ $(hostname) != psbuild-rhel* ]]
-    then
+    if [[ $(hostname) != psbuild-rhel* ]]; then
         echo "You must be on psbuild to create afs tokens"
-       return 1
+        return 1
     fi

     # If token doesn't exist, create it
-    if ! tokens | grep -q $UID
-    then
+    if ! tokens | grep -q $UID; then
         # afs needs kerberos token
-       kauth
-       aklog
+        kauth
+        aklog
     fi
     return 0
 }

 # if name == '__main__':
-if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
+if [[ ${BASH_SOURCE[0]} == "${0}" ]]; then
     afsauth
 fi

It basically formats things nicely. I have the indenting set to four spaces right now, but it could be a different number or tabs instead. I haven't formatted anything yet because I don't want to add this unilaterally,

Motivation and Context

182

To standardize style and fix existing shellcheck issues so that future contributors only see pre-commit/workflow errors related to their changes instead of what was already there.

How Has This Been Tested?

Interactively

Where Has This Been Documented?

N/A

KaushikMalapati commented 6 months ago

I will turn this into a real pr once I test it a bit more.

tangkong commented 6 months ago

Is the way to go here adding shellcheck AND shellfmt to precommit?

KaushikMalapati commented 6 months ago

Is the way to go here adding shellcheck AND shellfmt to precommit?

Yes. There's some overlap but shellcheck would mainly be for catching errors and shfmt for consistent styling.

ZLLentz commented 6 months ago

Very minor edits

46 files

First glance looks great but we're going to need to review and test carefully

ZLLentz commented 6 months ago

I think shellfmt and similar sorts of auto-formatting tools are great As long as it doesn't disagree with shellcheck on anything we can include it