-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanybar.go
77 lines (60 loc) · 1.41 KB
/
anybar.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"fmt"
"github.com/johntdyer/anybar-go/Godeps/_workspace/src/github.com/codegangsta/cli"
"github.com/johntdyer/anybar-go/Godeps/_workspace/src/github.com/wsxiaoys/terminal"
"log"
"net"
"os"
)
func main() {
app := cli.NewApp()
app.Version = "0.0.1"
app.Name = "anybar-go"
app.Usage = "Anybar CLI"
app.Author = "John Dyer"
app.Email = "johntdyer@gmail.com"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "port, p",
Value: "1738",
Usage: "Port to connect to anybar",
EnvVar: "ANYBAR_PORT",
},
cli.StringFlag{Name: "address, a",
Value: "localhost",
Usage: "Address to send message.",
},
}
app.Action = func(c *cli.Context) {
msg := ""
if len(c.Args()) > 0 {
msg = c.Args()[0]
addr := fmt.Sprintf("%s:%s", c.String("address"), c.String("port"))
sendPacket(msg, addr)
} else {
terminal.Stdout.Color("r").Print("Message is required").Nl().Reset()
}
}
app.Run(os.Args)
}
func sendPacket(msg string, addr string) {
laddr, err := net.ResolveUDPAddr("udp", ":0")
if err != nil {
log.Fatal(err)
}
maddr, err := net.ResolveUDPAddr("udp4", addr)
if err != nil {
log.Fatal(err)
}
c, err := net.ListenUDP("udp4", laddr)
if err != nil {
log.Fatal(err)
}
defer c.Close()
_, err = c.WriteToUDP([]byte(msg), maddr)
if err != nil {
log.Fatal(err)
}
terminal.Stdout.Color("b").Print(fmt.Sprintf("Sent '%s' successfully", msg)).Nl().Reset()
}