mirror of
https://github.com/byReqz/go-etcher.git
synced 2025-07-03 03:20:49 +00:00
Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
4e655c43da |
@ -1,283 +0,0 @@
|
||||
// package cetcher is the cli app for the go-etcher project
|
||||
package cetcher
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
ac "github.com/JoaoDanielRufino/go-input-autocomplete"
|
||||
"github.com/briandowns/spinner"
|
||||
"github.com/byReqz/go-etcher/etcher"
|
||||
"github.com/fatih/color"
|
||||
"github.com/schollz/progressbar/v3"
|
||||
flag "github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
var device string
|
||||
var input string
|
||||
var force bool
|
||||
var disable_hash 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.BoolVarP(&disable_hash, "no-hash", "n", false, "disable hash verification")
|
||||
flag.Parse()
|
||||
}
|
||||
|
||||
func GetPath() string {
|
||||
path, err := ac.Read("[ " + color.YellowString("i") + " ] Please input your image file: ")
|
||||
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:]
|
||||
}
|
||||
if path == "" {
|
||||
fmt.Println("[", color.RedString("!"), "] No image given, retrying.")
|
||||
path = GetPath()
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
func GetDest() string {
|
||||
PrintAvail()
|
||||
dest, err := ac.Read("[ " + color.YellowString("i") + " ] Please input destination: ")
|
||||
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:]
|
||||
}
|
||||
if dest == "" {
|
||||
fmt.Println("[", color.RedString("!"), "] No destination given, retrying.")
|
||||
dest = GetDest()
|
||||
}
|
||||
return dest
|
||||
}
|
||||
|
||||
func WriteImage(image *os.File, target *os.File, size int64) (int64, error) {
|
||||
bar := progressbar.NewOptions(int(size),
|
||||
progressbar.OptionSetWriter(os.Stderr),
|
||||
progressbar.OptionShowBytes(true),
|
||||
progressbar.OptionSetWidth(25),
|
||||
progressbar.OptionSetDescription("Writing image file..."),
|
||||
progressbar.OptionSetTheme(progressbar.Theme{
|
||||
Saucer: "=",
|
||||
SaucerHead: ">",
|
||||
SaucerPadding: " ",
|
||||
BarStart: "[",
|
||||
BarEnd: "]",
|
||||
}))
|
||||
writer := io.MultiWriter(target, bar)
|
||||
written, err := io.Copy(writer, image)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return written, err
|
||||
}
|
||||
|
||||
func PrintAvail() {
|
||||
devices, err := etcher.GetBlockdevices()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println("Available devices:")
|
||||
|
||||
for _, dev := range devices {
|
||||
fmt.Print(" * ", dev.Path)
|
||||
if dev.Size > 0 {
|
||||
fmt.Print(" [", (dev.Size / 1024 / 1024 / 1024), "GB]\n")
|
||||
} else {
|
||||
fmt.Println("")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Main() {
|
||||
s := spinner.New(spinner.CharSets[14], 100*time.Millisecond)
|
||||
|
||||
if input == "" {
|
||||
if len(flag.Args()) == 0 {
|
||||
input = GetPath()
|
||||
} else if len(flag.Args()) > 0 {
|
||||
input = flag.Args()[0]
|
||||
}
|
||||
}
|
||||
|
||||
if device == "" {
|
||||
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]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
s.Prefix = "[ "
|
||||
s.Suffix = " ] Getting file details"
|
||||
s.Start()
|
||||
statinput, err := os.Stat(input)
|
||||
if err != nil {
|
||||
s.Stop()
|
||||
fmt.Println("\r[", color.RedString("✘"), "] Getting file details ")
|
||||
log.Fatal(err)
|
||||
}
|
||||
statdevice, err := os.Stat(device)
|
||||
if err != nil {
|
||||
s.Stop()
|
||||
fmt.Println("\r[", color.RedString("✘"), "] Getting file details ")
|
||||
log.Fatal(err)
|
||||
}
|
||||
image, err := os.Open(input)
|
||||
if err != nil {
|
||||
s.Stop()
|
||||
fmt.Println("\r[", color.RedString("✘"), "] Getting file details ")
|
||||
log.Fatal(err)
|
||||
}
|
||||
var inputsize int64
|
||||
var inputisblock bool
|
||||
if statinput.Size() != 0 {
|
||||
inputsize = statinput.Size()
|
||||
inputisblock = false
|
||||
} else {
|
||||
inputsize, _ = image.Seek(0, io.SeekEnd)
|
||||
inputisblock = true
|
||||
_, _ = image.Seek(0, 0)
|
||||
}
|
||||
target, err := os.OpenFile(device, os.O_RDWR, 0660)
|
||||
if err != nil {
|
||||
s.Stop()
|
||||
fmt.Println("\r[", color.RedString("✘"), "] Getting file details ")
|
||||
log.Fatal(err)
|
||||
}
|
||||
var targetsize int64
|
||||
var targetisblock bool
|
||||
if statdevice.Size() != 0 {
|
||||
targetsize = statdevice.Size()
|
||||
targetisblock = false
|
||||
} else {
|
||||
targetsize, err = target.Seek(0, io.SeekEnd)
|
||||
targetisblock = true
|
||||
_, _ = target.Seek(0, 0)
|
||||
}
|
||||
prehash := sha256.New()
|
||||
if !(force || disable_hash) {
|
||||
if err != nil {
|
||||
s.Stop()
|
||||
fmt.Println("\r[", color.RedString("✘"), "] Getting file details ")
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
_, err = io.Copy(prehash, image)
|
||||
_, _ = image.Seek(0, 0)
|
||||
}
|
||||
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 ")
|
||||
}
|
||||
inputmb := fmt.Sprint("[", inputsize/1024/1024, "MB]")
|
||||
devicemb := fmt.Sprint("[", targetsize/1024/1024, "MB]")
|
||||
var inputblock string
|
||||
var targetblock string
|
||||
if inputisblock {
|
||||
inputblock = "[Blockdevice]"
|
||||
} else {
|
||||
inputblock = "[File]"
|
||||
}
|
||||
if targetisblock {
|
||||
targetblock = "[Blockdevice]"
|
||||
} else {
|
||||
targetblock = "[File]"
|
||||
}
|
||||
fmt.Println("[", color.BlueString("i"), "] Input device/file: "+input, inputmb, inputblock)
|
||||
fmt.Println("[", color.BlueString("i"), "] Output device/file: "+device, devicemb, targetblock)
|
||||
if !force {
|
||||
if inputsize > targetsize {
|
||||
fmt.Println("[", color.RedString("w"), "]", color.RedString(" Warning:"), "Input file seems to be bigger than the destination!")
|
||||
}
|
||||
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")
|
||||
}
|
||||
}
|
||||
written, err := WriteImage(image, target, inputsize)
|
||||
_, _ = target.Seek(0, 0)
|
||||
if err != nil {
|
||||
fmt.Println("\r[", color.RedString("✘"), "] Writing image,", written, "bytes written ")
|
||||
log.Fatal(err)
|
||||
} else {
|
||||
fmt.Println("\r[", color.GreenString("✓"), "] Writing image,", written, "bytes written ")
|
||||
}
|
||||
|
||||
s.Prefix = "[ "
|
||||
s.Suffix = " ] Syncing"
|
||||
s.Start()
|
||||
err = image.Sync()
|
||||
if err != nil {
|
||||
s.Stop()
|
||||
fmt.Println("\r[", color.RedString("✘"), "] Syncing ")
|
||||
log.Fatal(err)
|
||||
}
|
||||
err = target.Sync()
|
||||
if err != nil {
|
||||
s.Stop()
|
||||
fmt.Println("\r[", color.RedString("✘"), "] Syncing ")
|
||||
log.Fatal(err)
|
||||
} else {
|
||||
s.Stop()
|
||||
fmt.Println("\r[", color.GreenString("✓"), "] Syncing ")
|
||||
}
|
||||
if !(force || disable_hash) {
|
||||
s.Prefix = "[ "
|
||||
s.Suffix = " ] Verifying"
|
||||
s.Start()
|
||||
posthash := sha256.New()
|
||||
_, err = io.CopyN(posthash, target, inputsize)
|
||||
presum := fmt.Sprintf("%x", prehash.Sum(nil))
|
||||
postsum := fmt.Sprintf("%x", posthash.Sum(nil))
|
||||
if err != nil || presum != postsum {
|
||||
s.Stop()
|
||||
fmt.Println("\r[", color.RedString("✘"), "] Verifying ")
|
||||
log.Fatal(err)
|
||||
} else {
|
||||
s.Stop()
|
||||
fmt.Println("\r[", color.GreenString("✓"), "] Verifying ")
|
||||
}
|
||||
}
|
||||
err = image.Close()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
err = target.Close()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
package etcher
|
||||
|
||||
type Blockdevice struct {
|
||||
Path string
|
||||
Size int
|
||||
Type string
|
||||
//ID string
|
||||
}
|
||||
|
||||
// CheckHash checks if the given file has been properly applied to the given blockdevice.
|
||||
//func CheckHash(f os.File, b Blockdevice) error {}
|
@ -1,68 +0,0 @@
|
||||
package etcher
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// getBlockdeviceSize gets the size of a blockdevice from the kernel filesystem.
|
||||
func getBlockdeviceSize(name string) (size int, err error) {
|
||||
sizefile, err := os.Open("/sys/block/" + name + "/size")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
sizeread, err := io.ReadAll(sizefile)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_ = sizefile.Close()
|
||||
|
||||
sizestring := strings.TrimSuffix(string(sizeread), "\n")
|
||||
size, err = strconv.Atoi(sizestring)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
size = size * 512 // multiply blockcount by blocksize
|
||||
|
||||
return size, err
|
||||
}
|
||||
|
||||
// GetBlockdevices gets the available blockdevices for reading from/writing to.
|
||||
func GetBlockdevices() (devices []Blockdevice, err error) {
|
||||
block, err := os.ReadDir("/sys/block")
|
||||
if err != nil {
|
||||
return
|
||||
} else if len(block) == 0 {
|
||||
return devices, fmt.Errorf("no blockdevices found")
|
||||
}
|
||||
|
||||
for _, device := range block {
|
||||
var dev Blockdevice
|
||||
|
||||
dev.Path = "/dev/" + device.Name()
|
||||
|
||||
if strings.Contains(device.Name(), "sd") {
|
||||
dev.Type = "sata"
|
||||
} else if strings.Contains(device.Name(), "hd") {
|
||||
dev.Type = "ide"
|
||||
} else if strings.Contains(device.Name(), "nvme") {
|
||||
dev.Type = "nvme"
|
||||
} else if strings.Contains(device.Name(), "dm") {
|
||||
dev.Type = "dmcrypt"
|
||||
} else if strings.Contains(device.Name(), "md") {
|
||||
dev.Type = "raid"
|
||||
}
|
||||
|
||||
dev.Size, err = getBlockdeviceSize(device.Name())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
devices = append(devices, dev)
|
||||
}
|
||||
|
||||
return devices, nil
|
||||
}
|
28
go.mod
28
go.mod
@ -1,23 +1,23 @@
|
||||
module github.com/byReqz/go-etcher
|
||||
|
||||
go 1.17
|
||||
go 1.21
|
||||
|
||||
toolchain go1.22.2
|
||||
|
||||
require (
|
||||
github.com/briandowns/spinner v1.18.0
|
||||
github.com/fatih/color v1.13.0
|
||||
github.com/schollz/progressbar/v3 v3.8.5
|
||||
github.com/JoaoDanielRufino/go-input-autocomplete v1.1.0
|
||||
github.com/briandowns/spinner v1.23.0
|
||||
github.com/fatih/color v1.16.0
|
||||
github.com/schollz/progressbar/v3 v3.14.2
|
||||
github.com/spf13/pflag v1.0.5
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/JoaoDanielRufino/go-input-autocomplete v1.0.4 // indirect
|
||||
github.com/eiannone/keyboard v0.0.0-20200508000154-caf4b762e807 // indirect
|
||||
github.com/mattn/go-colorable v0.1.9 // indirect
|
||||
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.13 // indirect
|
||||
github.com/eiannone/keyboard v0.0.0-20220611211555-0d226195f203 // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
|
||||
github.com/rivo/uniseg v0.2.0 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
|
||||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
|
||||
github.com/rivo/uniseg v0.4.7 // indirect
|
||||
golang.org/x/sys v0.19.0 // indirect
|
||||
golang.org/x/term v0.19.0 // indirect
|
||||
)
|
||||
|
65
go.sum
65
go.sum
@ -1,53 +1,38 @@
|
||||
github.com/JoaoDanielRufino/go-input-autocomplete v1.0.4 h1:ZsgP08f77C1Hizi5pfB9xNBG+d605s0z+PWuI5KLEEk=
|
||||
github.com/JoaoDanielRufino/go-input-autocomplete v1.0.4/go.mod h1:tB42IGOd4W3hzlAcw8JWf0mMnu94q4tPsjhT24MbcB4=
|
||||
github.com/briandowns/spinner v1.18.0 h1:SJs0maNOs4FqhBwiJ3Gr7Z1D39/rukIVGQvpNZVHVcM=
|
||||
github.com/briandowns/spinner v1.18.0/go.mod h1:QOuQk7x+EaDASo80FEXwlwiA+j/PPIcX3FScO+3/ZPQ=
|
||||
github.com/JoaoDanielRufino/go-input-autocomplete v1.1.0 h1:FNqKY5QPEIsqD1L8MFWh+mcU/vIZT45n5/XIYuDUOQc=
|
||||
github.com/JoaoDanielRufino/go-input-autocomplete v1.1.0/go.mod h1:jbEOh6l6PzFysFEbqoooNDwTQwb9Oz8hBwMJh2CTVlI=
|
||||
github.com/briandowns/spinner v1.23.0 h1:alDF2guRWqa/FOZZYWjlMIx2L6H0wyewPxo/CH4Pt2A=
|
||||
github.com/briandowns/spinner v1.23.0/go.mod h1:rPG4gmXeN3wQV/TsAY4w8lPdIM6RX3yqeBQJSrbXjuE=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/eiannone/keyboard v0.0.0-20200508000154-caf4b762e807 h1:jdjd5e68T4R/j4PWxfZqcKY8KtT9oo8IPNVuV4bSXDQ=
|
||||
github.com/eiannone/keyboard v0.0.0-20200508000154-caf4b762e807/go.mod h1:Xoiu5VdKMvbRgHuY7+z64lhu/7lvax/22nzASF6GrO8=
|
||||
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
|
||||
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
|
||||
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
|
||||
github.com/eiannone/keyboard v0.0.0-20220611211555-0d226195f203 h1:XBBHcIb256gUJtLmY22n99HaZTz+r2Z51xUPi01m3wg=
|
||||
github.com/eiannone/keyboard v0.0.0-20220611211555-0d226195f203/go.mod h1:E1jcSv8FaEny+OP/5k9UxZVw9YFWGj7eI4KR/iOBqCg=
|
||||
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
|
||||
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
|
||||
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw=
|
||||
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
|
||||
github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U=
|
||||
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
|
||||
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
|
||||
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
|
||||
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
|
||||
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
|
||||
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
|
||||
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
|
||||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||
github.com/schollz/progressbar/v3 v3.8.5 h1:VcmmNRO+eFN3B0m5dta6FXYXY+MEJmXdWoIS+jjssQM=
|
||||
github.com/schollz/progressbar/v3 v3.8.5/go.mod h1:ewO25kD7ZlaJFTvMeOItkOZa8kXu1UvFs379htE8HMQ=
|
||||
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
||||
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||
github.com/schollz/progressbar/v3 v3.14.2 h1:EducH6uNLIWsr560zSV1KrTeUb/wZGAHqyMFIEa99ks=
|
||||
github.com/schollz/progressbar/v3 v3.14.2/go.mod h1:aQAZQnhF4JGFtRJiw/eobaXpsqpVQAftEQ+hLGXaRc4=
|
||||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 h1:0es+/5331RGQPcXlMfP+WrnIIS6dNnNRe0WB02W0F4M=
|
||||
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200806125547-5acd03effb82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
|
||||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
|
||||
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
|
||||
golang.org/x/term v0.19.0 h1:+ThwsDv+tYfnJFhF4L8jITxu1tdTWRTZpdsWgEgjL6Q=
|
||||
golang.org/x/term v0.19.0/go.mod h1:2CuTdWZ7KHSQwUzKva0cbMg6q2DMI3Mmxp+gKJbskEk=
|
||||
|
301
main.go
301
main.go
@ -1,7 +1,302 @@
|
||||
package main
|
||||
|
||||
import "github.com/byReqz/go-etcher/cmd/cetcher"
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
ac "github.com/JoaoDanielRufino/go-input-autocomplete"
|
||||
"github.com/briandowns/spinner"
|
||||
"github.com/fatih/color"
|
||||
"github.com/schollz/progressbar/v3"
|
||||
flag "github.com/spf13/pflag"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var device string
|
||||
var input string
|
||||
var force bool
|
||||
var disable_hash 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.BoolVarP(&disable_hash, "no-hash", "n", false, "disable hash verification")
|
||||
flag.Parse()
|
||||
}
|
||||
|
||||
func GetPath() string {
|
||||
path, err := ac.Read("[ " + color.YellowString("i") + " ] Please input your image file: ")
|
||||
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:]
|
||||
}
|
||||
if path == "" {
|
||||
fmt.Println("[", color.RedString("!"), "] No image given, retrying.")
|
||||
path = GetPath()
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
func GetDest() string {
|
||||
PrintAvail()
|
||||
dest, err := ac.Read("[ " + color.YellowString("i") + " ] Please input destination: ")
|
||||
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:]
|
||||
}
|
||||
if dest == "" {
|
||||
fmt.Println("[", color.RedString("!"), "] No destination given, retrying.")
|
||||
dest = GetDest()
|
||||
}
|
||||
return dest
|
||||
}
|
||||
|
||||
func WriteImage(image *os.File, target *os.File, size int64) (int64, error) {
|
||||
bar := progressbar.NewOptions(int(size),
|
||||
progressbar.OptionSetWriter(os.Stderr),
|
||||
progressbar.OptionShowBytes(true),
|
||||
progressbar.OptionSetWidth(25),
|
||||
progressbar.OptionSetDescription("Writing image file..."),
|
||||
progressbar.OptionSetTheme(progressbar.Theme{
|
||||
Saucer: "=",
|
||||
SaucerHead: ">",
|
||||
SaucerPadding: " ",
|
||||
BarStart: "[",
|
||||
BarEnd: "]",
|
||||
}))
|
||||
writer := io.MultiWriter(target, bar)
|
||||
written, err := io.Copy(writer, image)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return written, err
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
}
|
||||
fmt.Println("Available devices:")
|
||||
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() {
|
||||
cetcher.Main()
|
||||
}
|
||||
s := spinner.New(spinner.CharSets[14], 100*time.Millisecond)
|
||||
|
||||
if input == "" {
|
||||
if len(flag.Args()) == 0 {
|
||||
input = GetPath()
|
||||
} else if len(flag.Args()) > 0 {
|
||||
input = flag.Args()[0]
|
||||
}
|
||||
}
|
||||
|
||||
if device == "" {
|
||||
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]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
s.Prefix = "[ "
|
||||
s.Suffix = " ] Getting file details"
|
||||
s.Start()
|
||||
statinput, err := os.Stat(input)
|
||||
if err != nil {
|
||||
s.Stop()
|
||||
fmt.Println("\r[", color.RedString("✘"), "] Getting file details ")
|
||||
log.Fatal(err)
|
||||
}
|
||||
statdevice, err := os.Stat(device)
|
||||
if err != nil {
|
||||
s.Stop()
|
||||
fmt.Println("\r[", color.RedString("✘"), "] Getting file details ")
|
||||
log.Fatal(err)
|
||||
}
|
||||
image, err := os.Open(input)
|
||||
if err != nil {
|
||||
s.Stop()
|
||||
fmt.Println("\r[", color.RedString("✘"), "] Getting file details ")
|
||||
log.Fatal(err)
|
||||
}
|
||||
var inputsize int64
|
||||
var inputisblock bool
|
||||
if statinput.Size() != 0 {
|
||||
inputsize = statinput.Size()
|
||||
inputisblock = false
|
||||
} else {
|
||||
inputsize, _ = image.Seek(0, io.SeekEnd)
|
||||
inputisblock = true
|
||||
_, _ = image.Seek(0, 0)
|
||||
}
|
||||
target, err := os.OpenFile(device, os.O_RDWR, 0660)
|
||||
if err != nil {
|
||||
s.Stop()
|
||||
fmt.Println("\r[", color.RedString("✘"), "] Getting file details ")
|
||||
log.Fatal(err)
|
||||
}
|
||||
var targetsize int64
|
||||
var targetisblock bool
|
||||
if statdevice.Size() != 0 {
|
||||
targetsize = statdevice.Size()
|
||||
targetisblock = false
|
||||
} else {
|
||||
targetsize, err = target.Seek(0, io.SeekEnd)
|
||||
targetisblock = true
|
||||
_, _ = target.Seek(0, 0)
|
||||
}
|
||||
prehash := sha256.New()
|
||||
if !(force || disable_hash) {
|
||||
if err != nil {
|
||||
s.Stop()
|
||||
fmt.Println("\r[", color.RedString("✘"), "] Getting file details ")
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
_, err = io.Copy(prehash, image)
|
||||
_, _ = image.Seek(0, 0)
|
||||
}
|
||||
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 ")
|
||||
}
|
||||
inputmb := fmt.Sprint("[", inputsize/1024/1024, "MB]")
|
||||
devicemb := fmt.Sprint("[", targetsize/1024/1024, "MB]")
|
||||
var inputblock string
|
||||
var targetblock string
|
||||
if inputisblock {
|
||||
inputblock = "[Blockdevice]"
|
||||
} else {
|
||||
inputblock = "[File]"
|
||||
}
|
||||
if targetisblock {
|
||||
targetblock = "[Blockdevice]"
|
||||
} else {
|
||||
targetblock = "[File]"
|
||||
}
|
||||
fmt.Println("[", color.BlueString("i"), "] Input device/file: "+input, inputmb, inputblock)
|
||||
fmt.Println("[", color.BlueString("i"), "] Output device/file: "+device, devicemb, targetblock)
|
||||
if !force {
|
||||
if inputsize > targetsize {
|
||||
fmt.Println("[", color.RedString("w"), "]", color.RedString(" Warning:"), "Input file seems to be bigger than the destination!")
|
||||
}
|
||||
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")
|
||||
}
|
||||
}
|
||||
written, err := WriteImage(image, target, inputsize)
|
||||
_, _ = target.Seek(0, 0)
|
||||
if err != nil {
|
||||
fmt.Println("\r[", color.RedString("✘"), "] Writing image,", written, "bytes written ")
|
||||
log.Fatal(err)
|
||||
} else {
|
||||
fmt.Println("\r[", color.GreenString("✓"), "] Writing image,", written, "bytes written ")
|
||||
}
|
||||
|
||||
s.Prefix = "[ "
|
||||
s.Suffix = " ] Syncing"
|
||||
s.Start()
|
||||
err = image.Sync()
|
||||
if err != nil {
|
||||
s.Stop()
|
||||
fmt.Println("\r[", color.RedString("✘"), "] Syncing ")
|
||||
log.Fatal(err)
|
||||
}
|
||||
err = target.Sync()
|
||||
if err != nil {
|
||||
s.Stop()
|
||||
fmt.Println("\r[", color.RedString("✘"), "] Syncing ")
|
||||
log.Fatal(err)
|
||||
} else {
|
||||
s.Stop()
|
||||
fmt.Println("\r[", color.GreenString("✓"), "] Syncing ")
|
||||
}
|
||||
if !(force || disable_hash) {
|
||||
s.Prefix = "[ "
|
||||
s.Suffix = " ] Verifying"
|
||||
s.Start()
|
||||
posthash := sha256.New()
|
||||
_, err = io.CopyN(posthash, target, inputsize)
|
||||
presum := fmt.Sprintf("%x", prehash.Sum(nil))
|
||||
postsum := fmt.Sprintf("%x", posthash.Sum(nil))
|
||||
if err != nil || presum != postsum {
|
||||
s.Stop()
|
||||
fmt.Println("\r[", color.RedString("✘"), "] Verifying ")
|
||||
log.Fatal(err)
|
||||
} else {
|
||||
s.Stop()
|
||||
fmt.Println("\r[", color.GreenString("✓"), "] Verifying ")
|
||||
}
|
||||
}
|
||||
err = image.Close()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
err = target.Close()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user