mirror of
https://github.com/byReqz/go-etcher.git
synced 2024-12-25 12:41:49 +00:00
restructure functions and add sync
This commit is contained in:
parent
920921a51c
commit
2416b2ac09
80
main.go
80
main.go
@ -47,31 +47,7 @@ func GetDest() string {
|
||||
return dest
|
||||
}
|
||||
|
||||
func ReadImage(path string) ([]byte, 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
|
||||
}
|
||||
func WriteImage(image *os.File, target *os.File, size int64) (int64, error) {
|
||||
bar := progressbar.DefaultBytes(
|
||||
size,
|
||||
"Writing image",
|
||||
@ -84,6 +60,26 @@ func WriteImage(path string, destination string, size int64) (int64, error) {
|
||||
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() {
|
||||
s := spinner.New(spinner.CharSets[14], 100*time.Millisecond)
|
||||
s.Prefix = "[ "
|
||||
@ -104,7 +100,26 @@ func main() {
|
||||
s.Stop()
|
||||
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 {
|
||||
s.Stop()
|
||||
fmt.Println("\r[", color.RedString("✘"), "] Writing image,", written, "bytes written")
|
||||
@ -113,4 +128,17 @@ func main() {
|
||||
s.Stop()
|
||||
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 ")
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user