-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.sh
139 lines (116 loc) · 3.44 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
function log {
echo "[${1}] ${2}"
}
function log_info {
log "INFO" "$@"
}
function log_warn {
log "WARN" "$@"
}
function log_error {
log "ERROR" "$@"
}
# verify ROOTDIR is set
if [ -z "$ROOTDIR" ]; then
log_error "Must set ROOTDIR before sourcing lib"
return 1
fi
export LOCALBIN="$HOME/.local/bin"
export LOCALOPT="$HOME/.local/opt"
export WEBI_WELCOME=0
export REMOTE_CONTAINERS="${REMOTE_CONTAINERS:-}"
function is_mac {
test $(uname -s) = "Darwin"
}
function canonical_path {
local path="$1"
if is_mac; then
python3 -c "import os;print(os.path.realpath('$path'))"
else
readlink -f "$path"
fi
}
# Sets up a simlink at $pointer pointing at $target
function safelink {
local pointer="$1"
local target="$(canonical_path "${2}")"
# verify args are non-empty
if [ -z "$pointer" ]; then
log_error "safelink: pointer is empty"
return 1
fi
if [ -z "$target" ]; then
log_error "safelink: target is empty"
return 1
fi
# make sure that the pointer's directory exists
mkdir -p $(dirname "$pointer")
# check to see if the pointer exists
if [[ -e "$pointer" ]]; then
# if it exists, figure out its canonical path
# if its not a symlink this will just return the abspath to $pointer
local current_target="$(canonical_path "$pointer")"
# if its pointing at the right spot, we are done
if [[ "$current_target" == "$target" ]]; then
log_info "[ok] ${pointer} -> ${target}"
return 0
fi
# otherwise, we need to warn the user
log_warn "[warning] ${pointer} already exists, it looks like:"
ls -l "$pointer"
else
log_info "[++] ${pointer} -> ${target}"
fi
if [[ "${REMOTE_CONTAINERS}" == "true" ]]; then
# in a devcontainer we just overwrite anything there
ln -f -s "$target" "$pointer"
log_info "[++] ${pointer} -> ${target}"
else
# setup the symlink, -i will ask the user before overwriting a symlink
ln -i -s "$target" "$pointer"
fi
return $?
}
should_update() {
return $(test ${UPDATE:-false} = "true")
}
install_from_tar_gz() {
local url="$1"
local binpath="$2"
local binname="${3:-$(basename "$binpath")}"
if ! should_update && [[ -e "${LOCALBIN}/${binname}" ]]; then
log_info "[ok] ${binname} already installed"
return 0
fi
mkdir -p "${LOCALOPT}/${binname}"
curl -Ls "${url}" | tar xzf - -C "${LOCALOPT}/${binname}"
safelink "${LOCALBIN}/${binname}" "${LOCALOPT}/${binname}/${binpath}"
log_info "[++] ${binname} installed"
}
install_binary() {
local url="$1"
local binname="$2"
if ! should_update && [[ -e "${LOCALBIN}/${binname}" ]]; then
log_info "[ok] ${binname} already installed"
return 0
fi
curl -Ls "${url}" > "${LOCALBIN}/${binname}"
chmod +x "${LOCALBIN}/${binname}"
log_info "[++] ${binname} installed"
}
webinstall() {
local name="$1"
local binname="${2:-${name}}"
binname="${binname%@*}"
# install webi if missing
if [[ ! -e "${LOCALBIN}/webi" ]]; then
curl -sS https://webinstall.dev/webi | bash >/dev/null
log_info "[++] webi installed"
fi
if should_update || ! command -v "${binname}" >/dev/null; then
log_info "installing ${binname}"
${LOCALBIN}/webi "${name}"
else
log_info "[ok] ${binname} already installed"
fi
}