simple format sdcard golang example

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2019-04-07 20:40:51 -07:00
parent 393a019b78
commit 63c71e53b5
2 changed files with 134 additions and 17 deletions

View File

@ -17,6 +17,7 @@ import "github.com/davecgh/go-spew/spew"
// https://github.com/yaa110/sslice
var mainwin *ui.Window
var mydrive string
func doButton(mybut *ui.Button) {
log.Println("doButton() hostname = test", config.String("hostname"))
@ -27,15 +28,40 @@ func doButton(mybut *ui.Button) {
func rsync(mybut *ui.Button) {
log.Println("doButton() mybut =", reflect.ValueOf(mybut).Elem())
log.Println("run rsync here")
if _, err := os.Stat("/mnt/sdcard/lost+found/"); !os.IsNotExist(err) {
log.Println("run rsync here")
script(`
rsync -av --progress --inplace /home/pinebook/factory/FACTORY-IMAGER-ROOTFS/ /mnt/sdcard/
umount /mnt/sdcard
`)
ui.MsgBox(mainwin, "The sdcard is finished", "")
} else {
log.Println("partition is not mounted")
ui.MsgBoxError(mainwin, "The partition is not mounted at /mnt/sdcard", "")
}
}
// select the dev entry to partition and format
// for example: /dev/mtdblock0 or /dev/sdc
func selectDrive(mybox *ui.Combobox) {
spew.Dump(mybox.Visible())
spew.Dump(mybox.Selected())
spew.Dump(mybox.ControlBase)
// spew.Dump(box.ControlBase.Parent)
spew.Dump(mybox)
log.Println("selected = ", mybox.Selected())
if (mybox.Selected() == 1) {
mydrive = "/dev/sdb"
}
}
func format(mybut *ui.Button) {
log.Println("doButton() mybut =", reflect.ValueOf(mybut).Elem())
log.Println("format and mount here")
/*
os.Chdir("/root")
script(`
pwd
ls -l /etc/issue
@ -50,8 +76,9 @@ func format(mybut *ui.Button) {
echo bar
`)
simpleProcess("dd if=/home/pinebook/factory/factory-pine14inch of=/dev/sdb status=progress bs=1M oflag=sync count=300");
script("dd if=/home/pinebook/factory/factory-pine14inch of=/dev/sdb status=progress bs=1M oflag=sync count=300");
return
*/
script(`
parted -s /dev/sdb mklabel msdos
@ -63,7 +90,8 @@ func format(mybut *ui.Button) {
e2label /dev/sdb1 root
e2label /dev/sdb2 factory-image
mount /dev/sdb1 /mnt/sdcard/
dd if=../u-boot/u-boot-sunxi-with-spl.bin of=/dev/sdb status=progress bs=1K seek=8
pwd
dd if=../u-boot/u-boot-sunxi-with-spl.bin of=/dev/sdb status=progress oflag=sync bs=1K seek=8
`)
}
@ -142,14 +170,7 @@ func makeBurnSDcardPage() *ui.Box {
cbox.Append("/dev/sdb")
cbox.Append("/dev/sr0")
cbox.Append("Combobox Item 3")
cbox.OnSelected(func(*ui.Combobox) {
spew.Dump(cbox.Visible())
spew.Dump(cbox.Selected())
spew.Dump(cbox.ControlBase)
// spew.Dump(cbox.ControlBase.Parent)
spew.Dump(cbox)
log.Println("selected = ", cbox.Selected())
})
cbox.OnSelected(selectDrive)
vbox.Append(cbox, false)
vbox2 := addGroupBox(hbox, "Select the Image")
@ -350,5 +371,7 @@ func setupUI() {
func main() {
parseConfig()
// script("dd if=/home/pinebook/factory/factory-pine14inch of=/dev/sdb status=progress bs=1M oflag=sync count=300");
ui.Main(setupUI)
}

View File

@ -10,6 +10,7 @@ import "os/exec"
import "bufio"
import "reflect"
import "github.com/davecgh/go-spew/spew"
import "github.com/svent/go-nbreader"
/*
func exampleCommand(a string, b ...string) {
@ -74,7 +75,8 @@ func script(cmds string) int {
fmt.Println("LINE:", line)
time.Sleep(1)
// simpleProcess("ls /")
simpleProcess(line)
// simpleProcess(line)
shell(line)
}
return 0
}
@ -83,12 +85,11 @@ func script(cmds string) int {
// this has to internally handle 'cd' and call os.Chdir()
func simpleProcess(cmd string) int {
log.Println("START " + cmd)
// cmd := exec.Command("ls", "/tmp", "/ballon", "/")
cmd = strings.TrimSpace(cmd) // this is like 'chomp' in perl
cmdArgs := strings.Fields(cmd)
if (len(cmdArgs) == 0) {
log.Println("END " + cmd)
log.Println("END ", cmd)
return 0 // nothing to do
}
if (cmdArgs[0] == "cd") {
@ -96,7 +97,7 @@ func simpleProcess(cmd string) int {
log.Println("os.Chdir()", cmd)
os.Chdir(cmdArgs[1])
}
log.Println("END " + cmd)
log.Println("END ", cmd)
return 0 // nothing to do
}
process := exec.Command(cmdArgs[0], cmdArgs[1:len(cmdArgs)]...)
@ -133,7 +134,7 @@ func simpleProcess(cmd string) int {
log.Println("END ", cmd)
return -1
}
log.Println("END " + cmd)
log.Println("END ", cmd)
return 0
}
@ -185,3 +186,96 @@ func stdoutExec(prompt string, timeout int) {
cmd.Wait()
}
func shell(cmdline string) int {
log.Println("START " + cmdline)
cmd := strings.TrimSpace(cmdline) // this is like 'chomp' in perl
cmdArgs := strings.Fields(cmd)
if (len(cmdArgs) == 0) {
log.Println("END ", cmd)
return 0 // nothing to do
}
if (cmdArgs[0] == "cd") {
if (len(cmdArgs) > 1) {
log.Println("os.Chdir()", cmd)
os.Chdir(cmdArgs[1])
}
log.Println("END ", cmd)
return 0 // nothing to do
}
process := exec.Command(cmdArgs[0], cmdArgs[1:len(cmdArgs)]...)
stdout, _ := process.StdoutPipe()
stderr, _ := process.StderrPipe()
process.Start()
f := bufio.NewWriter(os.Stdout)
newreader := bufio.NewReader(stdout)
nbr := nbreader.NewNBReader(newreader, 1024)
newerrreader := bufio.NewReader(stderr)
nbrerr := nbreader.NewNBReader(newerrreader, 1024)
for {
time.Sleep(2 * time.Millisecond) // only check the buffer 500 times a second
// log.Println("sleep done")
oneByte := make([]byte, 1024)
count, err := nbr.Read(oneByte)
if (err != nil) {
// log.Println("Read() count = ", count, "err = ", err)
oneByte = make([]byte, 1024)
count, err = nbr.Read(oneByte)
f.Write([]byte(string(oneByte)))
f.Flush()
}
f.Write([]byte(string(oneByte)))
f.Flush()
oneByte = make([]byte, 1024)
count, err = nbrerr.Read(oneByte)
if (err != nil) {
oneByte = make([]byte, 1024)
count, err = nbrerr.Read(oneByte)
f.Write([]byte(string(oneByte)))
f.Flush()
log.Println("Read() count = ", count, "err = ", err)
spew.Dump(process.Process)
spew.Dump(process.ProcessState)
err := process.Wait()
if err != nil {
spew.Dump(err.(*exec.ExitError))
spew.Dump(process.ProcessState)
stuff := err.(*exec.ExitError)
log.Println("ERROR ", stuff)
log.Println("END ", cmdline)
return -1
}
log.Println("END ", cmdline)
return 0
} else {
f.Write([]byte(string(oneByte)))
f.Flush()
}
// spew.Dump(reflect.ValueOf(cmd.Process).Elem())
}
err := process.Wait()
if err != nil {
spew.Dump(err.(*exec.ExitError))
spew.Dump(process.ProcessState)
stuff := err.(*exec.ExitError)
log.Println("ERROR ", stuff)
log.Println("END ", cmdline)
return -1
}
log.Println("END ", cmdline)
return 0
}