2022-01-08 22:33:14 +00:00
|
|
|
package main
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
"github.com/schollz/progressbar/v3"
|
|
|
|
"github.com/fatih/color"
|
|
|
|
"github.com/briandowns/spinner"
|
2022-01-11 13:43:16 +00:00
|
|
|
flag "github.com/spf13/pflag"
|
2022-01-08 22:33:14 +00:00
|
|
|
)
|
|
|
|
|
2022-01-11 13:43:16 +00:00
|
|
|
var device string
|
|
|
|
var input string
|
|
|
|
var force bool
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
flag.StringVarP(&device, "device", "d", "", "target device")
|
|
|
|
flag.StringVarP(&input, "input", "i", "", "input file")
|
|
|
|
flag.BoolVarP(&force, "force", "f", false, "override safety features")
|
|
|
|
flag.Parse()
|
|
|
|
}
|
|
|
|
|
2022-01-08 22:33:14 +00:00
|
|
|
func GetPath() string {
|
2022-01-11 13:43:16 +00:00
|
|
|
fmt.Print("[ ", color.YellowString("i"), " ] Please input your image file: ")
|
2022-01-08 22:33:14 +00:00
|
|
|
var path string
|
|
|
|
_, err := fmt.Scanln(&path)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
path = strings.TrimSpace(path)
|
|
|
|
if strings.HasPrefix(path, "~/") {
|
|
|
|
homedir, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return homedir + path[1:]
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetDest() string {
|
2022-01-11 13:43:16 +00:00
|
|
|
fmt.Print("[ ", color.YellowString("i"), " ] Please input destination: ")
|
2022-01-08 22:33:14 +00:00
|
|
|
var dest string
|
|
|
|
_, err := fmt.Scanln(&dest)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
dest = strings.TrimSpace(dest)
|
|
|
|
if strings.HasPrefix(dest, "~/") {
|
|
|
|
homedir, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return homedir + dest[1:]
|
|
|
|
}
|
|
|
|
return dest
|
|
|
|
}
|
|
|
|
|
2022-01-09 15:49:43 +00:00
|
|
|
func WriteImage(image *os.File, target *os.File, size int64) (int64, error) {
|
2022-01-08 22:33:14 +00:00
|
|
|
bar := progressbar.DefaultBytes(
|
2022-01-09 15:49:43 +00:00
|
|
|
size,
|
|
|
|
"Writing image",
|
2022-01-08 22:33:14 +00:00
|
|
|
)
|
2022-01-09 15:49:43 +00:00
|
|
|
writer := io.MultiWriter(target, bar)
|
|
|
|
written, err := io.Copy(writer, image)
|
2022-01-08 22:33:14 +00:00
|
|
|
if err != nil {
|
2022-01-09 15:49:43 +00:00
|
|
|
return 0, err
|
2022-01-08 22:33:14 +00:00
|
|
|
}
|
2022-01-09 15:49:43 +00:00
|
|
|
return written, err
|
2022-01-08 22:33:14 +00:00
|
|
|
}
|
|
|
|
|
2022-01-09 15:49:43 +00:00
|
|
|
func Sync(image *os.File, target *os.File) error {
|
|
|
|
err := image.Sync()
|
2022-01-08 22:33:14 +00:00
|
|
|
if err != nil {
|
2022-01-09 15:49:43 +00:00
|
|
|
return err
|
2022-01-08 22:33:14 +00:00
|
|
|
}
|
2022-01-09 15:49:43 +00:00
|
|
|
err = target.Sync()
|
2022-01-08 22:33:14 +00:00
|
|
|
if err != nil {
|
2022-01-09 15:49:43 +00:00
|
|
|
return err
|
2022-01-08 22:33:14 +00:00
|
|
|
}
|
2022-01-09 15:49:43 +00:00
|
|
|
err = image.Close()
|
2022-01-08 22:33:14 +00:00
|
|
|
if err != nil {
|
2022-01-09 15:49:43 +00:00
|
|
|
return err
|
2022-01-08 22:33:14 +00:00
|
|
|
}
|
2022-01-09 15:49:43 +00:00
|
|
|
err = target.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2022-01-08 22:33:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
s := spinner.New(spinner.CharSets[14], 100*time.Millisecond)
|
|
|
|
|
2022-01-11 13:43:16 +00:00
|
|
|
if input == "" {
|
|
|
|
input = GetPath()
|
|
|
|
}
|
|
|
|
|
|
|
|
if device == "" {
|
|
|
|
device = GetDest()
|
|
|
|
}
|
2022-01-09 15:56:13 +00:00
|
|
|
|
|
|
|
s.Prefix = "[ "
|
|
|
|
s.Suffix = " ] Getting file details"
|
2022-01-08 22:33:14 +00:00
|
|
|
s.Start()
|
2022-01-11 13:43:16 +00:00
|
|
|
stat, err := os.Stat(input)
|
2022-01-08 22:33:14 +00:00
|
|
|
if err != nil {
|
|
|
|
s.Stop()
|
|
|
|
fmt.Println("\r[", color.RedString("✘"), "] Getting file details ")
|
|
|
|
log.Fatal(err)
|
|
|
|
} else {
|
|
|
|
s.Stop()
|
|
|
|
fmt.Println("\r[", color.GreenString("✓"), "] Getting file details ")
|
|
|
|
}
|
2022-01-09 15:56:13 +00:00
|
|
|
|
2022-01-09 15:49:43 +00:00
|
|
|
s.Prefix = "[ "
|
|
|
|
s.Suffix = " ] Opening files"
|
|
|
|
s.Start()
|
2022-01-11 13:43:16 +00:00
|
|
|
image, err := os.Open(input)
|
2022-01-09 15:49:43 +00:00
|
|
|
if err != nil {
|
|
|
|
s.Stop()
|
|
|
|
fmt.Println("\r[", color.RedString("✘"), "] Opening files ")
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2022-01-11 13:43:16 +00:00
|
|
|
target, err := os.OpenFile(device, os.O_RDWR, 0660)
|
2022-01-09 15:49:43 +00:00
|
|
|
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())
|
2022-01-08 22:33:14 +00:00
|
|
|
if err != nil {
|
|
|
|
s.Stop()
|
|
|
|
fmt.Println("\r[", color.RedString("✘"), "] Writing image,", written, "bytes written")
|
|
|
|
log.Fatal(err)
|
|
|
|
} else {
|
|
|
|
s.Stop()
|
|
|
|
fmt.Println("\r[", color.GreenString("✓"), "] Writing image,", written, "bytes written")
|
|
|
|
}
|
2022-01-09 15:49:43 +00:00
|
|
|
|
|
|
|
s.Prefix = "[ "
|
|
|
|
s.Suffix = " ] Syncing"
|
|
|
|
s.Start()
|
2022-01-09 15:56:13 +00:00
|
|
|
err = Sync(image, target)
|
2022-01-09 15:49:43 +00:00
|
|
|
if err != nil {
|
|
|
|
s.Stop()
|
|
|
|
fmt.Println("\r[", color.RedString("✘"), "] Syncing ")
|
|
|
|
log.Fatal(err)
|
|
|
|
} else {
|
|
|
|
s.Stop()
|
|
|
|
fmt.Println("\r[", color.GreenString("✓"), "] Syncing ")
|
|
|
|
}
|
2022-01-08 22:33:14 +00:00
|
|
|
}
|