From 78d3becdf3b538194f6926d1f2792a7a131de1d0 Mon Sep 17 00:00:00 2001 From: FengYa Date: Fri, 8 Jun 2018 13:44:38 +0800 Subject: [PATCH 1/5] create another module to use another ipinfo api --- ipinfohigherlimit/widget.go | 141 ++++++++++++++++++++++++++++++++++++ wtf.go | 4 + 2 files changed, 145 insertions(+) create mode 100644 ipinfohigherlimit/widget.go diff --git a/ipinfohigherlimit/widget.go b/ipinfohigherlimit/widget.go new file mode 100644 index 00000000..3fdabb3f --- /dev/null +++ b/ipinfohigherlimit/widget.go @@ -0,0 +1,141 @@ +package ipinfohigherlimit + +import ( + "encoding/json" + "fmt" + "net/http" + "strconv" + "text/template" + + "bytes" + + "github.com/olebedev/config" + "github.com/senorprogrammer/wtf/wtf" +) + +// Config is a pointer to the global config object +var Config *config.Config + +// Widget widget struct +type Widget struct { + wtf.TextWidget + result string + colors struct { + name, value string + } +} + +type ipinfo struct { + Query string `json:"query"` + ISP string `json:"isp"` + AS string `json:"as"` + City string `json:"city"` + Region string `json:"region"` + Country string `json:"country"` + CountryCode string `json:"countryCode"` + Latitude float64 `json:"lat"` + Longitude float64 `json:"lon"` + PostalCode string `json:"zip"` + Organization string `json:"org"` + Timezone string `json:"timezone"` +} + +// NewWidget constructor +func NewWidget() *Widget { + widget := Widget{ + TextWidget: wtf.NewTextWidget(" IPInfo ", "ipinfohigherlimit", false), + } + + widget.View.SetWrap(false) + + widget.config() + + return &widget +} + +// Refresh refresh the module +func (widget *Widget) Refresh() { + if widget.Disabled() { + return + } + + widget.UpdateRefreshedAt() + widget.ipinfo() + widget.View.Clear() + + widget.View.SetText(widget.result) +} + +//this method reads the config and calls ipinfo for ip information +func (widget *Widget) ipinfo() { + client := &http.Client{} + req, err := http.NewRequest("GET", "http://ip-api.com/json", nil) + if err != nil { + widget.result = fmt.Sprintf("%s", err.Error()) + return + } + req.Header.Set("User-Agent", "curl") + response, err := client.Do(req) + if err != nil { + widget.result = fmt.Sprintf("%s", err.Error()) + return + } + defer response.Body.Close() + if err != nil { + widget.result = fmt.Sprintf("%s", err.Error()) + return + } + var info ipinfo + err = json.NewDecoder(response.Body).Decode(&info) + if err != nil { + widget.result = fmt.Sprintf("%s", err.Error()) + return + } + + widget.setResult(&info) +} + +// read module configs +func (widget *Widget) config() { + nameColor, valueColor := Config.UString("wtf.mods.ipinfo.colors.name", "red"), Config.UString("wtf.mods.ipinfo.colors.value", "white") + widget.colors.name = nameColor + widget.colors.value = valueColor +} + +func (widget *Widget) setResult(info *ipinfo) { + resultTemplate, _ := template.New("ipinfo_result").Parse( + formatableText("IP Address", "Ip") + + formatableText("ISP", "ISP") + + formatableText("AS", "AS") + + formatableText("City", "City") + + formatableText("Region", "Region") + + formatableText("Country", "Country") + + formatableText("Coordinates", "Coordinates") + + formatableText("Postal Code", "PostalCode") + + formatableText("Organization", "Organization") + + formatableText("Timezone", "Timezone"), + ) + + resultBuffer := new(bytes.Buffer) + + resultTemplate.Execute(resultBuffer, map[string]string{ + "nameColor": widget.colors.name, + "valueColor": widget.colors.value, + "Ip": info.Query, + "ISP": info.ISP, + "AS": info.AS, + "City": info.City, + "Region": info.Region, + "Country": info.Country, + "Coordinates": strconv.FormatFloat(info.Latitude, 'f', 6, 64) + "," + strconv.FormatFloat(info.Longitude, 'f', 6, 64), + "PostalCode": info.PostalCode, + "Organization": info.Organization, + "Timezone": info.Timezone, + }) + + widget.result = resultBuffer.String() +} + +func formatableText(key, value string) string { + return fmt.Sprintf(" [{{.nameColor}}]%s: [{{.valueColor}}]{{.%s}}\n", key, value) +} diff --git a/wtf.go b/wtf.go index f0df2b06..ad739a99 100644 --- a/wtf.go +++ b/wtf.go @@ -20,6 +20,7 @@ import ( "github.com/senorprogrammer/wtf/github" "github.com/senorprogrammer/wtf/help" "github.com/senorprogrammer/wtf/ipinfo" + "github.com/senorprogrammer/wtf/ipinfohigherlimit" "github.com/senorprogrammer/wtf/jira" "github.com/senorprogrammer/wtf/newrelic" "github.com/senorprogrammer/wtf/opsgenie" @@ -176,6 +177,8 @@ func addWidget(app *tview.Application, pages *tview.Pages, widgetName string) { Widgets = append(Widgets, github.NewWidget(app, pages)) case "ipinfo": Widgets = append(Widgets, ipinfo.NewWidget()) + case "ipinfohigherlimit": + Widgets = append(Widgets, ipinfohigherlimit.NewWidget()) case "jira": Widgets = append(Widgets, jira.NewWidget()) case "newrelic": @@ -215,6 +218,7 @@ func makeWidgets(app *tview.Application, pages *tview.Pages) { git.Config = Config github.Config = Config ipinfo.Config = Config + ipinfohigherlimit.Config = Config jira.Config = Config newrelic.Config = Config opsgenie.Config = Config From 5ba7c6416fcd0ab8bb58cb460b3fedea238ce2bc Mon Sep 17 00:00:00 2001 From: FengYa Date: Fri, 8 Jun 2018 14:22:01 +0800 Subject: [PATCH 2/5] delete the useless clear operation --- ipinfohigherlimit/widget.go | 1 - 1 file changed, 1 deletion(-) diff --git a/ipinfohigherlimit/widget.go b/ipinfohigherlimit/widget.go index 3fdabb3f..32ed40e2 100644 --- a/ipinfohigherlimit/widget.go +++ b/ipinfohigherlimit/widget.go @@ -61,7 +61,6 @@ func (widget *Widget) Refresh() { widget.UpdateRefreshedAt() widget.ipinfo() - widget.View.Clear() widget.View.SetText(widget.result) } From b0af0d1d3be60bd7aded6be37a090f48dc489b36 Mon Sep 17 00:00:00 2001 From: FengYa Date: Fri, 8 Jun 2018 14:43:58 +0800 Subject: [PATCH 3/5] delete useless check code --- ipinfohigherlimit/widget.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/ipinfohigherlimit/widget.go b/ipinfohigherlimit/widget.go index 32ed40e2..e0106a15 100644 --- a/ipinfohigherlimit/widget.go +++ b/ipinfohigherlimit/widget.go @@ -55,13 +55,8 @@ func NewWidget() *Widget { // Refresh refresh the module func (widget *Widget) Refresh() { - if widget.Disabled() { - return - } - widget.UpdateRefreshedAt() widget.ipinfo() - widget.View.SetText(widget.result) } From 115e9df9a8671effc9923705804bd3339cf57a1a Mon Sep 17 00:00:00 2001 From: FengYa Date: Fri, 8 Jun 2018 14:56:57 +0800 Subject: [PATCH 4/5] delete useless repeat code --- ipinfohigherlimit/widget.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/ipinfohigherlimit/widget.go b/ipinfohigherlimit/widget.go index e0106a15..9b781d0d 100644 --- a/ipinfohigherlimit/widget.go +++ b/ipinfohigherlimit/widget.go @@ -75,10 +75,6 @@ func (widget *Widget) ipinfo() { return } defer response.Body.Close() - if err != nil { - widget.result = fmt.Sprintf("%s", err.Error()) - return - } var info ipinfo err = json.NewDecoder(response.Body).Decode(&info) if err != nil { From 97aafd60f932b19c55ff662fa30b80abcb7d5706 Mon Sep 17 00:00:00 2001 From: FengYa Date: Sat, 9 Jun 2018 23:06:51 +0800 Subject: [PATCH 5/5] change name of the module --- {ipinfohigherlimit => ipapi}/widget.go | 4 ++-- wtf.go | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) rename {ipinfohigherlimit => ipapi}/widget.go (97%) diff --git a/ipinfohigherlimit/widget.go b/ipapi/widget.go similarity index 97% rename from ipinfohigherlimit/widget.go rename to ipapi/widget.go index 9b781d0d..d65f2456 100644 --- a/ipinfohigherlimit/widget.go +++ b/ipapi/widget.go @@ -1,4 +1,4 @@ -package ipinfohigherlimit +package ipapi import ( "encoding/json" @@ -43,7 +43,7 @@ type ipinfo struct { // NewWidget constructor func NewWidget() *Widget { widget := Widget{ - TextWidget: wtf.NewTextWidget(" IPInfo ", "ipinfohigherlimit", false), + TextWidget: wtf.NewTextWidget(" IPInfo ", "ipapi", false), } widget.View.SetWrap(false) diff --git a/wtf.go b/wtf.go index 8957edd5..7cfe4c74 100644 --- a/wtf.go +++ b/wtf.go @@ -21,7 +21,7 @@ import ( "github.com/senorprogrammer/wtf/github" "github.com/senorprogrammer/wtf/help" "github.com/senorprogrammer/wtf/ipinfo" - "github.com/senorprogrammer/wtf/ipinfohigherlimit" + "github.com/senorprogrammer/wtf/ipapi" "github.com/senorprogrammer/wtf/jira" "github.com/senorprogrammer/wtf/newrelic" "github.com/senorprogrammer/wtf/opsgenie" @@ -186,8 +186,8 @@ func addWidget(app *tview.Application, pages *tview.Pages, widgetName string) { Widgets = append(Widgets, github.NewWidget(app, pages)) case "ipinfo": Widgets = append(Widgets, ipinfo.NewWidget()) - case "ipinfohigherlimit": - Widgets = append(Widgets, ipinfohigherlimit.NewWidget()) + case "ipapi": + Widgets = append(Widgets, ipapi.NewWidget()) case "jira": Widgets = append(Widgets, jira.NewWidget()) case "newrelic": @@ -228,7 +228,7 @@ func makeWidgets(app *tview.Application, pages *tview.Pages) { git.Config = Config github.Config = Config ipinfo.Config = Config - ipinfohigherlimit.Config = Config + ipapi.Config = Config jira.Config = Config newrelic.Config = Config opsgenie.Config = Config