Skip to content

Commit

Permalink
fix: permission issue (#191)
Browse files Browse the repository at this point in the history
* fix: use sudo by default

* fix: error handling

* fix: macOS argument parsing failure

* fix: skip sudo for macOS CI
  • Loading branch information
jbwfu authored Jan 9, 2025
1 parent d34d8b4 commit 03a4054
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 29 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/macOS.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
- name: Run install.sh
run: |
chmod +x install.sh
./install.sh
./install.sh -k
- name: Run QQ on macOS, wait, and check LiteLoaderQQNT
run: |
Expand Down Expand Up @@ -76,7 +76,7 @@ jobs:
- name: ReRun install.sh
run: |
./install.sh
./install.sh -k
- name: Check LiteLoader
run: |
Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/unix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ jobs:
- name: Run install.sh
run: |
chmod +x install.sh
./install.sh
[[ "$RUNNER_OS" != "Linux" ]] && param='-k'
./install.sh $param
- name: Check LiteLoaderQQNT
run: |
Expand Down Expand Up @@ -91,7 +92,8 @@ jobs:
- name: ReRun install.sh
run: |
./install.sh
[[ "$RUNNER_OS" != "Linux" ]] && param='-k'
./install.sh $param
check_flatpak_qq:
Expand Down
44 changes: 19 additions & 25 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ function show_help() {
Usage: cmd [options]...
Options:
-k, --skip-sudo 强制跳过 sudo 提权
-h, --help 显示帮助信息
-f, --force 强制更新 LiteLoaderQQNT 及插件商店
EOF

[ "$PLATFORM" != "macos" ] && cat << EOF
--appimage[=<path>] 操作 AppImage,未指定路径时自动从官网下载
--with-plugin patch AppImage 后继续安装插件、写入环境变量
--ll-dir <options|path> 指定 LiteLoaderQQNT 本体存放路径
--ll-profile <path> 指定 LiteLoaderQQNT 数据存放路径(Linux only)
若 QQ 的安装方式支持,默认为
'\$HOME/.config/LiteLoaderQQNT'
-k, --skip-sudo 强制跳过 sudo 提权
-h, --help 显示帮助信息
-f, --force 强制更新 LiteLoaderQQNT 及插件商店
默认会自动检测系统,尝试可能的 QQ 安装方式,除默认安装方式外,现已支持:
AppImage,linglong(玲珑),flatpak
Expand Down Expand Up @@ -78,26 +81,13 @@ function check_dependencies() {

# 提升权限
function elevate_permissions() {
user=$(id -u)
[ "$SKIP_SUDO" = 0 ] && { log_info "跳过提权"; return 0; }
command -v sudo >/dev/null 2>&1 || { log_info "未找到 sudo 命令"; return 0; }

case "$PLATFORM" in
linux)
log_info "请输入您的密码以提升权限:"
sudo -v || { log_error "提权失败,请重试或添加 '-k' 参数以跳过提权,退出"; return 1; }
sudo_cmd="sudo" ;;
macos)
testfile="/Applications/test_permission_file"
if touch "$testfile" 2>/dev/null; then
log_info "终端具有对 /Applications 的管理权限。"
rm "$testfile"
else
log_info "终端没有对 /Applications 的管理权限。"
log_info "推荐赋予终端 完全磁盘访问权限 App管理权限。"
fi

sudo_cmd="" ;;
esac
log_info "请输入您的密码以提升权限:"
sudo -v || { log_error "提权失败,请重试或添加 '-k' 参数以跳过提权,退出"; return 1; }
sudo_cmd="sudo"
}

function sync_files() {
Expand Down Expand Up @@ -198,7 +188,8 @@ function download_and_extract() {

if download_url "$url"; then
[ -d "$extract_dir" ] && mv "$extract_dir" "${extract_dir}_bak"
if unzip -q "${url##*/}" -d "$extract_dir"; then
if $sudo_cmd unzip -q "${url##*/}" -d "$extract_dir"; then
$sudo_cmd chown -R "$user":"$user" "$extract_dir"
rm -rf "${extract_dir}_bak"
return 0
fi
Expand Down Expand Up @@ -370,7 +361,9 @@ function patch_resources() {

# 写入 require(String.raw`*`) 到 *.js 文件
log_info "正在创建/覆写文件:'$jsfile_path'"
echo "require(\"${ll_path%/}\");" | sudo tee "$jsfile_path" > /dev/null
if ! echo "require(\"${ll_path%/}\");" | $sudo_cmd tee "$jsfile_path" > /dev/null; then
log_error "写入失败,退出" && return 1
fi
log_info "写入成功:'require(\"${ll_path%/}\");'"

# 检查 package.json 文件是否存在
Expand All @@ -380,7 +373,7 @@ function patch_resources() {
# 修改 package.json 中的 main 字段为 ./app_launcher/launcher.js
log_info "正在修改文件:$package_json"
_tmp="$(cat "$package_json")"
if ! echo "$_tmp" | sed '/"main"/ s#"[^"]*",$#"./app_launcher/'"$jsfile_name"'",#' | sudo tee "$package_json" >/dev/null; then
if ! echo "$_tmp" | sed '/"main"/ s#"[^"]*",$#"./app_launcher/'"$jsfile_name"'",#' | $sudo_cmd tee "$package_json" >/dev/null; then
log_error "修改失败:$package_json"; return 1
fi
log_info "修改文件 main 字段成功:'./app_launcher/$jsfile_name'"
Expand Down Expand Up @@ -674,6 +667,8 @@ eval set -- "$OPTIONS"
unset OPTIONS

echo "$@" | grep -q -wE '(-h|--help)' && { show_help; exit 0; } # 优先显示帮助信息
echo "$@" | grep -q -wE '(-k|--skip-sudo)' && { SKIP_SUDO=0; } # 跳过 sudo
echo "$@" | grep -q -wE '(-f|--force)' && { INSTALL_FORCE=0; } # 覆盖安装

# 处理每个参数
while true; do
Expand All @@ -687,8 +682,7 @@ while true; do
--with-plugin) APPIMAGE_WITH_PLUGIN=0; shift 1 ;;
--ll-dir) LITELOADERQQNT_DIR="${2:-LITELOADERQQNT_DIR}"; shift 2 ;;
--ll-profile) LITELOADERQQNT_PROFILE="${2:-$LITELOADERQQNT_PROFILE}"; shift 2 ;;
-k|--skip-sudo) SKIP_SUDO=0; shift 1 ;;
-f|--force) INSTALL_FORCE=0; shift 1 ;;
-k|--skip-sudo|-f|--force) shift 1 ;;
--) shift; break ;;
*) log_error "未知选项 '$1'."; show_help; exit 1 ;;
esac
Expand Down

0 comments on commit 03a4054

Please sign in to comment.