Skip to content

Commit

Permalink
Adds a connect-only natscontext (#5)
Browse files Browse the repository at this point in the history
This adds a version of natscontext that supports only the
parts needed to connect to nats and none of the management
features the CLI needs, this is in order to keep the usage
API and dependencies low

Signed-off-by: R.I.Pienaar <[email protected]>
  • Loading branch information
ripienaar authored Dec 6, 2024
1 parent c88e755 commit 2cd9577
Show file tree
Hide file tree
Showing 15 changed files with 724 additions and 3 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/natscontext.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: natscontext
on:
push:
paths:
- 'natscontext/**'

pull_request:
paths:
- 'natscontext/**'


jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: 'stable'

- name: Install deps
working-directory: natscontext
shell: bash --noprofile --norc -x -eo pipefail {0}
run: |
go get -t ./...
go install honnef.co/go/tools/cmd/staticcheck@latest
go install github.com/client9/misspell/cmd/misspell@latest
- name: Run linters
working-directory: natscontext
shell: bash --noprofile --norc -x -eo pipefail {0}
run: |
$(exit $(go fmt ./... | wc -l))
go vet ./...
go vet ./test/...
staticcheck ./...
staticcheck ./test/...
find . -type f -name "*.go" | xargs misspell -error -locale US
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: 'stable'

- name: Run tests
working-directory: natscontext
shell: bash --noprofile --norc -x -eo pipefail {0}
run: |
go test -v -count=1 ./...
go test -v -count=1 ./test/...
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ You can use the library as a whole, or pick just what you need.

# Utilities

| Module | Description | Docs |
|----------------------|----------------------|--------------------------------|
| Core NATS Extensions | Core NATS extensions | [README.md](natsext/README.md) |
| Module | Description | Docs |
|----------------------|----------------------------------------------|------------------------------------|
| Core NATS Extensions | Core NATS extensions | [README.md](natsext/README.md) |
| `natscontext` | Allow connecting to NATS using NATS Contexts | [README.md](natscontext/README.md) |
37 changes: 37 additions & 0 deletions natscontext/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# NATS Context Connection Helper

This is a package that helps Go developers connect to NATS using a NATS Context as featured in the `nats` Command Line
Tool.

## Installation

```bash
go get github.com/synadia-io/orbit.go/natscontext
```

## Usage

Using the `nats` command line create a Context that can connect to your server, here we use a credential in a file:

```bash
nats context add staging --creds /home/user/staging.creds --js-domain STAGING
```

We can now use the context called `staging` from Go:

```go
nc, settings, err := natscontext.Connect("staging", nats.Name("my application"))
if err != nil {
// handle error
}

// Get a JetStream handle using the domain in the context
js, err := jetstream.NewWithDomain(nc, settings.JSDomain)
if err != nil {
// handle error
}
```

If the full path to a context JSON file is given instead of the friendly name then that file will be used.

All context settings are supported except Windows Certificate Store related settings.
Loading

0 comments on commit 2cd9577

Please sign in to comment.