Skip to content

Commit f98c542

Browse files
committed
output formate
1 parent 0d13968 commit f98c542

File tree

5 files changed

+86
-22
lines changed

5 files changed

+86
-22
lines changed

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
module github.com/OpenIoTHub/getip
22

33
go 1.15
4+
5+
require (
6+
github.com/urfave/cli/v2 v2.3.0
7+
gopkg.in/yaml.v2 v2.2.3
8+
)

go.sum

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
2+
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
3+
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
4+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6+
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
7+
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
8+
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
9+
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
10+
github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
11+
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
12+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
13+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
14+
gopkg.in/yaml.v2 v2.2.3 h1:fvjTMHxHEw/mxHbtzPi3JCcKXQRAnQTBRo6YCJSVHKI=
15+
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

iputils/utils.go

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package iputils
22

33
import (
4-
"errors"
54
"io/ioutil"
65
"log"
76
"net"
@@ -57,54 +56,52 @@ var Ipv6APIUrls = []string{
5756
"http://v6.ipv6-test.com/api/myip.php",
5857
}
5958

60-
func GetMyPublicIpv4() (string, error) {
59+
func GetMyPublicIpv4() string {
6160
for _, url := range Ipv4APIUrls {
6261
resp, err := http.Get(url)
63-
if resp != nil && resp.Body != nil {
64-
defer resp.Body.Close()
65-
}
6662
if err != nil {
6763
log.Printf("get public ipv4 err:%s", err)
6864
continue
6965
}
7066
bytes, err := ioutil.ReadAll(resp.Body)
7167
if err != nil {
7268
log.Printf("get public ipv4 err:%s", err)
69+
_ = resp.Body.Close()
7370
continue
7471
}
7572
ipv4 := strings.Replace(string(bytes), "\n", "", -1)
7673
ip := net.ParseIP(ipv4)
7774
if ip != nil {
78-
log.Println("got ipv4 addr:", ip.String())
79-
return ip.String(), nil
75+
_ = resp.Body.Close()
76+
return ip.String()
8077
}
8178
}
82-
return "", errors.New("ipv4 not found")
79+
return ""
8380
}
8481

85-
func GetMyPublicIpv6() (string, error) {
82+
func GetMyPublicIpv6() string {
8683
for _, url := range Ipv6APIUrls {
8784
resp, err := http.Get(url)
88-
if resp != nil && resp.Body != nil {
89-
defer resp.Body.Close()
90-
}
9185
if err != nil {
9286
log.Printf("get public ipv6 err:%s", err)
9387
continue
9488
}
89+
// 读取 IPv6
9590
bytes, err := ioutil.ReadAll(resp.Body)
9691
if err != nil {
9792
log.Printf("get public ipv6 err:%s", err)
93+
_ = resp.Body.Close()
9894
continue
9995
}
96+
// 删除 document.write(xxx) (如有)
10097
tmp := strings.Replace(string(bytes), "document.write('", "", -1)
101-
tmp2 := strings.Replace(tmp, "');", "", -1)
102-
ipv6 := strings.Replace(tmp2, "\n", "", -1)
98+
tmp = strings.Replace(tmp, "');", "", -1)
99+
ipv6 := strings.Replace(tmp, "\n", "", -1)
103100
ip := net.ParseIP(ipv6)
104101
if ip != nil {
105-
log.Println("got ipv6 addr:", ip.String())
106-
return ip.String(), nil
102+
_ = resp.Body.Close()
103+
return ip.String()
107104
}
108105
}
109-
return "", errors.New("pv6 not found")
106+
return ""
110107
}

main.go

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,58 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"github.com/OpenIoTHub/getip/iputils"
7+
"github.com/OpenIoTHub/getip/models"
8+
"github.com/urfave/cli/v2"
9+
"gopkg.in/yaml.v2"
10+
"log"
11+
"os"
12+
"sort"
613
)
714

815
func main() {
9-
ipv4, err := iputils.GetMyPublicIpv4()
10-
if err != nil {
11-
fmt.Println("got my public ipv4 addr:", ipv4)
16+
ipv4 := iputils.GetMyPublicIpv4()
17+
ipv6 := iputils.GetMyPublicIpv6()
18+
output := models.Output{
19+
Ipv4Addr: ipv4,
20+
Ipv6Addr: ipv6,
21+
}
22+
outputJson := func(c *cli.Context) error {
23+
jsonBytes, _ := json.Marshal(output)
24+
fmt.Println(string(jsonBytes))
25+
return nil
26+
}
27+
outputYaml := func(c *cli.Context) error {
28+
yamlBytes, _ := yaml.Marshal(output)
29+
fmt.Print(string(yamlBytes))
30+
return nil
1231
}
13-
ipv6, err := iputils.GetMyPublicIpv6()
32+
app := &cli.App{
33+
Name: "getip",
34+
Usage: "Get my public ip",
35+
Commands: []*cli.Command{
36+
{
37+
Name: "json",
38+
Aliases: []string{"j"},
39+
Usage: "output formate:json",
40+
Action: outputJson,
41+
},
42+
{
43+
Name: "yaml",
44+
Aliases: []string{"y"},
45+
Usage: "output formate:yaml",
46+
Action: outputYaml,
47+
},
48+
},
49+
Action: outputJson,
50+
}
51+
52+
sort.Sort(cli.CommandsByName(app.Commands))
53+
54+
err := app.Run(os.Args)
1455
if err != nil {
15-
fmt.Println("got my public ipv6 addr:", ipv6)
56+
log.Fatal(err)
1657
}
1758
}

models/output.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package models
2+
3+
type Output struct {
4+
Ipv4Addr string
5+
Ipv6Addr string
6+
}

0 commit comments

Comments
 (0)