forked from nextdns/nextdns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctl.go
38 lines (35 loc) · 690 Bytes
/
ctl.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
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/nextdns/nextdns/config"
"github.com/nextdns/nextdns/ctl"
)
func ctlCmd(args []string) error {
cmd := args[0]
fs := flag.NewFlagSet(cmd, flag.ExitOnError)
control := fs.String("control", config.DefaultControl, "Address to the control socket")
_ = fs.Parse(args[1:])
cl, err := ctl.Dial(*control)
if err != nil {
return err
}
defer cl.Close()
data, err := cl.Send(ctl.Event{
Name: cmd,
})
if err != nil {
return err
}
if s, ok := data.(string); ok {
fmt.Println(s)
return nil
}
b, err := json.MarshalIndent(data, "", " ")
if err != nil {
return err
}
fmt.Println(string(b))
return nil
}