From 36abb0e31244f55b7d2fa27adfda2b5fceda6f82 Mon Sep 17 00:00:00 2001 From: Nils Date: Wed, 26 Jan 2022 23:40:45 +0100 Subject: [PATCH] show possible target devices --- main.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/main.go b/main.go index 652ec18..beddb94 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,8 @@ import ( "time" "log" "strings" + "runtime" + "strconv" "github.com/schollz/progressbar/v3" "github.com/fatih/color" "github.com/briandowns/spinner" @@ -45,6 +47,7 @@ func GetPath() string { } func GetDest() string { + PrintAvail() dest, err := ac.Read("[ " + color.YellowString("i") + " ] Please input destination: ") if err != nil { log.Fatal(err) @@ -105,6 +108,43 @@ func Sync(image *os.File, target *os.File) error { return nil } +func PrintAvail() { + if runtime.GOOS == "linux" { + block, _ := os.ReadDir("/sys/block") + if len(block) == 0 { + return + } + var targets []string + for _, device := range block { + if strings.HasPrefix(device.Name(), "sd") { + targets = append(targets, device.Name()) + } + if strings.HasPrefix(device.Name(), "nvme") { + targets = append(targets, device.Name()) + } + if strings.HasPrefix(device.Name(), "vd") { + targets = append(targets, device.Name()) + } + } + for _, target := range targets { + sizefile, _ := os.Open("/sys/block/" + target + "/size") + sizeread, _ := io.ReadAll(sizefile) + _ = sizefile.Close() + sizestring := strings.ReplaceAll(string(sizeread), "\n", "") + size, _ := strconv.Atoi(sizestring) + size = size * 512 + size = size / 1024 / 1024 / 1024 + + fmt.Print(" * ", "/dev/" + target) + if size > 0 { + fmt.Print(" [", size, "GB]\n") + } else { + fmt.Println("") + } + } + } +} + func main() { s := spinner.New(spinner.CharSets[14], 100*time.Millisecond)