add a 'script' function

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2019-04-06 07:42:01 -07:00
parent d2223187ea
commit 9857b1c615
2 changed files with 59 additions and 16 deletions

View File

@ -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("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") os.Chdir("/root")
simpleProcess("ls -l")
os.Chdir("/mnt") script(`
simpleProcess("pwd") pwd
simpleProcess("ls -l /aksd") 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 return
simpleProcess("parted -s /dev/sdb mklabel msdos") script(`
simpleProcess("parted -s /dev/sdb mkpart primary ext4 1MiB 8GB") parted -s /dev/sdb mklabel msdos
simpleProcess("parted -s /dev/sdb mkpart primary ext4 8GB 32GB") parted -s /dev/sdb mkpart primary ext4 1MiB 8GB
simpleProcess("sleep 1") parted -s /dev/sdb mkpart primary ext4 8GB 32GB
simpleProcess("mkfs.ext4 /dev/sdb1") sleep 1
simpleProcess("mkfs.ext4 /dev/sdb2") mkfs.ext4 /dev/sdb1
simpleProcess("e2label /dev/sdb1 root") mkfs.ext4 /dev/sdb2
simpleProcess("e2label /dev/sdb2 factory-image") e2label /dev/sdb1 root
simpleProcess("mount /dev/sdb1 /mnt/sdcard/") e2label /dev/sdb2 factory-image
simpleProcess("dd if=../u-boot/u-boot-sunxi-with-spl.bin of=/dev/sdb bs=1K seek=8") 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)) { func addButton(vbox *ui.Box, label string, click func(*ui.Button)) {
@ -137,6 +149,9 @@ func makeBurnSDcardPage() *ui.Box {
cbox.OnSelected(func(*ui.Combobox) { cbox.OnSelected(func(*ui.Combobox) {
spew.Dump(cbox.Visible()) spew.Dump(cbox.Visible())
spew.Dump(cbox.Selected()) spew.Dump(cbox.Selected())
spew.Dump(cbox.ControlBase)
// spew.Dump(cbox.ControlBase.Parent)
spew.Dump(cbox)
log.Println("selected = ", cbox.Selected()) log.Println("selected = ", cbox.Selected())
}) })
vbox.Append(cbox, false) vbox.Append(cbox, false)
@ -158,7 +173,6 @@ func makeBurnSDcardPage() *ui.Box {
rb2.Append("stuff") rb2.Append("stuff")
vbox3.Append(rb2, false) vbox3.Append(rb2, false)
return hbox return hbox
} }

View File

@ -4,6 +4,7 @@ import "fmt"
import "log" import "log"
import "strings" import "strings"
import "io" import "io"
import "time"
import "os" import "os"
import "os/exec" import "os/exec"
import "bufio" import "bufio"
@ -64,12 +65,40 @@ func ping(hostname string, count int) {
cmd.Wait() 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 // Spawns a process and captures stdout and stderr
// this has to internally handle 'cd' and call os.Chdir()
func simpleProcess(cmd string) int { func simpleProcess(cmd string) int {
log.Println("START " + cmd) log.Println("START " + cmd)
// cmd := exec.Command("ls", "/tmp", "/ballon", "/") // cmd := exec.Command("ls", "/tmp", "/ballon", "/")
cmd = strings.TrimSpace(cmd) // this is like 'chomp' in perl
cmdArgs := strings.Fields(cmd) 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)]...) process := exec.Command(cmdArgs[0], cmdArgs[1:len(cmdArgs)]...)
stdout, _ := process.StdoutPipe() stdout, _ := process.StdoutPipe()
stderr, _ := process.StderrPipe() stderr, _ := process.StderrPipe()