Skip to content

Commit

Permalink
Display the returned JSON data with formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Calinou committed Apr 29, 2018
1 parent f406d65 commit d4bbe86
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
26 changes: 25 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@
[[constraint]]
name = "github.com/urfave/cli"
version = "1.20.0"

[[constraint]]
name = "github.com/fatih/color"
version = "1.6.0"
14 changes: 14 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ package main
import (
"encoding/json"
"fmt"
"github.com/fatih/color"
"github.com/urfave/cli"
"log"
"net/http"
Expand Down Expand Up @@ -58,10 +59,23 @@ func main() {
os.Exit(1)
}

// Decode the returned JSON into a struct
defer req.Body.Close()
ipInfo := IpInfo{}
json.NewDecoder(req.Body).Decode(&ipInfo)

// Display the result
fmt.Println(
"\n IP address ", color.HiCyanString(ipInfo.Ip),
"\n Hostname ", color.HiCyanString(ipInfo.Hostname),
"\n City ", color.HiCyanString(ipInfo.City),
"\n Region ", color.HiCyanString(ipInfo.Region),
"\n Country ", color.HiCyanString(ipInfo.Country),
"\n Location ", color.HiCyanString(ipInfo.Loc),
"\n Postal code ", color.HiCyanString(ipInfo.Postal),
"\n Organization ", color.HiCyanString(ipInfo.Org),
)

return nil
}

Expand Down

0 comments on commit d4bbe86

Please sign in to comment.