lazy/utils.sh
2024-08-02 19:16:07 +08:00

75 lines
1.6 KiB
Bash
Executable File

#!/usr/bin/env bash
git_clone() {
local repo=$1
local dir=$2
if [ ! -d "$dir" ]; then
if [ "$3" = "--depth=1" ]; then
git clone --depth=1 "$github/$repo" $dir
elif [ "$3" = "--norecursive" ]; then
git clone --single-branch "$github/$repo" $dir
else
git clone --single-branch --recursive "$github/$repo" $dir
fi
fi
}
# offline: 是否离线
# abroad: 是否海外
check_internet_access() {
offline=0
local response=$(curl -s -o /dev/null -w "%{http_code}" -m 3 "http://www.baidu.com")
if [ "$response" -ne 200 ]; then
offline=1
echo "Internet: offline"
return
fi
abroad=0
local response=$(curl -s -o /dev/null -w "%{http_code}" -m 3 "http://www.google.com")
if [ "$response" -eq 200 ]; then
abroad=1
echo "Internet: abroad"
github="https://github.com"
else
abroad=0
echo "Internet: internal"
github="https://g.nano.ac/https://github.com"
fi
}
confirm_action() {
local prompt="$1"
local default_response="$2"
read -p "$prompt [$default_response]: " response
if [[ -z "$response" ]]; then
response="$default_response"
fi
[[ $response =~ ^[Yy]$ ]]
}
command_exists() {
command -v "$@" >/dev/null 2>&1;
}
pkg_install() {
# 如果已经安装了,就直接返回(只检查第一个)
command_exists "$1" && return
if [ "$(uname)" = "Darwin" ]; then
brew install "$@"
else
sudo apt install "$@" -y
fi
}
# ============================================================
check_internet_access