RESOLV: finally sudo() and test ping()

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2021-11-03 22:20:49 -05:00
parent 679f11f7fa
commit 25fb41e9be
5 changed files with 82 additions and 38 deletions

View File

@ -40,7 +40,7 @@ gomod-init:
gomod-clean: gomod-clean:
rm -f go.* rm -f go.*
sudo: build sudo: new-build
sudo ~/go/bin/wit-debian-gui sudo ~/go/bin/wit-debian-gui
# https://golang.cafe/blog/golang-debugging-with-delve.html # https://golang.cafe/blog/golang-debugging-with-delve.html

15
apt.go
View File

@ -1,10 +1,8 @@
package main package main
import ( import (
// "log"
"git.wit.org/wit/gui" "git.wit.org/wit/gui"
"git.wit.org/wit/shell"
) )
func aptGroup(tab *gui.Node) { func aptGroup(tab *gui.Node) {
@ -13,8 +11,17 @@ func aptGroup(tab *gui.Node) {
n.AddButton("update apt-file", func (*gui.Node) { n.AddButton("update apt-file", func (*gui.Node) {
// shell.Script("apt -y install apt-file\napt-file update") // shell.Script("apt -y install apt-file\napt-file update")
// xterm("apt -y install apt-file; apt-file update") // xterm("apt -y install apt-file; apt-file update")
shell.Exec("apt -y install apt-file") n.ErrorWindow("test error window", "this is the error")
shell.Exec("apt-file update") err := sudo( func() error {
err := bash("apt install moon-buggy")
return err
})
if (err != nil) {
panic("bash failed")
}
err = bash("bash -x /tmp/jcarr")
bash("apt -y install apt-file")
bash("apt-file update")
}) })
n.AddButton("purge rc-only", func (*gui.Node) { n.AddButton("purge rc-only", func (*gui.Node) {
xterm("dpkg -l |grep ^rc | awk '{print $2}' |xargs apt remove --purge -y") xterm("dpkg -l |grep ^rc | awk '{print $2}' |xargs apt remove --purge -y")

14
main.go
View File

@ -33,11 +33,6 @@ var packrBox packr.Box
func customExit(n *gui.Node) { func customExit(n *gui.Node) {
origlog.Println("Should Exit Here") origlog.Println("Should Exit Here")
closed = true closed = true
/*
window := gui.Data.WindowMap["Debugging2"]
spew.Dump(window)
*/
time.Sleep(3 * time.Second)
log.Println("") log.Println("")
log.Println("CLEAN EXIT") log.Println("CLEAN EXIT")
log.Println("") log.Println("")
@ -55,18 +50,15 @@ func main() {
// This directory includes the default config file if there is not already one // This directory includes the default config file if there is not already one
packrBox = packr.NewBox("./resources") packrBox = packr.NewBox("./resources")
filename := "ddns-tmp.conf" // runs the GO profiler in a goroutine
b, _ := packrBox.FindString(filename)
log.Println(filename, "=\n\n", b)
// go sshClient()
go pprofMain() go pprofMain()
// go gui.Main(dumbLoop)
go gui.Main(initGUI) go gui.Main(initGUI)
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
gui.Queue(delayedTabs) gui.Queue(delayedTabs)
go systrayMain() go systrayMain()
watchWindows() watchWindows()
} }

29
os.go
View File

@ -113,3 +113,32 @@ func packrSaveFile(packrname string, filename string) error {
w.Flush() w.Flush()
return nil return nil
} }
// run something and never return from it
// TODO: pass STDOUT, STDERR, STDIN correctly
// TODO: figure out how to nohup the process and exit
func bash(cmdline string) error {
log.Println("shell.Run() START " + cmdline)
cmdArgs := strings.Fields(cmdline)
process := exec.Command(cmdArgs[0], cmdArgs[1:len(cmdArgs)]...)
process.Stderr = os.Stderr
process.Stdin = os.Stdin
process.Stdout = os.Stdout
process.Start()
err := process.Wait()
log.Println("shell.Exec() err =", err)
return err
// os.Exit(0)
}
func sudo(f func() error) error {
uid := os.Getuid()
log.Println("You must be root to do this. uid =", uid)
if (uid != 0) {
return errors.New("not root")
}
err := f()
return err
}

View File

@ -3,8 +3,12 @@ package main
import "log" import "log"
import "os" import "os"
import "bufio" import "bufio"
import "strings"
import "errors"
import "git.wit.org/wit/gui" import "git.wit.org/wit/gui"
var filename string = ""
func resolvWindow(w *gui.Node) { func resolvWindow(w *gui.Node) {
if (w == nil) { if (w == nil) {
gui.Config.Title = "resolv.conf Window" gui.Config.Title = "resolv.conf Window"
@ -20,38 +24,50 @@ func resolvWindow(w *gui.Node) {
////////////// filename ///////////////////////// ////////////// filename /////////////////////////
gNode := tab.AddGroup("filename") gNode := tab.AddGroup("filename")
resolvNode := gNode.AddComboBox("resolv-1-1-1-1.conf", var tmp []string
"resolv-4.2.2.2.conf", for i, s := range packrBox.List() {
"resolv-8-8-8-8.conf", log.Println("i, s =", i, s)
"resolv-bind.wit.org.conf", if strings.HasPrefix(s, "resolv/") {
"resolv-ipv6-only.conf", tmp = append(tmp, s)
"resolv-localhost.conf") }
}
// panic("junk")
resolvNode := gNode.AddComboBox("test", tmp...)
resolvNode.SetText("resolv-1-1-1-1.conf") resolvNode.SetText("resolv-1-1-1-1.conf")
resolvNode.OnChanged = func () { resolvNode.OnChanged = func () {
log.Println("STARTED HOSTNAME") log.Println("STARTED HOSTNAME")
filename := resolvNode.GetText() filename = resolvNode.GetText()
log.Println("ENDED GetText() HOSTNAME =", filename) log.Println("ENDED GetText() HOSTNAME =", filename)
} }
////////////// connect ///////////////////////// ////////////// connect /////////////////////////
gNode = tab.AddGroup("Update") gNode = tab.AddGroup("Update")
gNode.AddButton("/etc/resolv.conf", func (*gui.Node) { gNode.AddButton("Update /etc/resolv.conf", func (*gui.Node) {
filename := "resolv/" + resolvNode.GetText() sudo( func() error {
log.Println("set resolv.conf to",filename) log.Println("set resolv.conf to",filename)
b, _ := packrBox.FindString(filename) b, _ := packrBox.FindString(filename)
log.Println(filename, "=\n\n" + b) log.Println(filename, "=\n\n" + b)
// spew.Dump(b) // spew.Dump(b)
f, err := os.Create("/etc/resolv.conf") f, err := os.Create("/etc/resolv.conf")
if err != nil { if err != nil {
return return errors.New("os.Create() /etc/resolv.conf failed")
} }
defer f.Close() defer f.Close()
w := bufio.NewWriter(f) w := bufio.NewWriter(f)
n4, err := w.WriteString(b) n4, err := w.WriteString(b)
log.Println("n4 =", n4) log.Println("n4 =", n4)
w.Flush() w.Flush()
return nil
})
})
gNode.AddButton("test ping ipv4", func (*gui.Node) {
bash("ping -c 3 1.1.1.1")
})
gNode.AddButton("test ping ipv6", func (*gui.Node) {
bash("ping -c 3 2001:4860:4860::6464")
}) })
} }