-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
58 lines (54 loc) · 1.06 KB
/
main.go
File metadata and controls
58 lines (54 loc) · 1.06 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"encoding/json"
"fmt"
"github.com/OpenIoTHub/getip/v2/iputils"
"github.com/OpenIoTHub/getip/v2/models"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v2"
"log"
"os"
"sort"
)
func main() {
ipv4 := iputils.GetMyPublicIpv4()
ipv6 := iputils.GetMyPublicIpv6()
output := models.Output{
Ipv4Addr: ipv4,
Ipv6Addr: ipv6,
}
outputJson := func(c *cli.Context) error {
jsonBytes, _ := json.Marshal(output)
fmt.Println(string(jsonBytes))
return nil
}
outputYaml := func(c *cli.Context) error {
yamlBytes, _ := yaml.Marshal(output)
fmt.Print(string(yamlBytes))
return nil
}
app := &cli.App{
Name: "getip",
Usage: "Get my public ip",
Commands: []*cli.Command{
{
Name: "json",
Aliases: []string{"j"},
Usage: "output formate:json",
Action: outputJson,
},
{
Name: "yaml",
Aliases: []string{"y"},
Usage: "output formate:yaml",
Action: outputYaml,
},
},
Action: outputJson,
}
sort.Sort(cli.CommandsByName(app.Commands))
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}