blob: 3f6f1bb2a831b46424efe0a445977f56ca9da8a9 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
#!/bin/bash
# Show (a short piece of) information related to current location.
# Use a keyword to specify what info to show.
DIE() {
echo "==> $progname: error: $1"
exit 0
}
Options() {
local opts
local lopts="help,timeout:"
local sopts="ht:"
opts="$(/usr/bin/getopt -o=$sopts --longoptions $lopts --name "$progname" -- "$@")" || {
Options -h
return 1
}
eval set -- "$opts"
while true ; do
case "$1" in
-t | --timeout) timeout="$2" ; shift ;;
-h | --help)
cat <<EOF >&2
$progname - show info about your current location
Usage: $progname [options] [location-item]
Location-item: one of
${items[*]}
Location-item defaults to showing all available info.
Options:
-h, --help This help.
-t, --timeout Max number of seconds to wait for a response from
$infourl (default: $timeout_default).
EOF
exit 0
;;
--) shift ; break ;;
esac
shift
done
item="$1"
local it
for it in "${items[@]}" ; do
[ "$item" = "$it" ] && break
done
[ "$item" = "$it" ] || DIE "item name must be one of: ${items[*]}"
[ -n "$timeout" ] || DIE "given timeout value cannot be empty"
}
Main() {
local progname="$(basename "$0")"
# default values
local timeout_default=30
local timeout=$timeout_default
local item=""
# supported values
local items=(city country hostname ip loc org postal region timezone "") # "" means: all
# info source
local infourl=https://ipinfo.io
Options "$@"
# show the wanted location info
echo "$(LANG=C curl -Lsm $timeout $infourl/$item)"
}
Main "$@"
|