-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
72 lines (63 loc) · 1.96 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"crypto/tls"
"fmt"
"github.com/nats-io/go-nats"
"github.com/spf13/cobra"
"os"
)
func compileNatsOptions() []nats.Option {
var natsOptions = make([]nats.Option, 0)
if !DisableTls {
cert, err := tls.LoadX509KeyPair(NatsCert, NatsKey)
if err != nil {
panic(err)
}
config := &tls.Config{
// since NATS backbone should always be on a private line with self-signed certs, we just skip host verification
InsecureSkipVerify: true,
Certificates: []tls.Certificate{cert},
MinVersion: tls.VersionTLS12,
}
natsOptions = append(natsOptions, nats.Secure(config))
}
return natsOptions
}
var RootCmd = &cobra.Command{
Use: "nats-client",
Short: "Thin client for NATS server/cluster basic interaction",
Long: "Thin client for NATS server/cluster basic interaction",
Run: func(cmd *cobra.Command, args []string) {
if err := cmd.Help(); err != nil {
panic(err)
}
},
}
var NatsUri string
var NatsCert string
var NatsKey string
var DisableTls bool
func main() {
var testCmd = &cobra.Command{
Use: "test",
Short: "Tests the connectivity to a NATS server/cluster",
Long: "Tests the connectivity to a NATS server/cluster",
Run: func(cmd *cobra.Command, args []string) {
var natsOptions = compileNatsOptions()
natsConn, err := nats.Connect(NatsUri, natsOptions...)
if err != nil {
panic(err)
}
defer natsConn.Close()
},
}
testCmd.Flags().StringVarP(&NatsUri, "nats", "n", "nats://nats:4222", "The nats cluster URI")
testCmd.Flags().StringVarP(&NatsCert, "natsCert", "", "/etc/ssl/certs/uniform-nats.crt", "The nats cluster TLS certificate file path")
testCmd.Flags().StringVarP(&NatsKey, "natsKey", "", "/etc/ssl/private/uniform-nats.key", "The nats cluster TLS key file path")
testCmd.Flags().BoolVar(&DisableTls, "disableTls", false, "A flag indicating if service should disable tls encryption")
RootCmd.AddCommand(testCmd)
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}