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

restructure functions and add sync

This commit is contained in:
Nils 2022-01-09 16:49:43 +01:00
parent 920921a51c
commit 2416b2ac09
Signed by: byreqz
GPG Key ID: 396A62D7D436749E

80
main.go
View File

@ -47,31 +47,7 @@ func GetDest() string {
return dest return dest
} }
func ReadImage(path string) ([]byte, error) { func WriteImage(image *os.File, target *os.File, size int64) (int64, error) {
readfile, err := os.Open(path)
if err != nil {
return make([]byte, 0), err
}
bar := progressbar.DefaultBytes(
-1,
"Reading file",
)
content, err := io.ReadAll(io.TeeReader(readfile, bar))
if err != nil {
return make([]byte, 0), err
}
return content, err
}
func WriteImage(path string, destination string, size int64) (int64, error) {
image, err := os.Open(path)
if err != nil {
return 0, err
}
target, err := os.OpenFile(destination, os.O_RDWR, 0660)
if err != nil {
return 0, err
}
bar := progressbar.DefaultBytes( bar := progressbar.DefaultBytes(
size, size,
"Writing image", "Writing image",
@ -84,6 +60,26 @@ func WriteImage(path string, destination string, size int64) (int64, error) {
return written, err return written, err
} }
func Sync(image *os.File, target *os.File) error {
err := image.Sync()
if err != nil {
return err
}
err = target.Sync()
if err != nil {
return err
}
err = image.Close()
if err != nil {
return err
}
err = target.Close()
if err != nil {
return err
}
return nil
}
func main() { func main() {
s := spinner.New(spinner.CharSets[14], 100*time.Millisecond) s := spinner.New(spinner.CharSets[14], 100*time.Millisecond)
s.Prefix = "[ " s.Prefix = "[ "
@ -104,7 +100,26 @@ func main() {
s.Stop() s.Stop()
fmt.Println("\r[", color.GreenString("✓"), "] Getting file details ") fmt.Println("\r[", color.GreenString("✓"), "] Getting file details ")
} }
written, err := WriteImage(path, dest, stat.Size()) s.Prefix = "[ "
s.Suffix = " ] Opening files"
s.Start()
image, err := os.Open(path)
if err != nil {
s.Stop()
fmt.Println("\r[", color.RedString("✘"), "] Opening files ")
log.Fatal(err)
}
target, err := os.OpenFile(dest, os.O_RDWR, 0660)
if err != nil {
s.Stop()
fmt.Println("\r[", color.RedString("✘"), "] Opening files ")
log.Fatal(err)
} else {
s.Stop()
fmt.Println("\r[", color.GreenString("✓"), "] Opening files ")
}
written, err := WriteImage(image, target, stat.Size())
if err != nil { if err != nil {
s.Stop() s.Stop()
fmt.Println("\r[", color.RedString("✘"), "] Writing image,", written, "bytes written") fmt.Println("\r[", color.RedString("✘"), "] Writing image,", written, "bytes written")
@ -113,4 +128,17 @@ func main() {
s.Stop() s.Stop()
fmt.Println("\r[", color.GreenString("✓"), "] Writing image,", written, "bytes written") fmt.Println("\r[", color.GreenString("✓"), "] Writing image,", written, "bytes written")
} }
s.Prefix = "[ "
s.Suffix = " ] Syncing"
s.Start()
Sync(image, target)
if err != nil {
s.Stop()
fmt.Println("\r[", color.RedString("✘"), "] Syncing ")
log.Fatal(err)
} else {
s.Stop()
fmt.Println("\r[", color.GreenString("✓"), "] Syncing ")
}
} }