mirror of
https://github.com/YspCoder/clawgo.git
synced 2026-04-19 14:17:38 +08:00
ci cross-platform fix: make system_info disk usage helper windows-safe with build tags
This commit is contained in:
@@ -6,7 +6,6 @@ import (
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
type SystemInfoTool struct{}
|
||||
@@ -87,15 +86,8 @@ func (t *SystemInfoTool) Execute(ctx context.Context, args map[string]interface{
|
||||
}
|
||||
}
|
||||
|
||||
// Disk usage for /
|
||||
var stat syscall.Statfs_t
|
||||
if err := syscall.Statfs("/", &stat); err == nil {
|
||||
bsize := uint64(stat.Bsize)
|
||||
total := stat.Blocks * bsize
|
||||
free := stat.Bfree * bsize
|
||||
used := total - free
|
||||
sb.WriteString(fmt.Sprintf("Disk (/): Used %.2f GB / Total %.2f GB (%.2f%%)\n",
|
||||
float64(used)/1024/1024/1024, float64(total)/1024/1024/1024, float64(used)/float64(total)*100))
|
||||
if diskLine := diskUsageRoot(); diskLine != "" {
|
||||
sb.WriteString(diskLine)
|
||||
}
|
||||
|
||||
return sb.String(), nil
|
||||
|
||||
22
pkg/tools/system_disk_unix.go
Normal file
22
pkg/tools/system_disk_unix.go
Normal file
@@ -0,0 +1,22 @@
|
||||
//go:build !windows
|
||||
|
||||
package tools
|
||||
|
||||
import "fmt"
|
||||
import "syscall"
|
||||
|
||||
func diskUsageRoot() string {
|
||||
var stat syscall.Statfs_t
|
||||
if err := syscall.Statfs("/", &stat); err != nil {
|
||||
return ""
|
||||
}
|
||||
bsize := uint64(stat.Bsize)
|
||||
total := stat.Blocks * bsize
|
||||
free := stat.Bfree * bsize
|
||||
used := total - free
|
||||
if total == 0 {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprintf("Disk (/): Used %.2f GB / Total %.2f GB (%.2f%%)\n",
|
||||
float64(used)/1024/1024/1024, float64(total)/1024/1024/1024, float64(used)/float64(total)*100)
|
||||
}
|
||||
7
pkg/tools/system_disk_windows.go
Normal file
7
pkg/tools/system_disk_windows.go
Normal file
@@ -0,0 +1,7 @@
|
||||
//go:build windows
|
||||
|
||||
package tools
|
||||
|
||||
func diskUsageRoot() string {
|
||||
return ""
|
||||
}
|
||||
Reference in New Issue
Block a user