Skip to content

Commit 77843f3

Browse files
author
Giles Westwood
committed
merge some updates from core project
1 parent d05f406 commit 77843f3

File tree

2 files changed

+230
-29
lines changed

2 files changed

+230
-29
lines changed

lib/file.sh

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,15 @@ sk-file-older-than(){
2626
local file=${1:-/var/tmp/} days=${2:-1}
2727
sk_help "Usage: $FUNCNAME <file> <days>" "$@" && return 1
2828

29-
[[ ! -f "$file" ]] && return 1
29+
if [[ -f "$file" ]];then
30+
foo=bar
31+
elif [[ -d "$file" ]];then
32+
foo=bar
33+
else
34+
log "file or dir does not exist"
35+
return 1
36+
fi
37+
3038
if test "`find $file -mtime +$days`";then
3139
return 0
3240
else
@@ -207,36 +215,9 @@ sk-file-between-dates(){
207215
echo $result
208216
}
209217

210-
211-
212-
sk-file-readlink(){
213-
sk_help_noarg "$FUNCNAME: <file>. Macos compatible version of readlink to return the full path of file" "$@" && return
214-
local target=$1
215-
local dir=''
216-
current_dir=`pwd`
217-
cd $(dirname "$target")
218-
target=$(basename "$target")
219-
220-
# Iterate down a (possible) chain of symlinks
221-
while [ -L "$target" ]
222-
do
223-
target=$(readlink "$target")
224-
cd $(dirname "$target")
225-
target=$(basename "$target")
226-
done
227-
228-
# Compute the canonicalized name by finding the physical path
229-
# for the directory we're in and appending the target file.
230-
dir=`pwd -P`
231-
result="$dir/$target"
232-
cd $current_dir
233-
echo $result
234-
235-
}
236-
237218
sk-file-ls-tree() {
238219
for d in $@; do
239-
d=$(sk-file-readlink "$d")
220+
d=$(sk-readlink-f "$d")
240221
(
241222
while [ "$d" != "/" ]; do
242223
echo $d
@@ -303,6 +284,27 @@ sk-file-sum-mb(){
303284
ls -FaGl --block-size=M "${@}" | awk '{ total += $4 }; END { print total }'
304285
}
305286

287+
sk-file-perms-user-read(){
288+
sk_help_noarg "Usage: $FUNCNAME <file>. Check if file is readable only by the current user" "$@" && return
289+
local file=${1:-/tmp}
290+
local mode
291+
292+
if [[ "$OSTYPE" == "darwin"* ]]; then
293+
# macOS uses BSD stat
294+
mode=$(stat -f "%p" "$file")
295+
else
296+
# Linux uses GNU stat
297+
mode=$(stat -c "%a" "$file")
298+
fi
299+
300+
if [[ "$mode" == "100400" || "$mode" == "400" ]]; then
301+
foo='bar'
302+
else
303+
echo "File $file has permissions that are too open and not 400"
304+
return 1
305+
fi
306+
}
307+
306308
sk-file-exists(){
307309
sk_help "Usage: -f <file_to_check>. Check if a file exists and return true if it does
308310
optional:

lib/sys.sh

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,128 @@
33
# General systems functions
44
#
55
#-------------------------------------------------------------------------------
6+
alias pc="pre-commit"
7+
alias pci="pre-commit install"
8+
alias pca="sk-pre-commit-all"
9+
10+
sk-group-user-uid-highest(){
11+
group_name="${1:-wibble}"
12+
13+
# List all users in the group
14+
group_users=$(getent group "$group_name" | awk -F: '{print $4}' | tr ',' '\n')
15+
16+
# Initialize the highest UID variable
17+
highest_uid=-1
18+
19+
# Loop through each user and find their UID
20+
for user in $group_users; do
21+
user_uid=$(id -u "$user")
22+
if [ "$user_uid" -gt "$highest_uid" ]; then
23+
highest_uid=$user_uid
24+
fi
25+
done
26+
27+
# Output the highest UID
28+
echo "The highest UID in the group '$group_name' is: $highest_uid"
29+
}
30+
31+
sk-pushover(){
32+
sk-config-read -c .pushover.conf
33+
curl -s \
34+
--form-string "token=${APP_TOKEN}" \
35+
--form-string "user=${USER_KEY}" \
36+
--form-string "message=$@" \
37+
https://api.pushover.net/1/messages.json
38+
}
39+
40+
sk-hostname-noid(){
41+
hostname | awk -F '-' '{print $1"-"$2"-"$3"-"$4}'
42+
}
43+
44+
sk-motd(){
45+
sudo bash -c 'run-parts /etc/update-motd.d/ > /var/run/motd.dynamic'
46+
cat /var/run/motd.dynamic
47+
}
48+
49+
sk-cron-is(){
50+
if [ -t 1 ] ; then
51+
false
52+
else
53+
true
54+
fi
55+
}
56+
57+
sk-interactive(){
58+
if [ -t 1 ] ; then
59+
true
60+
else
61+
false
62+
fi
63+
}
64+
65+
sk-jinjafier() {
66+
sk_help_noarg "$FUNCNAME: <properties_file>. Convert a properties files into a jina2 template" "$@" && return
67+
local propfile=${1:-example.properties}
68+
69+
sk-asdf-install jinjafier -p jinjafier -v latest --plugin_git_url https://github.com/ORCID/asdf-jinjafier.git --silent
70+
jinjafier $propfile
71+
}
72+
73+
sk-dos2unix(){
74+
sk-pack-install dos2unix -p dos2unix
75+
dos2unix $@
76+
}
77+
78+
sk-restic-restore-all-latest(){
79+
for restore_script in `sudo ls -1 /root/restic/ | grep restore | grep -v auto`;do
80+
echo "/root/restic/$restore_script"
81+
sudo /root/restic/$restore_script
82+
done
83+
}
84+
85+
sk-restic-path-list(){
86+
sk_help "Usage: Extract path name from each restore file" "$@" && return
87+
sudo ls -1 /root/restic/ | grep restore | grep -v auto | perl -pe 's/restore-//g'
88+
}
89+
90+
sk-restic-path-list-first(){
91+
sk_help "Usage: Extract path name from each restore file and list the first one only" "$@" && return
92+
sudo ls -1 /root/restic/ | grep restore | grep -v auto | perl -pe 's/restore-//g' | head -1
93+
}
94+
95+
sk-restic-restore-snapshot-id(){
96+
sk_help_noarg "Usage: <snapshot_id> <path>(first) Restore a specific snapshot id" "$@" && return
97+
local restic_backup_path=${2:-$(sk-restic-path-list-first)}
98+
local access_file=$(sudo ls -1 /root/restic/ | grep access | grep $restic_backup_path)
99+
echo /root/restic/$access_file
100+
source <(sudo cat /root/restic/$access_file)
101+
restic restore "$1"
102+
}
103+
104+
sk-restic-snapshot-list(){
105+
sk_help "Usage: <snapshot_id> <path>(first) Restore a specific snapshot id" "$@" && return
106+
local restic_backup_path=${2:-$(sk-restic-path-list-first)}
107+
local access_file=$(sudo ls -1 /root/restic/ | grep access | grep $restic_backup_path)
108+
source <(sudo cat /root/restic/$access_file)
109+
echo /root/restic/$access_file
110+
restic snapshots
111+
}
112+
113+
sk-restic-unlock(){
114+
access_file=$(sudo ls -1 /root/restic/ | grep access | tail -1)
115+
source <(sudo cat /root/restic/$access_file)
116+
echo "restic unlock"
117+
restic unlock
118+
}
119+
120+
sk-pre-commit-dir(){
121+
sk_help_noarg "$FUNCNAME: <dir>. Run pre-commit on all files in a dir" "$@" && return
122+
git ls-files -- $1 | xargs pre-commit run --files
123+
}
124+
125+
sk-pre-commit-all(){
126+
pre-commit run --all-files
127+
}
6128

7129
sk-ntp-fix(){
8130
sudo service chrony stop
@@ -27,6 +149,25 @@ sk-sys-cores(){
27149
fi
28150
}
29151

152+
sk-awk(){
153+
if [[ "$PLATFORM" == 'Darwin' ]];then
154+
sk-pack-install gawk -p gawk
155+
gawk $@
156+
else
157+
awk $@
158+
fi
159+
}
160+
161+
162+
sk-grep(){
163+
if [[ "$PLATFORM" == 'Darwin' ]];then
164+
sk-pack-install ggrep -p grep
165+
ggrep $@
166+
else
167+
grep $@
168+
fi
169+
}
170+
30171
sk-datediff-setup(){
31172
if [[ "$PLATFORM" == 'Darwin' ]];then
32173
sk-pack-install datediff -p dateutils
@@ -35,6 +176,37 @@ sk-datediff-setup(){
35176
fi
36177
}
37178

179+
# needs to run as sudo
180+
sk-tcptraceroute-port(){
181+
if [[ "$PLATFORM" == 'Darwin' ]];then
182+
sk-pack-install tcptraceroute
183+
sudo tcptraceroute $@
184+
else
185+
sk-pack-install traceroute
186+
sudo traceroute -T $@
187+
fi
188+
}
189+
190+
sk-tcptraceroute(){
191+
if [[ "$PLATFORM" == 'Darwin' ]];then
192+
sk-pack-install tcptraceroute
193+
tcptraceroute $@
194+
else
195+
sk-pack-install traceroute
196+
traceroute -T $@
197+
fi
198+
}
199+
200+
sk-traceroute(){
201+
if [[ "$PLATFORM" == 'Darwin' ]];then
202+
sk-pack-install gtraceroute -p inetutils
203+
gtraceroute $@
204+
else
205+
sk-pack-install traceroute
206+
traceroute $@
207+
fi
208+
}
209+
38210
sk-sed(){
39211
if [[ "$PLATFORM" == 'Darwin' ]];then
40212
sk-pack-install gsed -p gnu-sed
@@ -44,6 +216,33 @@ sk-sed(){
44216
fi
45217
}
46218

219+
sk-readlink-f(){
220+
if [[ "$PLATFORM" == 'Darwin' ]];then
221+
sk-pack-install greadlink -p coreutils
222+
greadlink -f $@
223+
else
224+
readlink -f $@
225+
fi
226+
}
227+
228+
sk-readlink-e(){
229+
if [[ "$PLATFORM" == 'Darwin' ]];then
230+
sk-pack-install greadlink -p coreutils
231+
greadlink -e $@
232+
else
233+
readlink -e $@
234+
fi
235+
}
236+
237+
sk-realpath(){
238+
if [[ "$PLATFORM" == 'Darwin' ]];then
239+
sk-pack-install grealpath -p coreutils
240+
grealpath $@
241+
else
242+
realpath $@
243+
fi
244+
}
245+
47246
# bring macos into compat with linux
48247
sk-date(){
49248
if [[ "$PLATFORM" == 'Darwin' ]];then

0 commit comments

Comments
 (0)