-
Notifications
You must be signed in to change notification settings - Fork 0
/
util-ip.go
46 lines (35 loc) · 870 Bytes
/
util-ip.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package util_c
import (
_ "embed"
"io"
"net/http"
"time"
"github.com/d3code/clog"
"github.com/d3code/x/internal/embed_text"
"github.com/d3code/pkg/xerr"
"github.com/spf13/cobra"
)
func init() {
Util.AddCommand(ipCommand)
}
var ipCommand = &cobra.Command{
Use: "ip",
Short: "Public IP address",
Long: embed_text.UtilIp,
Run: func(cmd *cobra.Command, args []string) {
ip := GetIp()
clog.Info(ip)
},
}
func GetIp() string {
httpRequest, err := http.NewRequest("GET", "https://ipecho.net/plain", nil)
xerr.ExitIfError(err)
httpClient := http.Client{
Timeout: 10 * time.Second,
}
response, err := httpClient.Do(httpRequest)
xerr.ExitIfError(err)
responseBody, err := io.ReadAll(response.Body)
xerr.ExitIfError(err)
return string(responseBody)
}