From 9857b1c61560e29d5dbd81c766cfffde57810d09 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sat, 6 Apr 2019 07:42:01 -0700 Subject: [PATCH] add a 'script' function Signed-off-by: Jeff Carr --- make-sdcard/main.go | 46 ++++++++++++++++++++----------- make-sdcard/{ping.go => shell.go} | 29 +++++++++++++++++++ 2 files changed, 59 insertions(+), 16 deletions(-) rename make-sdcard/{ping.go => shell.go} (82%) diff --git a/make-sdcard/main.go b/make-sdcard/main.go index d1b8d37..06cec50 100644 --- a/make-sdcard/main.go +++ b/make-sdcard/main.go @@ -40,23 +40,35 @@ func format(mybut *ui.Button) { // simpleProcess("dd if=/home/pinebook/factory/factory-pine14inch of=/dev/sdb status=progress bs=1M oflag=sync count=300"); // simpleProcess("dd if=/home/pinebook/factory/factory-pine14inch of=/dev/sdb status=progress bs=1M oflag=sync count=300"); - simpleProcess("pwd") - simpleProcess("ls -l") - os.Chdir("/mnt") - simpleProcess("pwd") - simpleProcess("ls -l /aksd") + os.Chdir("/root") + + script(` + pwd + ls -l /etc/issue + pwd + ls -l /ajlskdf + ls -l /etc/issue + ls -l /etc/issue + cd /tmp + echo blah + echo foo ; lkajsdf + cd + echo bar + `) return - simpleProcess("parted -s /dev/sdb mklabel msdos") - simpleProcess("parted -s /dev/sdb mkpart primary ext4 1MiB 8GB") - simpleProcess("parted -s /dev/sdb mkpart primary ext4 8GB 32GB") - simpleProcess("sleep 1") - simpleProcess("mkfs.ext4 /dev/sdb1") - simpleProcess("mkfs.ext4 /dev/sdb2") - simpleProcess("e2label /dev/sdb1 root") - simpleProcess("e2label /dev/sdb2 factory-image") - simpleProcess("mount /dev/sdb1 /mnt/sdcard/") - simpleProcess("dd if=../u-boot/u-boot-sunxi-with-spl.bin of=/dev/sdb bs=1K seek=8") + script(` + parted -s /dev/sdb mklabel msdos + parted -s /dev/sdb mkpart primary ext4 1MiB 8GB + parted -s /dev/sdb mkpart primary ext4 8GB 32GB + sleep 1 + mkfs.ext4 /dev/sdb1 + mkfs.ext4 /dev/sdb2 + 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 bs=1K seek=8 + `) } func addButton(vbox *ui.Box, label string, click func(*ui.Button)) { @@ -137,6 +149,9 @@ func makeBurnSDcardPage() *ui.Box { 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()) }) vbox.Append(cbox, false) @@ -158,7 +173,6 @@ func makeBurnSDcardPage() *ui.Box { rb2.Append("stuff") vbox3.Append(rb2, false) - return hbox } diff --git a/make-sdcard/ping.go b/make-sdcard/shell.go similarity index 82% rename from make-sdcard/ping.go rename to make-sdcard/shell.go index d09620c..72d58a6 100644 --- a/make-sdcard/ping.go +++ b/make-sdcard/shell.go @@ -4,6 +4,7 @@ import "fmt" import "log" import "strings" import "io" +import "time" import "os" import "os/exec" import "bufio" @@ -64,12 +65,40 @@ func ping(hostname string, count int) { cmd.Wait() } +func script(cmds string) int { + // split on new lines (while we are at it, handle stupid windows text files + lines := strings.Split(strings.Replace(cmds, "\r\n", "\n", -1), "\n") + + for _, line := range lines { + line = strings.TrimSpace(line) // this is like 'chomp' in perl + fmt.Println("LINE:", line) + time.Sleep(1) + // simpleProcess("ls /") + simpleProcess(line) + } + return 0 +} + // Spawns a process and captures stdout and stderr +// 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) + 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()