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 2c801fa2..ac185b2a 100644 --- a/wtf.go +++ b/wtf.go @@ -22,6 +22,7 @@ import ( "github.com/senorprogrammer/wtf/gitlab" "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" @@ -188,6 +189,8 @@ func addWidget(app *tview.Application, pages *tview.Pages, widgetName string) { Widgets = append(Widgets, gitlab.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": @@ -229,6 +232,7 @@ func makeWidgets(app *tview.Application, pages *tview.Pages) { github.Config = Config gitlab.Config = Config ipinfo.Config = Config + ipinfohigherlimit.Config = Config jira.Config = Config newrelic.Config = Config opsgenie.Config = Config