MCPcopy
hub / github.com/nats-io/nats.go / main

Function main

examples/nats-sub/main.go:43–122  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

41}
42
43func main() {
44 var urls = flag.String("s", nats.DefaultURL, "The nats server URLs (separated by comma)")
45 var userCreds = flag.String("creds", "", "User Credentials File")
46 var nkeyFile = flag.String("nkey", "", "NKey Seed File")
47 var tlsClientCert = flag.String("tlscert", "", "TLS client certificate file")
48 var tlsClientKey = flag.String("tlskey", "", "Private key file for client certificate")
49 var tlsCACert = flag.String("tlscacert", "", "CA certificate to verify peer against")
50 var showTime = flag.Bool("t", false, "Display timestamps")
51 var showHelp = flag.Bool("h", false, "Show help message")
52
53 log.SetFlags(0)
54 flag.Usage = usage
55 flag.Parse()
56
57 if *showHelp {
58 showUsageAndExit(0)
59 }
60
61 args := flag.Args()
62 if len(args) != 1 {
63 showUsageAndExit(1)
64 }
65
66 // Connect Options.
67 opts := []nats.Option{nats.Name("NATS Sample Subscriber")}
68 opts = setupConnOptions(opts)
69
70 if *userCreds != "" && *nkeyFile != "" {
71 log.Fatal("specify -seed or -creds")
72 }
73
74 // Use UserCredentials
75 if *userCreds != "" {
76 opts = append(opts, nats.UserCredentials(*userCreds))
77 }
78
79 // Use TLS client authentication
80 if *tlsClientCert != "" && *tlsClientKey != "" {
81 opts = append(opts, nats.ClientCert(*tlsClientCert, *tlsClientKey))
82 }
83
84 // Use specific CA certificate
85 if *tlsCACert != "" {
86 opts = append(opts, nats.RootCAs(*tlsCACert))
87 }
88
89 // Use Nkey authentication.
90 if *nkeyFile != "" {
91 opt, err := nats.NkeyOptionFromSeed(*nkeyFile)
92 if err != nil {
93 log.Fatal(err)
94 }
95 opts = append(opts, opt)
96 }
97
98 // Connect to NATS
99 nc, err := nats.Connect(*urls, opts...)
100 if err != nil {

Callers

nothing calls this directly

Calls 9

ConnectMethod · 0.80
showUsageAndExitFunction · 0.70
setupConnOptionsFunction · 0.70
printMsgFunction · 0.70
NameMethod · 0.65
SubscribeMethod · 0.65
StringMethod · 0.45
FlushMethod · 0.45
LastErrorMethod · 0.45

Tested by

no test coverage detected