From 1e57991aef67687b22ad952358ec429322dc7858 Mon Sep 17 00:00:00 2001 From: harisabdullah Date: Mon, 7 Aug 2023 12:27:14 +0500 Subject: [PATCH 1/3] Removing nil values from the yaml output `ipinfo -y` --- ipinfo/cmd_ip.go | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/ipinfo/cmd_ip.go b/ipinfo/cmd_ip.go index 863b2f58..14352382 100644 --- a/ipinfo/cmd_ip.go +++ b/ipinfo/cmd_ip.go @@ -1,8 +1,10 @@ package main import ( + "encoding/json" "fmt" "net" + "reflect" "github.com/fatih/color" "github.com/ipinfo/cli/lib/complete" @@ -111,9 +113,31 @@ func cmdIP(ipStr string) error { return outputCSV(data) } if fYAML { - return outputYAML(data) + // Convert struct to map + var initialMap map[string]interface{} + tempJSON, _ := json.Marshal(data) + json.Unmarshal(tempJSON, &initialMap) + + // Remove nil values from map + cleanedData := removeNilValues(initialMap) + + // Output as YAML + return outputYAML(cleanedData) } outputFriendlyCore(data) return nil } + +func removeNilValues(data interface{}) interface{} { + if reflect.ValueOf(data).Kind() == reflect.Map { + cleanedMap := make(map[string]interface{}) + for k, v := range data.(map[string]interface{}) { + if v != nil { + cleanedMap[k] = removeNilValues(v) + } + } + return cleanedMap + } + return data +} From c69e5730ee372a2c50348fbcc5cc9bdbd18aac36 Mon Sep 17 00:00:00 2001 From: harisabdullah Date: Wed, 9 Aug 2023 10:57:07 +0500 Subject: [PATCH 2/3] Removed null enteries --- ipinfo/cmd_ip.go | 29 +------------ vendor/github.com/ipinfo/go/v2/ipinfo/core.go | 42 +++++++++---------- 2 files changed, 23 insertions(+), 48 deletions(-) diff --git a/ipinfo/cmd_ip.go b/ipinfo/cmd_ip.go index 14352382..203a0ea2 100644 --- a/ipinfo/cmd_ip.go +++ b/ipinfo/cmd_ip.go @@ -1,16 +1,13 @@ package main import ( - "encoding/json" "fmt" - "net" - "reflect" - "github.com/fatih/color" "github.com/ipinfo/cli/lib/complete" "github.com/ipinfo/cli/lib/complete/predict" "github.com/ipinfo/go/v2/ipinfo" "github.com/spf13/pflag" + "net" ) var completionsIP = &complete.Command{ @@ -113,31 +110,9 @@ func cmdIP(ipStr string) error { return outputCSV(data) } if fYAML { - // Convert struct to map - var initialMap map[string]interface{} - tempJSON, _ := json.Marshal(data) - json.Unmarshal(tempJSON, &initialMap) - - // Remove nil values from map - cleanedData := removeNilValues(initialMap) - - // Output as YAML - return outputYAML(cleanedData) + return outputYAML(data) } outputFriendlyCore(data) return nil } - -func removeNilValues(data interface{}) interface{} { - if reflect.ValueOf(data).Kind() == reflect.Map { - cleanedMap := make(map[string]interface{}) - for k, v := range data.(map[string]interface{}) { - if v != nil { - cleanedMap[k] = removeNilValues(v) - } - } - return cleanedMap - } - return data -} diff --git a/vendor/github.com/ipinfo/go/v2/ipinfo/core.go b/vendor/github.com/ipinfo/go/v2/ipinfo/core.go index 808b1a16..257ccdaa 100644 --- a/vendor/github.com/ipinfo/go/v2/ipinfo/core.go +++ b/vendor/github.com/ipinfo/go/v2/ipinfo/core.go @@ -8,27 +8,27 @@ import ( // Core represents data from the Core API. type Core struct { IP net.IP `json:"ip" csv:"ip"` - Hostname string `json:"hostname,omitempty" csv:"hostname"` - Bogon bool `json:"bogon,omitempty" csv:"bogon"` - Anycast bool `json:"anycast,omitempty" csv:"anycast"` - City string `json:"city,omitempty" csv:"city"` - Region string `json:"region,omitempty" csv:"region"` - Country string `json:"country,omitempty" csv:"country"` - CountryName string `json:"country_name,omitempty" csv:"country_name"` - CountryFlag CountryFlag `json:"country_flag,omitempty" csv:"country_flag_,inline"` - CountryCurrency CountryCurrency `json:"country_currency,omitempty" csv:"country_currency_,inline"` - Continent Continent `json:"continent,omitempty" csv:"continent_,inline"` - IsEU bool `json:"isEU,omitempty" csv:"isEU"` - Location string `json:"loc,omitempty" csv:"loc"` - Org string `json:"org,omitempty" csv:"org"` - Postal string `json:"postal,omitempty" csv:"postal"` - Timezone string `json:"timezone,omitempty" csv:"timezone"` - ASN *CoreASN `json:"asn,omitempty" csv:"asn_,inline"` - Company *CoreCompany `json:"company,omitempty" csv:"company_,inline"` - Carrier *CoreCarrier `json:"carrier,omitempty" csv:"carrier_,inline"` - Privacy *CorePrivacy `json:"privacy,omitempty" csv:"privacy_,inline"` - Abuse *CoreAbuse `json:"abuse,omitempty" csv:"abuse_,inline"` - Domains *CoreDomains `json:"domains,omitempty" csv:"domains_,inline"` + Hostname string `json:"hostname,omitempty" csv:"hostname" yaml:"hostname,omitempty"` + Bogon bool `json:"bogon,omitempty" csv:"bogon" yaml:"bogon,omitempty"` + Anycast bool `json:"anycast,omitempty" csv:"anycast" yaml:"anycast,omitempty"` + City string `json:"city,omitempty" csv:"city" yaml:"city,omitempty"` + Region string `json:"region,omitempty" csv:"region" yaml:"region,omitempty"` + Country string `json:"country,omitempty" csv:"country" yaml:"country,omitempty"` + CountryName string `json:"country_name,omitempty" csv:"country_name" yaml:"countryName,omitempty"` + CountryFlag CountryFlag `json:"country_flag,omitempty" csv:"country_flag_,inline" yaml:"countryFlag,omitempty"` + CountryCurrency CountryCurrency `json:"country_currency,omitempty" csv:"country_currency_,inline "yaml:"countryCurrency,omitempty"` + Continent Continent `json:"continent,omitempty" csv:"continent_,inline" yaml:"continent,omitempty"` + IsEU bool `json:"isEU,omitempty" csv:"isEU" yaml:"isEU,omitempty"` + Location string `json:"loc,omitempty" csv:"loc" yaml:"location,omitempty"` + Org string `json:"org,omitempty" csv:"org" yaml:"org,omitempty"` + Postal string `json:"postal,omitempty" csv:"postal" yaml:"postal,omitempty"` + Timezone string `json:"timezone,omitempty" csv:"timezone" yaml:"timezone,omitempty"` + ASN *CoreASN `json:"asn,omitempty" csv:"asn_,inline" yaml:"ASN,omitempty"` + Company *CoreCompany `json:"company,omitempty" csv:"company_,inline" yaml:"company,omitempty"` + Carrier *CoreCarrier `json:"carrier,omitempty" csv:"carrier_,inline" yaml:"carrier,omitempty"` + Privacy *CorePrivacy `json:"privacy,omitempty" csv:"privacy_,inline" yaml:"privacy,omitempty"` + Abuse *CoreAbuse `json:"abuse,omitempty" csv:"abuse_,inline" yaml:"abuse,omitempty"` + Domains *CoreDomains `json:"domains,omitempty" csv:"domains_,inline" yaml:"domains,omitempty"` } // CoreASN represents ASN data for the Core API. From 52979b5a9d3fa5bc1952ad5127ff3ca333bb70f1 Mon Sep 17 00:00:00 2001 From: harisabdullah Date: Fri, 18 Aug 2023 10:57:48 +0500 Subject: [PATCH 3/3] Updating dependencies: `go mod vendor` --- go.mod | 2 +- go.sum | 2 ++ vendor/github.com/ipinfo/go/v2/ipinfo/client.go | 2 +- vendor/github.com/ipinfo/go/v2/ipinfo/core.go | 6 ++++-- vendor/github.com/ipinfo/go/v2/ipinfo/countries.go | 10 ++++++++++ vendor/modules.txt | 2 +- 6 files changed, 19 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 10f37c1a..c1614234 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.20 require ( github.com/fatih/color v1.13.0 github.com/hashicorp/go-multierror v1.1.1 - github.com/ipinfo/go/v2 v2.9.2 + github.com/ipinfo/go/v2 v2.9.4 github.com/ipinfo/mmdbctl v0.0.0-20230726080357-558a20ba8524 github.com/jszwec/csvutil v1.4.0 github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4 diff --git a/go.sum b/go.sum index 39ee3dc2..bbc4dd7e 100644 --- a/go.sum +++ b/go.sum @@ -12,6 +12,8 @@ github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+l github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM= github.com/ipinfo/go/v2 v2.9.2 h1:wih7S6ifXAdGE7OH5fgTfC/yA/lFYRKaG5z4FNiE+MY= github.com/ipinfo/go/v2 v2.9.2/go.mod h1:tRDkYfM20b1XzNqorn1Q1O6Xtg7uzw3Wn3I2R0SyJh4= +github.com/ipinfo/go/v2 v2.9.4 h1:6LcbIQ6o/bfNgqGMoJCfkUxVtJsbLWdB9sDq4Vv3fF0= +github.com/ipinfo/go/v2 v2.9.4/go.mod h1:tRDkYfM20b1XzNqorn1Q1O6Xtg7uzw3Wn3I2R0SyJh4= github.com/ipinfo/mmdbctl v0.0.0-20230726080357-558a20ba8524 h1:om+KMCwwLE1scrxr5YirYikcgl27Z2KbKQSpobt/2b0= github.com/ipinfo/mmdbctl v0.0.0-20230726080357-558a20ba8524/go.mod h1:RVULurqCcBRklgMU3st4j+sIEuVNY+mB1IMhhi/wjLA= github.com/jszwec/csvutil v1.4.0 h1:ro7gZN8PRsyNUEX8qE/eYPE5/kffEXMs+4eRcOd1oUk= diff --git a/vendor/github.com/ipinfo/go/v2/ipinfo/client.go b/vendor/github.com/ipinfo/go/v2/ipinfo/client.go index 1677b806..dee551d6 100644 --- a/vendor/github.com/ipinfo/go/v2/ipinfo/client.go +++ b/vendor/github.com/ipinfo/go/v2/ipinfo/client.go @@ -13,7 +13,7 @@ import ( const ( defaultBaseURL = "https://ipinfo.io/" - defaultUserAgent = "IPinfoClient/Go/2.9.2" + defaultUserAgent = "IPinfoClient/Go/2.9.4" ) // A Client is the main handler to communicate with the IPinfo API. diff --git a/vendor/github.com/ipinfo/go/v2/ipinfo/core.go b/vendor/github.com/ipinfo/go/v2/ipinfo/core.go index 257ccdaa..a09cd3b0 100644 --- a/vendor/github.com/ipinfo/go/v2/ipinfo/core.go +++ b/vendor/github.com/ipinfo/go/v2/ipinfo/core.go @@ -16,14 +16,15 @@ type Core struct { Country string `json:"country,omitempty" csv:"country" yaml:"country,omitempty"` CountryName string `json:"country_name,omitempty" csv:"country_name" yaml:"countryName,omitempty"` CountryFlag CountryFlag `json:"country_flag,omitempty" csv:"country_flag_,inline" yaml:"countryFlag,omitempty"` - CountryCurrency CountryCurrency `json:"country_currency,omitempty" csv:"country_currency_,inline "yaml:"countryCurrency,omitempty"` + CountryFlagURL string `json:"country_flag_url,omitempty" csv:"country_flag_url" yaml:"countryFlagURL,omitempty"` + CountryCurrency CountryCurrency `json:"country_currency,omitempty" csv:"country_currency_,inline" yaml:"countryCurrency,omitempty"` Continent Continent `json:"continent,omitempty" csv:"continent_,inline" yaml:"continent,omitempty"` IsEU bool `json:"isEU,omitempty" csv:"isEU" yaml:"isEU,omitempty"` Location string `json:"loc,omitempty" csv:"loc" yaml:"location,omitempty"` Org string `json:"org,omitempty" csv:"org" yaml:"org,omitempty"` Postal string `json:"postal,omitempty" csv:"postal" yaml:"postal,omitempty"` Timezone string `json:"timezone,omitempty" csv:"timezone" yaml:"timezone,omitempty"` - ASN *CoreASN `json:"asn,omitempty" csv:"asn_,inline" yaml:"ASN,omitempty"` + ASN *CoreASN `json:"asn,omitempty" csv:"asn_,inline" yaml:"asn,omitempty"` Company *CoreCompany `json:"company,omitempty" csv:"company_,inline" yaml:"company,omitempty"` Carrier *CoreCarrier `json:"carrier,omitempty" csv:"carrier_,inline" yaml:"carrier,omitempty"` Privacy *CorePrivacy `json:"privacy,omitempty" csv:"privacy_,inline" yaml:"privacy,omitempty"` @@ -88,6 +89,7 @@ func (v *Core) setCountryName() { v.IsEU = IsEU(v.Country) v.CountryFlag.Emoji = GetCountryFlagEmoji(v.Country) v.CountryFlag.Unicode = GetCountryFlagUnicode(v.Country) + v.CountryFlagURL = GetCountryFlagURL(v.Country) v.CountryCurrency.Code = GetCountryCurrencyCode(v.Country) v.CountryCurrency.Symbol = GetCountryCurrencySymbol(v.Country) v.Continent.Code = GetContinentCode(v.Country) diff --git a/vendor/github.com/ipinfo/go/v2/ipinfo/countries.go b/vendor/github.com/ipinfo/go/v2/ipinfo/countries.go index 2f188754..d5493800 100644 --- a/vendor/github.com/ipinfo/go/v2/ipinfo/countries.go +++ b/vendor/github.com/ipinfo/go/v2/ipinfo/countries.go @@ -1,5 +1,9 @@ package ipinfo +const ( + countryFlagsBaseURL = "https://cdn.ipinfo.io/static/images/countries-flags/" +) + // GetCountryName gets the full name of a country from its code, e.g. // "PK" -> "Pakistan". func GetCountryName(country string) string { @@ -42,6 +46,12 @@ func GetContinentName(country string) string { return continents[country].Name } +// GetCountryFlagURL gets the URL of the country flag, e.g. +// "PK" -> "https://cdn.ipinfo.io/static/images/countries-flags/PK.svg". +func GetCountryFlagURL(country string) string { + return countryFlagsBaseURL + country + ".svg" +} + // IsEU takes the country code and returns `true` // if the country is a member of the EU, e.g. "SE" -> true func IsEU(country string) bool { diff --git a/vendor/modules.txt b/vendor/modules.txt index 9678d80a..dad9696b 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -10,7 +10,7 @@ github.com/hashicorp/errwrap # github.com/hashicorp/go-multierror v1.1.1 ## explicit; go 1.13 github.com/hashicorp/go-multierror -# github.com/ipinfo/go/v2 v2.9.2 +# github.com/ipinfo/go/v2 v2.9.4 ## explicit; go 1.15 github.com/ipinfo/go/v2/ipinfo github.com/ipinfo/go/v2/ipinfo/cache