chore(unitproc):timeEpochNS:Rename timeNanosec -> timeEpochNS
[BK-2020-03.git] / unitproc / bktemp-validateInput
CommitLineData
4cd70356
SBS
1#!/bin/bash
2# Desc: Validates Input
3
4yell() { echo "$0: $*" >&2; } #o Yell, Die, Try Three-Fingered Claw technique
5die() { yell "$*"; exit 111; } #o Ref/Attrib: https://stackoverflow.com/a/25515370
6try() { "$@" || die "cannot $*"; } #o
7validateInput() {
8 # Desc: Validates Input
9 # Usage: validateInput [str input] [str input type]
1cbe13ec 10 # Version: 0.3.0
4cd70356
SBS
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")
51895d99
SBS
30 if [[ "$argType" = "ssh_pubkey" ]]; then
31 if [[ "$argInput" =~ ^[[:alnum:]-]*[\ ]*[[:alnum:]+/=]*$ ]]; then
a3d13a32 32 return 0; fi; fi;
51895d99
SBS
33
34 ## age_pubkey
35 ### Check for age1[:bech32:]
5ac0ccd6 36 if [[ "$argType" = "age_pubkey" ]]; then
51895d99
SBS
37 if [[ "$argInput" =~ ^age1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]*$ ]]; then
38 return 0; fi; fi
4cd70356 39
a3d13a32
SBS
40 ## integer
41 if [[ "$argType" = "integer" ]]; then
42 if [[ "$argInput" =~ ^[[:digit:]]*$ ]]; then
43 return 0; fi; fi;
1cbe13ec
SBS
44
45 ## time element (year, month, week, day, hour, minute, second)
46 if [[ "$argType" = "time_element" ]]; then
47 if [[ "$argInput" = "year" ]] || \
48 [[ "$argInput" = "month" ]] || \
49 [[ "$argInput" = "week" ]] || \
50 [[ "$argInput" = "day" ]] || \
51 [[ "$argInput" = "hour" ]] || \
52 [[ "$argInput" = "minute" ]] || \
53 [[ "$argInput" = "second" ]]; then
54 return 0; fi; fi;
55
4cd70356
SBS
56 # Return error if no condition matched.
57 return 1;
6f668757 58} # Validates strings
4cd70356
SBS
59
60#==BEGIN sample code==
a3d13a32
SBS
61testKey="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCq8G0XWi8A+MXNu7MEaxDsZJUwtgPC4shGAAarXC4PQTRxLfUHtyWkNbzGh31U/bKI3Snp1O78yOVEZSjdmWkDvkwtWKJQcoZBStgqw9oIPE0oG8uh49XoX77nVabi0JCf8zO63Ai4/9EdwyqE9xsJ+soOYt2V5+UCj0zsv1xpt2YhfYyXrzBv9x6k9lCxy0NW3Ik1mSW+OzSOT3tgDf36ujV/CI2i8ERM9civWy8FtFOReZMV2kkbj7fXI4E1wKM1RFIn7er1MseCSvbvh3o2uCO46/euqHAstYs5cL+4yB2qM/xKfHT5aocDjq7GBLKtree9xdNF0EE9CNr/+J5R";
62keyType="ssh_pubkey";
63if validateInput "$testKey" "$keyType"; then echo "Looks like a valid $keyType:\"$testKey\""; else echo "doesn't look like a valid $keyType:\"$testKey\""; fi
64echo "";
65
66testKey="blah-blah onetwothreeREEE";
67keyType="ssh_pubkey";
68if validateInput "$testKey" "$keyType"; then echo "Looks like a valid $keyType:\"$testKey\""; else echo "doesn't look like a valid $keyType:\"$testKey\""; fi
69echo "";
70
4cd70356 71testKey3="";
a3d13a32
SBS
72keyType="ssh_pubkey";
73if validateInput "$testKey" "$keyType"; then echo "Looks like a valid $keyType:\"$testKey\""; else echo "doesn't look like a valid $keyType:\"$testKey\""; fi
74echo "";
75
76testKey="age1c3s6huz6en6jh40cem0erc0m2un2ry85my0t0ujj4a5cu8gzkysq9l39e4";
77keyType="age_pubkey";
78if validateInput "$testKey" "$keyType"; then echo "Looks like a valid $keyType:\"$testKey\""; else echo "doesn't look like a valid $keyType:\"$testKey\""; fi
79echo "";
80
81testKey="123"
82keyType="integer";
83if validateInput "$testKey" "$keyType"; then echo "Looks like a valid $keyType:\"$testKey\""; else echo "doesn't look like a valid $keyType:\"$testKey\""; fi
84echo "";
85
86testKey="123abc"
87keyType="integer";
88if validateInput "$testKey" "$keyType"; then echo "Looks like a valid $keyType:\"$testKey\""; else echo "doesn't look like a valid $keyType:\"$testKey\""; fi
89echo "";
90
1cbe13ec
SBS
91testKey="year"
92keyType="time_element";
93if validateInput "$testKey" "$keyType"; then echo "Looks like a valid $keyType:\"$testKey\""; else echo "doesn't look like a valid $keyType:\"$testKey\""; fi
94echo "";
95
4cd70356
SBS
96#==END sample code==
97
4cd70356
SBS
98# Author: Steven Baltakatei Sandoval (bktei.com)
99# License: GPLv3+