diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index c9e0d10..af7eedf 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,4 +1,4 @@ -name: Build and Attach Binaries to Release +name: Release Binaries on: release: @@ -13,8 +13,15 @@ jobs: strategy: matrix: - goos: [windows, linux] - goarch: [amd64, arm64] + exclude: + - goos: windows + goarch: arm + - goos: darwin + goarch: 386 + - goos: darwin + goarch: arm + goos: [windows, linux, darwin, freebsd] + goarch: [amd64, arm64, 386, arm] steps: - name: Checkout code @@ -35,6 +42,9 @@ jobs: BINARY_NAME=komari-agent-${{ matrix.goos }}-${{ matrix.goarch }} if [ "${{ matrix.goos }}" = "windows" ]; then BINARY_NAME=${BINARY_NAME}.exe + elif [ "${{ matrix.goos }}" = "darwin" ]; then + # macOS binaries typically do not have a file extension + true fi go build -trimpath -ldflags="-s -w -X github.com/komari-monitor/komari-agent/update.CurrentVersion=${VERSION}" -o $BINARY_NAME @@ -45,6 +55,8 @@ jobs: BINARY_NAME=komari-agent-${{ matrix.goos }}-${{ matrix.goarch }} if [ "${{ matrix.goos }}" = "windows" ]; then BINARY_NAME=${BINARY_NAME}.exe + elif [ "${{ matrix.goos }}" = "darwin" ]; then + true fi gh release upload ${{ github.event.release.tag_name }} $BINARY_NAME --repo ${{ github.repository }} shell: bash diff --git a/.gitignore b/.gitignore index a48f75f..94ec8a5 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ agent.json komari-agent.exe komari-agent .komari-agent.exe.old +build/ \ No newline at end of file diff --git a/build_all.sh b/build_all.sh new file mode 100644 index 0000000..b366384 --- /dev/null +++ b/build_all.sh @@ -0,0 +1,62 @@ +#!/bin/bash + +# 定义颜色 +RED='\033[0;31m' +GREEN='\033[0;32m' +WHITE='\033[0;37m' +NC='\033[0m' # 无颜色 + +# 定义操作系统和架构列表 +OS_LIST=("windows" "linux" "darwin" "freebsd") +ARCH_LIST=("amd64" "arm64" "386" "arm") + +# 创建构建目录 +mkdir -p ./build + +# 获取当前版本 +VERSION=$(git describe --tags --abbrev=0 2>/dev/null || echo "dev") + +# 初始化失败列表 +FAILED_BUILDS=() + +# 遍历操作系统和架构组合 +for GOOS in "${OS_LIST[@]}"; do + for GOARCH in "${ARCH_LIST[@]}"; do + # 排除 windows/arm, darwin/386 和 darwin/arm + if { [ "$GOOS" = "windows" ] && [ "$GOARCH" = "arm" ]; } || \ + { [ "$GOOS" = "darwin" ] && { [ "$GOARCH" = "386" ] || [ "$GOARCH" = "arm" ]; }; }; then + continue + fi + + echo -e "Building for $GOOS/$GOARCH..." + + # 设置输出二进制文件名 + BINARY_NAME="komari-agent-${GOOS}-${GOARCH}" + if [ "$GOOS" = "windows" ]; then + BINARY_NAME="${BINARY_NAME}.exe" + fi + + # 构建二进制文件 + env GOOS=$GOOS GOARCH=$GOARCH CGO_ENABLED=0 go build -trimpath -ldflags="-s -w -X github.com/komari-monitor/komari-agent/update.CurrentVersion=${VERSION}" -o "./build/$BINARY_NAME" + + if [ $? -ne 0 ]; then + echo -e "${RED}Failed to build for $GOOS/$GOARCH${NC}" + FAILED_BUILDS+=("$GOOS/$GOARCH") + else + echo -e "${GREEN}Successfully built $BINARY_NAME${NC}" + fi + done +done + +# 输出失败的构建 +if [ ${#FAILED_BUILDS[@]} -ne 0 ]; then + echo -e "\n${RED}The following builds failed:${NC}" + for BUILD in "${FAILED_BUILDS[@]}"; do + echo -e "${RED}- $BUILD${NC}" + done +else + echo -e "\n${GREEN}All builds completed successfully.${NC}" +fi + +# 提示构建完成 +echo -e "\nBinaries are in the ./build directory." diff --git a/monitoring/unit/gpu_darwin.go b/monitoring/unit/gpu_darwin.go new file mode 100644 index 0000000..d31658d --- /dev/null +++ b/monitoring/unit/gpu_darwin.go @@ -0,0 +1,28 @@ +//go:build darwin +// +build darwin + +package monitoring + +import ( + "os/exec" + "strings" +) + +// GpuName returns the name of the GPU on Darwin (macOS) +func GpuName() string { + cmd := exec.Command("system_profiler", "SPDisplaysDataType") + output, err := cmd.Output() + if err != nil { + return "Unknown" + } + + lines := strings.Split(string(output), "\n") + for _, line := range lines { + line = strings.TrimSpace(line) + if strings.HasPrefix(line, "Chipset Model:") { + return strings.TrimSpace(strings.TrimPrefix(line, "Chipset Model:")) + } + } + + return "Unknown" +} diff --git a/monitoring/unit/gpu_freebsd.go b/monitoring/unit/gpu_freebsd.go new file mode 100644 index 0000000..adb66dc --- /dev/null +++ b/monitoring/unit/gpu_freebsd.go @@ -0,0 +1,28 @@ +//go:build freebsd +// +build freebsd + +package monitoring + +import ( + "os/exec" + "strings" +) + +// GpuName returns the name of the GPU on FreeBSD +func GpuName() string { + cmd := exec.Command("pciconf", "-lv") + output, err := cmd.Output() + if err != nil { + return "Unknown" + } + + lines := strings.Split(string(output), "\n") + for _, line := range lines { + line = strings.TrimSpace(line) + if strings.Contains(line, "VGA") || strings.Contains(line, "Display") { + return line + } + } + + return "Unknown" +} diff --git a/monitoring/unit/os_darwin.go b/monitoring/unit/os_darwin.go new file mode 100644 index 0000000..94c552f --- /dev/null +++ b/monitoring/unit/os_darwin.go @@ -0,0 +1,21 @@ +//go:build darwin +// +build darwin + +package monitoring + +import ( + "os/exec" + "strings" +) + +// OSName returns the name of the operating system on Darwin (macOS) +func OSName() string { + cmd := exec.Command("sw_vers", "-productName") + output, err := cmd.Output() + if err != nil { + return "macOS" + } + + name := strings.TrimSpace(string(output)) + return name +} diff --git a/monitoring/unit/os_freebsd.go b/monitoring/unit/os_freebsd.go new file mode 100644 index 0000000..e572555 --- /dev/null +++ b/monitoring/unit/os_freebsd.go @@ -0,0 +1,21 @@ +//go:build freebsd +// +build freebsd + +package monitoring + +import ( + "os/exec" + "strings" +) + +// OSName returns the name of the operating system on FreeBSD +func OSName() string { + cmd := exec.Command("uname", "-sr") + output, err := cmd.Output() + if err != nil { + return "FreeBSD" + } + + name := strings.TrimSpace(string(output)) + return name +} diff --git a/monitoring/unit/process_darwin.go b/monitoring/unit/process_darwin.go new file mode 100644 index 0000000..7c801fa --- /dev/null +++ b/monitoring/unit/process_darwin.go @@ -0,0 +1,27 @@ +//go:build darwin +// +build darwin + +package monitoring + +import ( + "os/exec" + "strings" +) + +// ProcessCount returns the number of running processes on Darwin (macOS) +func ProcessCount() (count int) { + return processCountDarwin() +} + +// processCountDarwin counts processes using the `ps` command +func processCountDarwin() (count int) { + cmd := exec.Command("ps", "-A") + output, err := cmd.Output() + if err != nil { + return 0 + } + + // Count the number of lines in the output, excluding the header line + lines := strings.Split(string(output), "\n") + return len(lines) - 1 +} diff --git a/monitoring/unit/process_freebsd.go b/monitoring/unit/process_freebsd.go new file mode 100644 index 0000000..dcac9c1 --- /dev/null +++ b/monitoring/unit/process_freebsd.go @@ -0,0 +1,27 @@ +//go:build freebsd +// +build freebsd + +package monitoring + +import ( + "os/exec" + "strings" +) + +// ProcessCount returns the number of running processes on FreeBSD +func ProcessCount() (count int) { + return processCountFreeBSD() +} + +// processCountFreeBSD counts processes using the `ps` command +func processCountFreeBSD() (count int) { + cmd := exec.Command("ps", "-ax") + output, err := cmd.Output() + if err != nil { + return 0 + } + + // Count the number of lines in the output, excluding the header line + lines := strings.Split(string(output), "\n") + return len(lines) - 1 +}