1
0
mirror of https://github.com/byReqz/go-etcher.git synced 2024-11-22 06:51:15 +00:00

show possible target devices

This commit is contained in:
Nils 2022-01-26 23:40:45 +01:00
parent 552d561d3d
commit 36abb0e312
Signed by: byreqz
GPG Key ID: 396A62D7D436749E

40
main.go
View File

@ -6,6 +6,8 @@ import (
"time" "time"
"log" "log"
"strings" "strings"
"runtime"
"strconv"
"github.com/schollz/progressbar/v3" "github.com/schollz/progressbar/v3"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/briandowns/spinner" "github.com/briandowns/spinner"
@ -45,6 +47,7 @@ func GetPath() string {
} }
func GetDest() string { func GetDest() string {
PrintAvail()
dest, err := ac.Read("[ " + color.YellowString("i") + " ] Please input destination: ") dest, err := ac.Read("[ " + color.YellowString("i") + " ] Please input destination: ")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -105,6 +108,43 @@ func Sync(image *os.File, target *os.File) error {
return nil 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() { func main() {
s := spinner.New(spinner.CharSets[14], 100*time.Millisecond) s := spinner.New(spinner.CharSets[14], 100*time.Millisecond)