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

190 lines
4.5 KiB
Go
Raw Normal View History

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) {
bar := progressbar.NewOptions(int(size),
progressbar.OptionSetWriter(os.Stderr),
// progressbar.OptionEnableColorCodes(true),
progressbar.OptionShowBytes(true),
progressbar.OptionSetWidth(25),
progressbar.OptionSetDescription("Writing image file..."),
progressbar.OptionSetTheme(progressbar.Theme{
Saucer: "=",
SaucerHead: ">",
SaucerPadding: " ",
BarStart: "[",
BarEnd: "]",
}))
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 == "" {
2022-01-12 22:35:36 +00:00
if len(flag.Args()) == 0 {
input = GetPath()
} else if len(flag.Args()) > 0 {
input = flag.Args()[0]
}
2022-01-11 13:43:16 +00:00
}
if device == "" {
2022-01-12 22:35:36 +00:00
if len(flag.Args()) == 0 {
device = GetDest()
} else if len(flag.Args()) > 0 {
if input == flag.Args()[0] && len(flag.Args()) > 1 {
device = flag.Args()[1]
} else if input != flag.Args()[0] && len(flag.Args()) > 0 {
device = flag.Args()[0]
}
}
2022-01-11 13:43:16 +00:00
}
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 ")
}
fmt.Println("[", color.BlueString("i"), "] Input device/file:", input)
fmt.Println("[", color.BlueString("i"), "] Output device/file:", device)
fmt.Print(color.HiWhiteString("Do you want to continue? [y/N]: "))
var yesno string
_, _ = fmt.Scanln(&yesno)
yesno = strings.TrimSpace(yesno)
if ! (yesno == "y" || yesno == "Y") {
log.Fatal("aborted")
}
2022-01-09 15:49:43 +00:00
written, err := WriteImage(image, target, stat.Size())
2022-01-08 22:33:14 +00:00
if err != nil {
fmt.Println("\r[", color.RedString("✘"), "] Writing image,", written, "bytes written ")
2022-01-08 22:33:14 +00:00
log.Fatal(err)
} else {
fmt.Println("\r[", color.GreenString("✓"), "] Writing image,", written, "bytes written ")
2022-01-08 22:33:14 +00:00
}
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
}