diff options
Diffstat (limited to 'examples/cloudflare')
| -rw-r--r-- | examples/cloudflare/Makefile | 18 | ||||
| -rw-r--r-- | examples/cloudflare/argv.go | 30 | ||||
| -rw-r--r-- | examples/cloudflare/dns.go | 132 | ||||
| -rw-r--r-- | examples/cloudflare/main.go | 84 |
4 files changed, 264 insertions, 0 deletions
diff --git a/examples/cloudflare/Makefile b/examples/cloudflare/Makefile new file mode 100644 index 0000000..bcd88c6 --- /dev/null +++ b/examples/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/examples/cloudflare/argv.go b/examples/cloudflare/argv.go new file mode 100644 index 0000000..38579c7 --- /dev/null +++ b/examples/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/examples/cloudflare/dns.go b/examples/cloudflare/dns.go new file mode 100644 index 0000000..eb8de23 --- /dev/null +++ b/examples/cloudflare/dns.go @@ -0,0 +1,132 @@ +// 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") + + newt := mainWindow.NewTab(hostname) + newg := newt.NewGroup("more") + grid := newg.NewGrid("gridnuts", 5, gridH) + +// grid.NewButton("Type", func () { +// log.Println("sort by Type") +// }) + typedrop := grid.NewDropdown("type") + typedrop.AddText("A") + typedrop.AddText("AAAA") + typedrop.AddText("CNAME") + typedrop.Custom = func () { + log.Println("custom dropdown() a =", typedrop.Name, typedrop.S) + } + grid.NewButton("Name", func () { + log.Println("sort by Name") + }) + grid.NewButton("Protection", func () { + log.Println("sort proxied") + }) + grid.NewButton("TTL", func () { + log.Println("sort by TTL") + }) + grid.NewButton("Value", func () { + log.Println("sort by Value") + }) + + newt.NewButton("Save", func () { + log.Println("save stuff to cloudflare") + }) + + records := getRecords() + for _, record := range records.Result { + grid.NewLabel(record.Type) + textbox := grid.NewTextbox(record.Name) + textbox.SetText(record.Name) + if (record.Proxied) { + grid.NewLabel("Proxied") + } else { + grid.NewLabel("DNS") + } + var ttl, short string + if (record.TTL == 1) { + ttl = "Auto" + } else { + ttl = strconv.Itoa(record.TTL) + } + grid.NewLabel(ttl) + // short = fmt.Sprintf("%80s", record.Content) + short = record.Content + if len(short) > 40 { + short = short[:40] // Slice the first 20 characters + } + + namebox := grid.NewTextbox(short) + namebox.SetText(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 + } + + return &records +} diff --git a/examples/cloudflare/main.go b/examples/cloudflare/main.go new file mode 100644 index 0000000..b83d276 --- /dev/null +++ b/examples/cloudflare/main.go @@ -0,0 +1,84 @@ +// This is a simple example +package main + +import ( + "os" + "fmt" + "log" + "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) + + // 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("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")) +} |
