7bd88959382158e9c49304bd2c212f7777074aba
[BK-2020-03.git] / unitproc / bktemp-validateInput
1 #!/bin/bash
2 # Desc: Validates Input
3
4 yell() { echo "$0: $*" >&2; } #o Yell, Die, Try Three-Fingered Claw technique
5 die() { yell "$*"; exit 111; } #o Ref/Attrib: https://stackoverflow.com/a/25515370
6 try() { "$@" || die "cannot $*"; } #o
7 validateInput() {
8 # Desc: Validates Input
9 # Usage: validateInput [str input] [str input type]
10 # Version: 0.1.1
11 # Input: arg1: string to validate
12 # arg2: string specifying input type (ex:"ssh_pubkey")
13 # Output: return code 0: if input string matched specified string type
14 # Depends: bash 5, yell
15
16 # Save function name
17 local FN="${FUNCNAME[0]}";
18
19 # Process arguments
20 argInput="$1";
21 argType="$2";
22 if [[ $# -gt 2 ]]; then yell "ERROR:$0:$FN:Too many arguments."; exit 1; fi;
23
24 # Check for blank
25 if [[ -z "$argInput" ]]; then return 1; fi
26
27 # Define input types
28 ## ssh_pubkey
29 ### Check for alnum/dash base64 (ex: "ssh-rsa AAAAB3NzaC1yc2EAAA")
30 if [[ "$argType" = "ssh_pubkey" ]] && [[ "$argInput" =~ ^[[:alnum:]-]*[\ ]*[[:alnum:]+/=]*$ ]]; then
31 return 0; fi;
32
33 # Return error if no condition matched.
34 return 1;
35 } # Validates strings
36
37 #==BEGIN sample code==
38 testKey1="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCq8G0XWi8A+MXNu7MEaxDsZJUwtgPC4shGAAarXC4PQTRxLfUHtyWkNbzGh31U/bKI3Snp1O78yOVEZSjdmWkDvkwtWKJQcoZBStgqw9oIPE0oG8uh49XoX77nVabi0JCf8zO63Ai4/9EdwyqE9xsJ+soOYt2V5+UCj0zsv1xpt2YhfYyXrzBv9x6k9lCxy0NW3Ik1mSW+OzSOT3tgDf36ujV/CI2i8ERM9civWy8FtFOReZMV2kkbj7fXI4E1wKM1RFIn7er1MseCSvbvh3o2uCO46/euqHAstYs5cL+4yB2qM/xKfHT5aocDjq7GBLKtree9xdNF0EE9CNr/+J5R";
39 testKey2="blah-blah onetwothreeREEE";
40 testKey3="";
41 keyType="ssh_pubkey";
42 if validateInput "$testKey1" "$keyType"; then echo "$testKey1 looks like a valid $keyType."; else echo "$testKey1 doesn't look like a valid $keyType."; fi
43 if validateInput "$testKey2" "$keyType"; then echo "$testKey2 looks like a valid $keyType."; else echo "$testKey2 doesn't look like a valid $keyType."; fi
44 if validateInput "$testKey3" "$keyType"; then echo "$testKey3 looks like a valid $keyType."; else echo "$testKey3 doesn't look like a valid $keyType."; fi
45 #==END sample code==
46
47
48
49
50 # Author: Steven Baltakatei Sandoval (bktei.com)
51 # License: GPLv3+