initial commit
This commit is contained in:
commit
7837683ce3
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
.DS_Store
|
||||||
|
.idea/
|
||||||
|
*.iml
|
||||||
|
/nats-pub/nats-pub
|
||||||
|
/nats-sub/nats-sub
|
16
README.md
Normal file
16
README.md
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
# NATS CLI Client
|
||||||
|
|
||||||
|
Just a really, really simple NATS CLI client. This is primarily intended for my own personal use. I needed a client
|
||||||
|
that I could use for simple tasks here and there. I didn't see an existing one (with a minimum of dependencies) that
|
||||||
|
supported TLS connections to the NATS server, which I need.
|
||||||
|
|
||||||
|
Includes:
|
||||||
|
|
||||||
|
```
|
||||||
|
nats-pub [-s server] [-tls] [-tlscert CERT_FILE] [-tlskey KEY_FILE] [-tlscacert CA_FILE] <subject> <message>
|
||||||
|
|
||||||
|
nats-sub [-s server] [-ts] [-tls] [-tlscert CERT_FILE] [-tlskey KEY_FILE] [-tlscacert CA_FILE] <subject>
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
I will add more support for extra features, etc. later if/when I need them.
|
50
nats-pub/app.go
Normal file
50
nats-pub/app.go
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
nc "github.com/gered/nats-cli"
|
||||||
|
"github.com/nats-io/nats"
|
||||||
|
)
|
||||||
|
|
||||||
|
func usage() {
|
||||||
|
log.Fatalf("nats-pub [-s server] [-tls] [-tlscert CERT_FILE] [-tlskey KEY_FILE] [-tlscacert CA_FILE] <subject> <message>")
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
log.SetFlags(0)
|
||||||
|
|
||||||
|
var url = flag.String("s", nats.DefaultURL, "NATS comma-separate server URL list")
|
||||||
|
var tls = flag.Bool("tls", false, "Enable TLS")
|
||||||
|
var tlsCertPath = flag.String("tlscert", "", "Certificate file")
|
||||||
|
var tlsKeyPath = flag.String("tlskey", "", "Private key file for certificate")
|
||||||
|
var tlsCACertPath = flag.String("tlscacert", "", "Client certificate CA file")
|
||||||
|
|
||||||
|
flag.Usage = usage
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
if flag.NArg() < 2 {
|
||||||
|
usage()
|
||||||
|
}
|
||||||
|
|
||||||
|
var subject = flag.Arg(0)
|
||||||
|
var message = flag.Arg(1)
|
||||||
|
|
||||||
|
conn, err := nc.Connect(*url, *tls, *tlsCertPath, *tlsKeyPath, *tlsCACertPath)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to connect to NATS: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
conn.Publish(subject, []byte(message))
|
||||||
|
conn.Flush()
|
||||||
|
|
||||||
|
err = conn.LastError()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Published message on subject %s\n", subject)
|
||||||
|
|
||||||
|
conn.Close()
|
||||||
|
}
|
59
nats-sub/app.go
Normal file
59
nats-sub/app.go
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"runtime"
|
||||||
|
|
||||||
|
nc "github.com/gered/nats-cli"
|
||||||
|
"github.com/nats-io/nats"
|
||||||
|
)
|
||||||
|
|
||||||
|
func usage() {
|
||||||
|
log.Fatalf("nats-sub [-s server] [-ts] [-tls] [-tlscert CERT_FILE] [-tlskey KEY_FILE] [-tlscacert CA_FILE] <subject>")
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
log.SetFlags(0)
|
||||||
|
|
||||||
|
var url = flag.String("s", nats.DefaultURL, "NATS comma-separate server URL list")
|
||||||
|
var ts = flag.Bool("ts", false, "Display timestamp on logging output")
|
||||||
|
var tls = flag.Bool("tls", false, "Enable TLS")
|
||||||
|
var tlsCertPath = flag.String("tlscert", "", "Certificate file")
|
||||||
|
var tlsKeyPath = flag.String("tlskey", "", "Private key file for certificate")
|
||||||
|
var tlsCACertPath = flag.String("tlscacert", "", "Client certificate CA file")
|
||||||
|
|
||||||
|
flag.Usage = usage
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
if flag.NArg() < 1 {
|
||||||
|
usage()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var subject = flag.Arg(0)
|
||||||
|
|
||||||
|
if *ts {
|
||||||
|
log.SetFlags(log.LstdFlags)
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, err := nc.Connect(*url, *tls, *tlsCertPath, *tlsKeyPath, *tlsCACertPath)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to connect to NATS: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
conn.Subscribe(subject, func(msg *nats.Msg) {
|
||||||
|
log.Printf("[%s]: %s\n", msg.Subject, string(msg.Data))
|
||||||
|
})
|
||||||
|
conn.Flush()
|
||||||
|
|
||||||
|
err = conn.LastError()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Listening on subject: %s\n", subject)
|
||||||
|
|
||||||
|
runtime.Goexit()
|
||||||
|
}
|
42
nats.go
Normal file
42
nats.go
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
package nats_cli
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"github.com/nats-io/nats"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Connect(url string, tls bool, certPath string, keyPath string, caCertPath string) (*nats.Conn, error) {
|
||||||
|
if tls {
|
||||||
|
if len(certPath) == 0 {
|
||||||
|
return nil, errors.New("tlscert not set")
|
||||||
|
}
|
||||||
|
if len(keyPath) == 0 {
|
||||||
|
return nil, errors.New("tlskey not set")
|
||||||
|
}
|
||||||
|
|
||||||
|
cert := nats.ClientCert(certPath, keyPath)
|
||||||
|
var conn *nats.Conn
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if len(caCertPath) > 0 {
|
||||||
|
conn, err = nats.Connect(url, nats.RootCAs(caCertPath), cert)
|
||||||
|
} else {
|
||||||
|
conn, err = nats.Connect(url, cert)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return conn, nil
|
||||||
|
|
||||||
|
} else {
|
||||||
|
conn, err := nats.Connect(url)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return conn, nil
|
||||||
|
}
|
||||||
|
}
|
Reference in a new issue