diff options
Diffstat (limited to 'cmds')
| -rw-r--r-- | cmds/buttonplugin/Makefile | 2 | ||||
| -rw-r--r-- | cmds/buttonplugin/log.go | 7 | ||||
| -rw-r--r-- | cmds/buttonplugin/main.go | 9 | ||||
| -rw-r--r-- | cmds/cloudflare/Makefile | 18 | ||||
| -rw-r--r-- | cmds/cloudflare/argv.go | 30 | ||||
| -rw-r--r-- | cmds/cloudflare/dns.go | 115 | ||||
| -rw-r--r-- | cmds/cloudflare/main.go | 122 |
7 files changed, 294 insertions, 9 deletions
diff --git a/cmds/buttonplugin/Makefile b/cmds/buttonplugin/Makefile index 1903a67..74486e5 100644 --- a/cmds/buttonplugin/Makefile +++ b/cmds/buttonplugin/Makefile @@ -7,7 +7,7 @@ # run: build - ./buttonplugin --gui-toolkit gocui >/tmp/witgui.log.stderr 2>&1 + ./buttonplugin --gui gocui >/tmp/witgui.log.stderr 2>&1 build-release: go get -v -u -x . diff --git a/cmds/buttonplugin/log.go b/cmds/buttonplugin/log.go index 13238f4..7d65d05 100644 --- a/cmds/buttonplugin/log.go +++ b/cmds/buttonplugin/log.go @@ -5,6 +5,7 @@ import ( "fmt" arg "github.com/alexflint/go-arg" "git.wit.org/wit/gui" + log "git.wit.org/wit/gui/log" ) @@ -14,6 +15,7 @@ var args struct { User string `arg:"env:USER"` Demo bool `help:"run a demo"` gui.GuiArgs + log.LogArgs } /* @@ -26,6 +28,11 @@ func init() { arg.MustParse(&args) fmt.Println(args.Foo, args.Bar, args.User) + if (args.Gui != "") { + gui.GuiArg.Gui = args.Gui + } + log.Log(true, "INIT() args.GuiArg.Gui =", gui.GuiArg.Gui) + /* log.Println() log.Println("STDOUT is now at /tmp/guilogfile") diff --git a/cmds/buttonplugin/main.go b/cmds/buttonplugin/main.go index 1fd8b5f..3200e9e 100644 --- a/cmds/buttonplugin/main.go +++ b/cmds/buttonplugin/main.go @@ -20,14 +20,7 @@ func main() { // This will turn on all debugging // gui.SetDebug(true) - // myGui = gui.New().LoadToolkit("gocui") - // myGui = gui.New().LoadToolkit("andlabs") - // myGui = gui.New().Default() - if (args.GuiToolkit == nil) { - myGui = gui.New().Default() - } else { - myGui = gui.New().LoadToolkit(args.GuiToolkit[0]) - } + myGui = gui.New().Default() buttonWindow() // This is just a optional goroutine to watch that things are alive diff --git a/cmds/cloudflare/Makefile b/cmds/cloudflare/Makefile new file mode 100644 index 0000000..bcd88c6 --- /dev/null +++ b/cmds/cloudflare/Makefile @@ -0,0 +1,18 @@ +run: build + ./cloudflare + +build-release: + go get -v -u -x . + go build + ./cloudflare + +build: + GO111MODULE="off" go get -v -x . + GO111MODULE="off" go build + +update: + GO111MODULE="off" go get -v -u -x . + +log: + reset + tail -f /tmp/witgui.* /tmp/guilogfile diff --git a/cmds/cloudflare/argv.go b/cmds/cloudflare/argv.go new file mode 100644 index 0000000..38579c7 --- /dev/null +++ b/cmds/cloudflare/argv.go @@ -0,0 +1,30 @@ +// This creates a simple hello world window +package main + +import ( + "fmt" + arg "github.com/alexflint/go-arg" + "git.wit.org/wit/gui" + log "git.wit.org/wit/gui/log" +) + + +var args struct { + Foo string + Bar bool + User string `arg:"env:USER"` + Demo bool `help:"run a demo"` + gui.GuiArgs + log.LogArgs +} + +func init() { + arg.MustParse(&args) + fmt.Println(args.Foo, args.Bar, args.User) + + if (args.Gui != "") { + gui.GuiArg.Gui = args.Gui + } + log.Log(true, "INIT() args.GuiArg.Gui =", gui.GuiArg.Gui) + +} diff --git a/cmds/cloudflare/dns.go b/cmds/cloudflare/dns.go new file mode 100644 index 0000000..6626843 --- /dev/null +++ b/cmds/cloudflare/dns.go @@ -0,0 +1,115 @@ +// This is a simple example +package main + +import ( + "os" + "log" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "strconv" +) + +// Define a struct to match the JSON structure of the response. +// This structure should be adjusted based on the actual format of the response. +type DNSRecords struct { + Result []struct { + ID string `json:"id"` + Type string `json:"type"` + Name string `json:"name"` + Content string `json:"content"` + Proxied bool `json:"proxied"` + Proxiable bool `json:"proxiable"` + TTL int `json:"ttl"` + } `json:"result"` +} + +// var domain string = "wit.org" +// var os.Getenv("CLOUDFLARE_DOMAIN") + +func loadDNS(hostname string) { + log.Println("adding DNS record") + + // more2.NewButton(name, func () { + // log.Println(name, "ip =", ip) + // }) + + newt := mainWindow.NewTab(hostname) + newg := newt.NewGroup("more") + more2 := newg.NewGrid("gridnuts", 5, gridH) + + records := getRecords() + for _, record := range records.Result { + more2.NewLabel(record.Type) + more2.NewLabel(record.Name) + if (record.Proxied) { + more2.NewLabel("Proxied") + } else { + more2.NewLabel("DNS") + } + var ttl, short string + if (record.TTL == 1) { + ttl = "Auto" + } else { + ttl = strconv.Itoa(record.TTL) + } + more2.NewLabel(ttl) + // short = fmt.Sprintf("%80s", record.Content) + short = record.Content + if len(short) > 40 { + short = short[:40] // Slice the first 20 characters + } + more2.NewLabel(short) + + fmt.Printf("ID: %s, Type: %s, Name: %s, short Content: %s\n", record.ID, record.Type, record.Name, short) + fmt.Printf("\tproxied: %b, %b, string TTL: %i\n", record.Proxied, record.Proxiable, ttl) + } +} + + +func getRecords() *DNSRecords { + var url string = os.Getenv("CLOUDFLARE_URL") + req, err := http.NewRequest("GET", url, nil) + if err != nil { + fmt.Println(err) + return nil + } + + var authKey string = os.Getenv("CLOUDFLARE_AUTHKEY") + var email string = os.Getenv("CLOUDFLARE_EMAIL") + + // Set headers + req.Header.Set("X-Auth-Key", authKey) + req.Header.Set("X-Auth-Email", email) + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + fmt.Println(err) + return nil + } + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + fmt.Println(err) + return nil + } + + var records DNSRecords + if err := json.Unmarshal(body, &records); err != nil { + fmt.Println(err) + return nil + } + + // Process the records as needed + /* + for _, record := range records.Result { + fmt.Printf("ID: %s, Type: %s, Name: %s, Content: %s\n", record.ID, record.Type, record.Name, record.Content) + fmt.Printf("\tproxied: %b, %b, TTL: %i\n", record.Proxied, record.Proxiable, record.TTL) + } + */ + + return &records +} diff --git a/cmds/cloudflare/main.go b/cmds/cloudflare/main.go new file mode 100644 index 0000000..75ba448 --- /dev/null +++ b/cmds/cloudflare/main.go @@ -0,0 +1,122 @@ +// This is a simple example +package main + +import ( + "os" + "fmt" + "log" + "strconv" + "git.wit.org/wit/gui" +) + +var title string = "Cloudflare DNS Control Panel" +var outfile string = "/tmp/guilogfile" +var myGui *gui.Node + +var buttonCounter int = 5 +var gridW int = 5 +var gridH int = 3 + +var mainWindow, more, more2 *gui.Node + +func main() { + myGui = gui.New().Default() + buttonWindow() + + // This is just a optional goroutine to watch that things are alive + gui.Watchdog() + gui.StandardExit() +} + +// This creates a window +func buttonWindow() { + var t, g *gui.Node + + log.Println("buttonWindow() START") + + mainWindow = myGui.NewWindow(title).SetText(title) + t = mainWindow.NewTab("Cloudflare") + g = t.NewGroup("buttons") + g1 := t.NewGroup("buttonGroup 2") + + more = g1.NewGroup("more") + showCloudflareCredentials(more) + + g1.NewButton("hello", func () { + log.Println("world") + }) + more2 = g1.NewGrid("gridnuts", gridW, gridH) + + var domain string = os.Getenv("CLOUDFLARE_DOMAIN") + if (domain == "") { + domain = "example.org" + } + + g.NewButton("Load " + domain + " DNS", func () { + loadDNS(domain) + }) + + g.NewButton("Load 'gocui'", func () { + // this set the xterm and mate-terminal window title. maybe works generally? + fmt.Println("\033]0;" + title + "blah \007") + myGui.LoadToolkit("gocui") + }) + + g.NewButton("Load 'andlabs'", func () { + myGui.LoadToolkit("andlabs") + }) + + g.NewButton("NewButton(more)", func () { + name := "foobar " + strconv.Itoa(buttonCounter) + log.Println("NewButton(more) Adding button", name) + buttonCounter += 1 + more.NewButton(name, func () { + log.Println("Got all the way to main() name =", name) + }) + }) + + g.NewButton("NewButton(more2)", func () { + name := "foobar " + strconv.Itoa(buttonCounter) + log.Println("NewButton(more2) Adding button", name) + buttonCounter += 1 + more2.NewButton(name, func () { + log.Println("Got all the way to main() name =", name) + }) + }) + + g.NewButton("NewButton(more2 d)", func () { + name := "d" + strconv.Itoa(buttonCounter) + log.Println("NewButton(more2 d) Adding button", name) + buttonCounter += 1 + more2.NewButton(name, func () { + log.Println("Got all the way to main() name =", name) + }) + }) + + g.NewButton("NewGroup()", func () { + name := "neat " + strconv.Itoa(buttonCounter) + log.Println("NewGroup() Adding button", name) + buttonCounter += 1 + more.NewGroup(name) + }) + + g.NewButton("gui.DebugWindow()", func () { + gui.DebugWindow() + }) +} + +func showCloudflareCredentials(box *gui.Node) { + grid := box.NewGrid("credsGrid", 2, 4) // width = 2 + + grid.NewLabel("Domain") + grid.NewLabel(os.Getenv("CLOUDFLARE_DOMAIN")) + + grid.NewLabel("Auth Key") + grid.NewLabel(os.Getenv("CLOUDFLARE_AUTHKEY")) + + grid.NewLabel("Email") + grid.NewLabel(os.Getenv("CLOUDFLARE_EMAIL")) + + grid.NewLabel("URL") + grid.NewLabel(os.Getenv("CLOUDFLARE_URL")) +} |
