-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathlib.sh
executable file
·72 lines (65 loc) · 2 KB
/
lib.sh
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
#!/bin/bash
# Library of functions and variables shared by various scripts.
# Note that this needs to be sourced to work correctly.
# Include config to get access to branch information.
if [ -f "$(dirname "${0}")"/config.sh ]; then
source "$(dirname "${0}")"/config.sh
else
echo "Unable to include config.sh"
exit 1
fi
# We need to check if nvm is available and load it, because prerelease requires it.
# First, check if can be loaded from NVM_DIR/nvm.sh, else, try to load it from HOME/.nvm/nvm.sh,
# with error if none is available.
if [ -r "${NVM_DIR}/nvm.sh" ]; then
source "${NVM_DIR}/nvm.sh"
elif [ -r "${HOME}/.nvm/nvm.sh" ]; then
source "${HOME}/.nvm/nvm.sh"
else
echo "Unable to load nvm, please set NVM_DIR to the correct path."
exit 1
fi
# A few colours for output.
# Reset to normal.
N="$(tput sgr0)"
# Red.
R="$(tput setaf 1)"
# Green.
G="$(tput setaf 2)"
# Yellow.
Y="$(tput setaf 3)"
# Cyan.
C="$(tput setaf 6)"
# Bold.
bold="$(tput bold)"
# Normal.
normal="$(tput sgr0)"
# The base dir
mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
git_last_commit_hash() {
git log --pretty=format:'%H' -n 1
return 0
}
# To output anything from the scripts (content and, optionally, no line break).
output() {
if $_verbose; then
if [ -n "${2}" ]; then
echo -n "${1}"
else
echo "${1}"
fi
fi
}
# Given a UTC time in the HH:MM:SS format, returns the next unix seconds
# when it will be that UTC time.
function next_utc_time() {
local time="${1}" # Time in HH:MM:SS format.
local now # Current time in seconds.
local next # To calculate the next time in seconds.
now=$(LC_ALL=C date -u +%s)
next=$(LC_ALL=C date -u -d "$(LC_ALL=C date -u -d @"${now}" +"%Y-%m-%d $time")" +%s)
if [ "${now}" -gt "${next}" ]; then # If the time has already passed today.
next=$(LC_ALL=C date -u -d "$(LC_ALL=C date -u -d @"${next}") +1 day" +%s)
fi
echo "${next}" # Return the next time in seconds.
}