feat(unitproc):Create function: validateInput
[BK-2020-03.git] / unitproc / bktemp-validateInput
diff --git a/unitproc/bktemp-validateInput b/unitproc/bktemp-validateInput
new file mode 100644 (file)
index 0000000..653c0d4
--- /dev/null
@@ -0,0 +1,51 @@
+#!/bin/bash
+# Desc: Validates Input
+
+yell() { echo "$0: $*" >&2; }      #o Yell, Die, Try Three-Fingered Claw technique
+die() { yell "$*"; exit 111; }     #o Ref/Attrib: https://stackoverflow.com/a/25515370
+try() { "$@" || die "cannot $*"; } #o
+validateInput() {
+    # Desc: Validates Input
+    # Usage: validateInput [str input] [str input type]
+    # Version: 0.1.0
+    # 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
+
+    # Save function name
+    local 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" ]] && [[ "$argInput" =~ ^[[:alnum:]-]*[\ ]*[[:alnum:]+/=]*$ ]]; then
+       return 0; fi;
+
+    # Return error if no condition matched.
+    return 1;
+}
+
+#==BEGIN sample code==
+testKey1="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCq8G0XWi8A+MXNu7MEaxDsZJUwtgPC4shGAAarXC4PQTRxLfUHtyWkNbzGh31U/bKI3Snp1O78yOVEZSjdmWkDvkwtWKJQcoZBStgqw9oIPE0oG8uh49XoX77nVabi0JCf8zO63Ai4/9EdwyqE9xsJ+soOYt2V5+UCj0zsv1xpt2YhfYyXrzBv9x6k9lCxy0NW3Ik1mSW+OzSOT3tgDf36ujV/CI2i8ERM9civWy8FtFOReZMV2kkbj7fXI4E1wKM1RFIn7er1MseCSvbvh3o2uCO46/euqHAstYs5cL+4yB2qM/xKfHT5aocDjq7GBLKtree9xdNF0EE9CNr/+J5R";
+testKey2="blah-blah onetwothreeREEE";
+testKey3="";
+keyType="ssh_pubkey";
+if validateInput "$testKey1" "$keyType"; then echo "$testKey1 looks like a valid $keyType."; else echo "$testKey1 doesn't look like a valid $keyType."; fi
+if validateInput "$testKey2" "$keyType"; then echo "$testKey2 looks like a valid $keyType."; else echo "$testKey2 doesn't look like a valid $keyType."; fi
+if validateInput "$testKey3" "$keyType"; then echo "$testKey3 looks like a valid $keyType."; else echo "$testKey3 doesn't look like a valid $keyType."; fi
+#==END sample code==
+
+
+
+
+# Author: Steven Baltakatei Sandoval (bktei.com)
+# License: GPLv3+