Skip to content

Commit 433ba23

Browse files
committed
fix: Windows 跨平台编译兼容 + 新增跨平台 Release 工作流
- session/detectDiskMB 拆分为平台文件:Unix 用 syscall.Statfs,Windows 用 GetDiskFreeSpaceEx - 新增 .github/workflows/release.yml:tag v* 触发,6 平台(win/darwin/linux × amd64/arm64)编译并发布 Release
1 parent 8ea6c47 commit 433ba23

4 files changed

Lines changed: 108 additions & 9 deletions

File tree

‎.github/workflows/release.yml‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
name: '跨平台 Release 打包'
2+
on:
3+
workflow_dispatch:
4+
push:
5+
tags:
6+
- 'v*'
7+
8+
permissions:
9+
contents: write
10+
11+
jobs:
12+
build:
13+
name: '构建 ${{ matrix.target }}'
14+
runs-on: ubuntu-latest
15+
strategy:
16+
fail-fast: false
17+
matrix:
18+
include:
19+
- { target: linux-amd64, goos: linux, goarch: amd64, ext: '' }
20+
- { target: linux-arm64, goos: linux, goarch: arm64, ext: '' }
21+
- { target: darwin-amd64, goos: darwin, goarch: amd64, ext: '' }
22+
- { target: darwin-arm64, goos: darwin, goarch: arm64, ext: '' }
23+
- { target: windows-amd64, goos: windows, goarch: amd64, ext: .exe }
24+
- { target: windows-arm64, goos: windows, goarch: arm64, ext: .exe }
25+
steps:
26+
- uses: actions/checkout@v4
27+
- uses: actions/setup-go@v5
28+
with:
29+
go-version: '1.25'
30+
- name: '编译 ${{ matrix.target }}'
31+
run: |
32+
mkdir -p dist
33+
CGO_ENABLED=0 GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} \
34+
go build -trimpath -ldflags="-s -w" -o dist/simple-web-shell${{ matrix.ext }} .
35+
- name: '打包'
36+
run: |
37+
cd dist
38+
if [ "${{ matrix.goos }}" = "windows" ]; then
39+
zip -qr ../simple-web-shell-${{ matrix.target }}.zip simple-web-shell.exe
40+
else
41+
tar czf ../simple-web-shell-${{ matrix.target }}.tar.gz simple-web-shell
42+
fi
43+
- uses: actions/upload-artifact@v4
44+
with:
45+
name: 'simple-web-shell-${{ matrix.target }}'
46+
path: 'simple-web-shell-${{ matrix.target }}.tar.gz'
47+
if-no-files-found: 'error'
48+
retention-days: 1
49+
50+
release:
51+
name: '发布 GitHub Release'
52+
needs: build
53+
runs-on: ubuntu-latest
54+
steps:
55+
- uses: actions/download-artifact@v4
56+
with:
57+
path: artifacts
58+
merge-multiple: true
59+
- name: '整理产物'
60+
run: |
61+
mkdir -p release
62+
find artifacts -type f | while read -r f; do
63+
case "$f" in
64+
*.tar.gz|*.zip) cp "$f" release/ ;;
65+
esac
66+
done
67+
- name: '上传到 Release'
68+
uses: softprops/action-gh-release@v2
69+
with:
70+
files: 'release/*'
71+
generate_release_notes: true
72+
fail_on_unmatched_files: false

‎session/disk_unix.go‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//go:build !windows
2+
3+
package session
4+
5+
import "syscall"
6+
7+
// detectDiskMB 尝试获取路径所在文件系统的容量(MB)。
8+
// Unix 实现:syscall.Statfs。
9+
func detectDiskMB(path string) int64 {
10+
var st syscall.Statfs_t
11+
if err := syscall.Statfs(path, &st); err == nil {
12+
total := uint64(st.Blocks) * uint64(st.Bsize)
13+
return int64(total / 1024 / 1024)
14+
}
15+
return 0
16+
}

‎session/disk_windows.go‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//go:build windows
2+
3+
package session
4+
5+
import "golang.org/x/sys/windows"
6+
7+
// detectDiskMB 尝试获取路径所在文件系统的容量(MB)。
8+
// Windows 实现:windows.GetDiskFreeSpaceEx。
9+
func detectDiskMB(path string) int64 {
10+
p, err := windows.UTF16PtrFromString(path)
11+
if err != nil {
12+
return 0
13+
}
14+
var total uint64
15+
if err := windows.GetDiskFreeSpaceEx(p, nil, &total, nil); err == nil {
16+
return int64(total / 1024 / 1024)
17+
}
18+
return 0
19+
}

‎session/session.go‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"strconv"
1414
"strings"
1515
"sync"
16-
"syscall"
1716
"time"
1817
)
1918

@@ -334,14 +333,7 @@ func detectMemoryMB() int64 {
334333
}
335334

336335
// detectDiskMB 尝试获取路径所在文件系统的容量(MB)。
337-
func detectDiskMB(path string) int64 {
338-
var st syscall.Statfs_t
339-
if err := syscall.Statfs(path, &st); err == nil {
340-
total := uint64(st.Blocks) * uint64(st.Bsize)
341-
return int64(total / 1024 / 1024)
342-
}
343-
return 0
344-
}
336+
// 平台相关实现见 disk_unix.go / disk_windows.go。
345337

346338
// defaultMetadata 填充通用元数据,失败忽略。
347339
func defaultMetadata(cwd, shell, gitBranch, owner string) map[string]string {

0 commit comments

Comments
 (0)