diff options
| author | Jeff Carr <[email protected]> | 2023-12-14 10:36:56 -0600 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2023-12-14 10:36:56 -0600 |
| commit | 282119d970faed3f8a60d5105a2f26ee14681ff4 (patch) | |
| tree | 1680731c899f0e147487b9ba4d50ace2f3e96eb1 /examples/cloudflare/main.go | |
| parent | 9d075afb1df62276dea06be4a188eaee8fc69420 (diff) | |
tabs, windows + gocui dropdown menu (almost)
dropdown menu figures out what text was clicked
dropdown menu movement changes line colors
dropdown menus force user to select a response
accidentally committed a binary
tab selection works
tab and window views almost working
tabs and windows almost working
window widgets selection works
better color handling
using gocui view.Visable flag
removal of old color setting code
still need an artificial delay for andlabs SetText()
catching more 'nil' errors
fixed the stupid duplicate tab problem in andlabs
figured out how andlabs had a tab/box mess
works on more than one domain
builds and runs again
debugging double tabs in andlabs gui
GO111MODULE compile notes
code reorg
further improvements
example cloudflare app does first successful dns update
add NewEntryLine() for single line entry boxes
Signed-off-by: Jeff Carr <[email protected]>
Diffstat (limited to 'examples/cloudflare/main.go')
| -rw-r--r-- | examples/cloudflare/main.go | 186 |
1 files changed, 167 insertions, 19 deletions
diff --git a/examples/cloudflare/main.go b/examples/cloudflare/main.go index b83d276..badf97a 100644 --- a/examples/cloudflare/main.go +++ b/examples/cloudflare/main.go @@ -5,11 +5,14 @@ import ( "os" "fmt" "log" + "bufio" + "strings" "git.wit.org/wit/gui" ) var title string = "Cloudflare DNS Control Panel" var outfile string = "/tmp/guilogfile" +var configfile string = ".config/wit/cloudflare" var myGui *gui.Node var buttonCounter int = 5 @@ -19,8 +22,10 @@ var gridH int = 3 var mainWindow, more, more2 *gui.Node func main() { + config = make(map[string]*configT) + readConfig() myGui = gui.New().Default() - buttonWindow() + makeCloudflareWindow() // This is just a optional goroutine to watch that things are alive gui.Watchdog() @@ -28,30 +33,90 @@ func main() { } // This creates a window -func buttonWindow() { - var t, g *gui.Node +func makeCloudflareWindow() { + var t *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) + // this tab has the master cloudflare API credentials + makeConfigTab(mainWindow) + + t = mainWindow.NewTab("Zones") + g1 := t.NewGroup("zones") - // more2 = g1.NewGrid("gridnuts", gridW, gridH) + // make dropdown list of zones + zonedrop = g1.NewDropdown("zone") + zonedrop.AddText("example.org") + for d, _ := range config { + zonedrop.AddText(d) + } - var domain string = os.Getenv("CLOUDFLARE_DOMAIN") - if (domain == "") { - domain = "example.org" + zonedrop.Custom = func () { + domain := zonedrop.S + log.Println("custom dropdown() zone (domain name) =", zonedrop.Name, domain) + if (config[domain] == nil) { + log.Println("custom dropdown() config[domain] = nil for domain =", domain) + domainWidget.SetText(domain) + zoneWidget.SetText("") + authWidget.SetText("") + emailWidget.SetText("") + } else { + log.Println("custom dropdown() a =", domain, config[domain].zoneID, config[domain].auth, config[domain].email) + domainWidget.SetText(config[domain].domain) + zoneWidget.SetText(config[domain].zoneID) + authWidget.SetText(config[domain].auth) + emailWidget.SetText(config[domain].email) + } } - g.NewButton("Load " + domain + " DNS", func () { - loadDNS(domain) + more = g1.NewGroup("data") + showCloudflareCredentials(more) + + makeDebugTab(mainWindow) +} + +func makeConfigTab(window *gui.Node) { + t := window.NewTab("Get Zones") + vb := t.NewBox("vBox", false) + g1 := vb.NewGroup("Cloudflare API Config") + + g1.NewLabel("If you have an API key with access to list all of /n your zone files, enter it here. \n \n Alternatively, you can set the enviroment variables: \n env $CLOUDFLARE_AUTHKEY \n env $CLOUDFLARE_EMAIL \n env $CLOUDFLARE_URL \n") + + // make grid to display credentials + grid := g1.NewGrid("credsGrid", 2, 4) // width = 2 + + grid.NewLabel("Auth Key") + aw := grid.NewEntryLine(os.Getenv("CLOUDFLARE_AUTHKEY")) + aw.SetText(os.Getenv("CLOUDFLARE_AUTHKEY")) + + grid.NewLabel("Email") + ew := grid.NewEntryLine(os.Getenv("CLOUDFLARE_EMAIL")) + ew.SetText(os.Getenv("CLOUDFLARE_EMAIL")) + + var url string = "https://api.cloudflare.com/client/v4/zones/" + grid.NewLabel("Cloudflare API") + grid.NewLabel(url) + + grid.Pad() + + vb.NewButton("getZones()", func () { + log.Println("getZones()") + getZones(aw.S, ew.S) }) + t.Pad() + t.Margin() + vb.Pad() + vb.Margin() + g1.Pad() + g1.Margin() +} + +func makeDebugTab(window *gui.Node) { + t2 := window.NewTab("debug") + g := t2.NewGroup("debug") g.NewButton("Load 'gocui'", func () { // this set the xterm and mate-terminal window title. maybe works generally? fmt.Println("\033]0;" + title + "blah \007") @@ -65,20 +130,103 @@ func buttonWindow() { g.NewButton("gui.DebugWindow()", func () { gui.DebugWindow() }) + + g.NewButton("List all Widgets", func () { + myGui.ListChildren(true) + }) + g.NewButton("Dump all Widgets", func () { + myGui.Dump() + }) } func showCloudflareCredentials(box *gui.Node) { + // make grid to display credentials grid := box.NewGrid("credsGrid", 2, 4) // width = 2 grid.NewLabel("Domain") - grid.NewLabel(os.Getenv("CLOUDFLARE_DOMAIN")) + domainWidget = grid.NewEntryLine(os.Getenv("CLOUDFLARE_DOMAIN")) + + grid.NewLabel("Zone ID") + zoneWidget = grid.NewEntryLine(os.Getenv("CLOUDFLARE_ZONEID")) grid.NewLabel("Auth Key") - grid.NewLabel(os.Getenv("CLOUDFLARE_AUTHKEY")) + authWidget = grid.NewEntryLine(os.Getenv("CLOUDFLARE_AUTHKEY")) grid.NewLabel("Email") - grid.NewLabel(os.Getenv("CLOUDFLARE_EMAIL")) + emailWidget = grid.NewEntryLine(os.Getenv("CLOUDFLARE_EMAIL")) + + var url string = "https://api.cloudflare.com/client/v4/zones/" + grid.NewLabel("Cloudflare API") + grid.NewLabel(url) + + grid.Pad() + + saveButton = box.NewButton("Save to config", func () { + }) + saveButton.Disable() + + loadButton = box.NewButton("Load Cloudflare DNS zonefile", func () { + var domain configT + domain.domain = domainWidget.S + domain.zoneID = zoneWidget.S + domain.auth = authWidget.S + domain.email = emailWidget.S + loadDNS(&domain) + }) +} + +func readConfig() { + homeDir, err := os.UserHomeDir() + if err != nil { + log.Println("searchPaths() error. exiting here?") + } + filename := homeDir + "/" + configfile + log.Println("filename =", filename) + + readFileLineByLine(filename) + // os.Exit(0) +} + +// readFileLineByLine opens a file and reads through each line. +func readFileLineByLine(filename string) error { + // Open the file. + file, err := os.Open(filename) + if err != nil { + return err + } + defer file.Close() + + log.Println("readFileLineByLine() =", filename) + + // Create a new Scanner for the file. + scanner := bufio.NewScanner(file) + + // Read through each line using scanner. + for scanner.Scan() { + var newc *configT + newc = new(configT) + + line := scanner.Text() + parts := strings.Fields(line) + + if (len(parts) < 4) { + log.Println("readFileLineByLine() SKIP =", parts) + continue + } + + newc.domain = parts[0] + newc.zoneID = parts[1] + newc.auth = parts[2] + newc.email = parts[3] + + config[parts[0]] = newc + log.Println("readFileLineByLine() =", newc.domain, newc.zoneID, newc.auth, newc.email) + } + + // Check for errors during Scan. + if err := scanner.Err(); err != nil { + return err + } - grid.NewLabel("URL") - grid.NewLabel(os.Getenv("CLOUDFLARE_URL")) + return nil } |
