/
gateway.go
60 lines (48 loc) · 1.25 KB
/
gateway.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"time"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var gatewayCmd = &cobra.Command{
Use: "gateway",
Short: "Display information about local T-Mobile gateway",
RunE: func(cmd *cobra.Command, args []string) error {
// all is used because we need time in addition to device
req, err := http.NewRequest("GET", "http://192.168.12.1/TMI/v1/gateway?get=all", nil)
if err != nil {
return err
}
req.Header.Add("Authorization", "Bearer "+viper.GetString("password"))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
var respJSON gatewayResp
json.Unmarshal(body, &respJSON)
// prepare
dur, err := time.ParseDuration(strconv.Itoa(respJSON.Time.Uptime) + "s")
if err != nil {
return err
}
fmt.Println("Gateway data:")
fmt.Printf("Name: %s\n", respJSON.Device.FriendlyName)
fmt.Printf("Device: %s %s\n", respJSON.Device.Manufacturer, respJSON.Device.Model)
fmt.Printf("Serial No.: %s\n", respJSON.Device.Serial)
fmt.Printf("Uptime: %s\n", dur)
return nil
},
}
func init() {
rootCmd.AddCommand(gatewayCmd)
}