Skip to content

Commit

Permalink
fix(open_explorer): don't execute command on Termux (#718)
Browse files Browse the repository at this point in the history
* fix(open_explorer): don't execute command on Termux

* fix: build arm64 in the `test` CI
  • Loading branch information
WaterLemons2k authored Jun 3, 2023
1 parent 412777b commit 35d6ac7
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
14 changes: 11 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ on:
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
goarch: [amd64, arm64]
steps:
- name: Checkout
uses: actions/checkout@v3
Expand All @@ -17,10 +20,15 @@ jobs:
go-version-file: 'go.mod'

- name: Test
run: make build test

run: |
# Run tests only when GOARCH is amd64, otherwize run builds only.
if [ "${{ matrix.goarch }}" = "amd64" ]; then
make build test
else
GOARCH=${{ matrix.goarch }} make build
fi
- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: ddns-go
name: ddns-go_${{ matrix.goarch }}
path: ddns-go
6 changes: 6 additions & 0 deletions util/open_explorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ func OpenExplorer(url string) {
case "darwin": // macOS
cmd = exec.Command("open", url)
default: // Linux
// 如果在 Termux 中运行则停止,因为 exec.Command 可能会触发 SIGSYS: bad system call
// https://github.com/docker/compose/issues/10511#issuecomment-1531453435
if isTermux() {
return
}

cmd = exec.Command("xdg-open", url)
}

Expand Down
10 changes: 10 additions & 0 deletions util/termux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package util

import "os"

// isTermux 是否在 Termux 中运行
//
// https://wiki.termux.com/wiki/Getting_started
func isTermux() bool {
return os.Getenv("PREFIX") == "/data/data/com.termux/files/usr"
}
23 changes: 23 additions & 0 deletions util/termux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package util

import (
"os"
"testing"
)

// TestIsTermux 测试在或不在 Termux 中运行都能正确判断
func TestIsTermux(t *testing.T) {
// 模拟在 Termux 中运行
os.Setenv("PREFIX", "/data/data/com.termux/files/usr")

if !isTermux() {
t.Error("期待 isTermux 返回 true,但得到 false。")
}

// 清除 PREFIX 变量,模拟不在 Termux 中运行
os.Unsetenv("PREFIX")

if isTermux() {
t.Error("期待 isTermux 返回 false,但得到 true。")
}
}

0 comments on commit 35d6ac7

Please sign in to comment.