fix(bklog):Add missing validateInput()
authorSteven Baltakatei Sandoval <baltakatei@gmail.com>
Sat, 11 Jul 2020 04:01:45 +0000 (04:01 +0000)
committerSteven Baltakatei Sandoval <baltakatei@gmail.com>
Sat, 11 Jul 2020 04:01:45 +0000 (04:01 +0000)
exec/bklog

index f3722df9885a7dc0760c3842e6ae9df70e807764..2f5c57ec5dbbb2392905f319047779a3a653ba10 100644 (file)
@@ -11,7 +11,7 @@ dirTmpDefault="/dev/shm"; # Default parent of working directory
 
 # Script Metadata
 scriptName="bklog";             # Define basename of script file.
-scriptVersion="0.1.5";          # Define version of script.
+scriptVersion="0.1.6";          # Define version of script.
 scriptURL="https://gitlab.com/baltakatei/ninfacyzga-01"; # Define wesite hosting this script.
 scriptTimeStart="$(date +%Y%m%dT%H%M%S.%N)"; # YYYYmmddTHHMMSS.NNNNNNNNN
 scriptHostname=$(hostname);     # Save hostname of system running this script.
@@ -624,6 +624,60 @@ timeDuration(){
     fi
 
 } # Get duration (ex: PT10M4S )
+validateInput() {
+    # Desc: Validates Input
+    # Usage: validateInput [str input] [str input type]
+    # Version: 0.3.1
+    # Input: arg1: string to validate
+    #        arg2: string specifying input type (ex:"ssh_pubkey")
+    # Output: return code 0: if input string matched specified string type
+    # Depends: bash 5, yell()
+
+    local fn argInput argType
+
+    # Save function name
+    fn="${FUNCNAME[0]}";
+
+    # Process arguments
+    argInput="$1";
+    argType="$2";
+    if [[ $# -gt 2 ]]; then yell "ERROR:$0:$fn:Too many arguments."; exit 1; fi;
+
+    # Check for blank
+    if [[ -z "$argInput" ]]; then return 1; fi
+    
+    # Define input types
+    ## ssh_pubkey
+    ### Check for alnum/dash base64 (ex: "ssh-rsa AAAAB3NzaC1yc2EAAA")
+    if [[ "$argType" = "ssh_pubkey" ]]; then
+       if [[ "$argInput" =~ ^[[:alnum:]-]*[\ ]*[[:alnum:]+/=]*$ ]]; then
+           return 0; fi; fi;
+
+    ## age_pubkey
+    ### Check for age1[:bech32:]
+    if [[ "$argType" = "age_pubkey" ]]; then
+       if [[ "$argInput" =~ ^age1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]*$ ]]; then
+           return 0; fi; fi
+
+    ## integer
+    if [[ "$argType" = "integer" ]]; then
+       if [[ "$argInput" =~ ^[[:digit:]]*$ ]]; then
+           return 0; fi; fi;
+
+    ## time element (year, month, week, day, hour, minute, second)
+    if [[ "$argType" = "time_element" ]]; then
+       if [[ "$argInput" = "year" ]] || \
+              [[ "$argInput" = "month" ]] || \
+              [[ "$argInput" = "week" ]] || \
+              [[ "$argInput" = "day" ]] || \
+              [[ "$argInput" = "hour" ]] || \
+              [[ "$argInput" = "minute" ]] || \
+              [[ "$argInput" = "second" ]]; then
+           return 0; fi; fi;
+    
+    # Return error if no condition matched.
+    return 1;
+} # Validates strings
 
 magicInitWorkingDir() {
     # Desc: Determine temporary working directory from defaults or user input