package main

import (
	"fmt"
	"os/exec"
	"strconv"
	"strings"
)

// getDiskUsage 解析 df 命令的输出
// df -B1 /dev/vdb
// Filesystem        1B-blocks        Used    Available Use% Mounted on
// /dev/vdb       527371075584 16390344704 484120408064   4% /mnt/disk001
func getDiskUsage(path string) (uint64, error) {
	cmd := exec.Command("df", "-B1", path)
	output, err := cmd.CombinedOutput()
	if err != nil {
		return 0, err
	}

	lines := strings.Split(string(output), "\n")
	if len(lines) < 2 {
		return 0, fmt.Errorf("unexpected output from df command")
	}

	fields := strings.Fields(lines[1])
	if len(fields) < 3 {
		return 0, fmt.Errorf("unexpected output format from df command")
	}
	parseUint, _ := strconv.ParseUint(fields[2], 10, 64)
	return parseUint, nil
}

func main() {
	path := "/dev/vdb" // 替换为您要查询的硬盘路径，如 "/dev/sda"

	used, err := getDiskUsage(path)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	fmt.Printf("Used Disk Space: %d bytes\n", used)
}
