forked from kenshaw/hilink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
55 lines (51 loc) · 1.08 KB
/
main.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
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"github.com/kenshaw/hilink"
)
func main() {
endpoint := flag.String("endpoint", "http://192.168.8.1/", "api endpoint")
debug := flag.Bool("v", false, "enable verbose")
flag.Parse()
if err := run(context.Background(), *endpoint, *debug); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
func run(ctx context.Context, endpoint string, debug bool) error {
// options
opts := []hilink.ClientOption{
hilink.WithURL(endpoint),
}
if debug {
opts = append(opts, hilink.WithLogf(log.Printf))
}
// create client
cl := hilink.NewClient(opts...)
// retrieve session id
sessID, tokID, err := cl.NewSessionAndTokenID(ctx)
if err != nil {
return err
}
// set session id
if err := cl.SetSessionAndTokenID(sessID, tokID); err != nil {
return err
}
// get device info
d, err := cl.DeviceInfo(ctx)
if err != nil {
return err
}
// change to json
buf, err := json.MarshalIndent(d, "", " ")
if err != nil {
return err
}
_, err = os.Stdout.Write(append(buf, '\n'))
return err
}