Skip to content

Commit

Permalink
Merge pull request #1146 from ripienaar/req_2_11
Browse files Browse the repository at this point in the history
Require NATS Server 2.11 for upcoming features
  • Loading branch information
ripienaar authored Sep 9, 2024
2 parents d3c4865 + 4f98a6e commit f49ac82
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 36 deletions.
28 changes: 24 additions & 4 deletions cli/consumer_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func configureConsumerCommand(app commandHost) {

f.Flag("headers-only", "Deliver only headers and no bodies").IsSetByUser(&c.hdrsOnlySet).BoolVar(&c.hdrsOnly)
f.Flag("max-deliver", "Maximum amount of times a message will be delivered").PlaceHolder("TRIES").IntVar(&c.maxDeliver)
f.Flag("max-outstanding", "Maximum pending Acks before consumers are paused").Hidden().Default("-1").IntVar(&c.maxAckPending)
f.Flag("max-outstanding", "Maximum pending Acks before consumers stop delivering messages").Hidden().Default("-1").IntVar(&c.maxAckPending)
f.Flag("max-pending", "Maximum pending Acks before consumers are paused").Default("-1").IntVar(&c.maxAckPending)
if !edit {
f.Flag("max-waiting", "Maximum number of outstanding pulls allowed").PlaceHolder("PULLS").IntVar(&c.maxWaiting)
Expand Down Expand Up @@ -671,7 +671,10 @@ func (c *consumerCmd) editAction(pc *fisk.ParseContext) error {
return out
})

diff := cmp.Diff(c.selectedConsumer.Configuration(), *ncfg, sorter)
t.Metadata = iu.RemoveReservedMetadata(t.Metadata)
ncfg.Metadata = iu.RemoveReservedMetadata(ncfg.Metadata)

diff := cmp.Diff(t, *ncfg, sorter)
if diff == "" {
if !c.dryRun {
fmt.Println("No difference in configuration")
Expand Down Expand Up @@ -1279,7 +1282,7 @@ func (c *consumerCmd) loadConfigFile(file string) (*api.ConsumerConfig, error) {
return &cfg, nil
}

func (c *consumerCmd) prepareConfig(pc *fisk.ParseContext) (cfg *api.ConsumerConfig, err error) {
func (c *consumerCmd) prepareConfig() (cfg *api.ConsumerConfig, err error) {
cfg = c.defaultConsumer()
cfg.Description = c.description

Expand Down Expand Up @@ -1620,6 +1623,11 @@ func (c *consumerCmd) parsePauseUntil(until string) (time.Time, error) {
func (c *consumerCmd) resumeAction(_ *fisk.ParseContext) error {
c.connectAndSetup(true, true)

err := iu.RequireAPILevel(c.mgr, 1, "resuming consumers requires NATS Server 2.11")
if err != nil {
return err
}

state, err := c.selectedConsumer.LatestState()
if err != nil {
return err
Expand Down Expand Up @@ -1649,6 +1657,11 @@ func (c *consumerCmd) resumeAction(_ *fisk.ParseContext) error {
func (c *consumerCmd) pauseAction(_ *fisk.ParseContext) error {
c.connectAndSetup(true, true)

err := iu.RequireAPILevel(c.mgr, 1, "pausing consumers requires NATS Server 2.11")
if err != nil {
return err
}

if c.pauseUntil == "" {
dflt := time.Now().Add(time.Hour).Format(time.DateTime)
err := iu.AskOne(&survey.Input{
Expand Down Expand Up @@ -1765,7 +1778,7 @@ func (c *consumerCmd) validateCfg(cfg *api.ConsumerConfig) (bool, []byte, []stri
}

func (c *consumerCmd) createAction(pc *fisk.ParseContext) (err error) {
cfg, err := c.prepareConfig(pc)
cfg, err := c.prepareConfig()
if err != nil {
return err
}
Expand Down Expand Up @@ -1797,6 +1810,13 @@ func (c *consumerCmd) createAction(pc *fisk.ParseContext) (err error) {

c.connectAndSetup(true, false)

if !cfg.PauseUntil.IsZero() {
err := iu.RequireAPILevel(c.mgr, 1, "pausing consumers requires NATS Server 2.11")
if err != nil {
return err
}
}

created, err := c.mgr.NewConsumerFromDefault(c.stream, *cfg)
fisk.FatalIfError(err, "Consumer creation failed")

Expand Down
1 change: 1 addition & 0 deletions cli/server_info_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func (c *SrvInfoCmd) info(_ *fisk.ParseContext) error {
js := varz.JetStream
cols.AddSectionTitle("JetStream")
cols.AddRow("Domain", js.Config.Domain)
cols.AddRow("API Support Level", js.Stats.API.Level)
cols.AddRow("Storage Directory", js.Config.StoreDir)
cols.AddRow("Active Accounts", js.Stats.Accounts)
cols.AddRow("Memory In Use", humanize.IBytes(js.Stats.Memory))
Expand Down
4 changes: 3 additions & 1 deletion cli/stream_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -1856,6 +1856,8 @@ func (c *streamCmd) editAction(pc *fisk.ParseContext) error {

// lazy deep copy
input := sourceStream.Configuration()
input.Metadata = iu.RemoveReservedMetadata(input.Metadata)

ij, err := json.Marshal(input)
if err != nil {
return err
Expand All @@ -1881,7 +1883,7 @@ func (c *streamCmd) editAction(pc *fisk.ParseContext) error {
return out
})

diff := cmp.Diff(sourceStream.Configuration(), cfg, sorter)
diff := cmp.Diff(input, cfg, sorter)
if diff == "" {
if !c.dryRun {
fmt.Println("No difference in configuration")
Expand Down
14 changes: 10 additions & 4 deletions cli/trace_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ import (
"context"
"errors"
"fmt"
"os"
"strings"
"sync"
"time"

"github.com/choria-io/fisk"
"github.com/choria-io/fisk/units"
"github.com/nats-io/jsm.go"
"github.com/nats-io/jsm.go/api/server/tracing"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go"
"os"
"strings"
"sync"
"time"
iu "github.com/nats-io/natscli/internal/util"
)

type traceCmd struct {
Expand Down Expand Up @@ -82,6 +84,10 @@ func (c *traceCmd) traceAction(_ *fisk.ParseContext) error {
return err
}

if !iu.ServerMinVersion(nc, 2, 11, 0) {
return fmt.Errorf("tracing messages require NATS Server 2.11")
}

msg := nats.NewMsg(c.subject)
for k, v := range c.header {
msg.Header.Set(k, v)
Expand Down
18 changes: 9 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ require (
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/klauspost/compress v1.17.9
github.com/mattn/go-isatty v0.0.20
github.com/nats-io/jsm.go v0.1.1-0.20240830115755-5a5ef77efb68
github.com/nats-io/jsm.go v0.1.1-0.20240909112155-c79a81abfae8
github.com/nats-io/jwt/v2 v2.5.8
github.com/nats-io/nats-server/v2 v2.11.0-dev.0.20240903162803-5761fcfba516
github.com/nats-io/nats-server/v2 v2.11.0-dev.0.20240907185554-7f92c34c1415
github.com/nats-io/nats.go v1.37.0
github.com/nats-io/nkeys v0.4.7
github.com/nats-io/nuid v1.0.1
github.com/prometheus/client_golang v1.19.1
github.com/prometheus/client_golang v1.20.3
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
github.com/synadia-io/jwt-auth-builder.go v0.0.0-20240628155003-21e8d1e9d490
github.com/tylertreat/hdrhistogram-writer v0.0.0-20210816161836-2e440612a39f
golang.org/x/crypto v0.26.0
golang.org/x/crypto v0.27.0
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa
golang.org/x/term v0.23.0
golang.org/x/term v0.24.0
gopkg.in/gizak/termui.v1 v1.0.0-20151021151108-e62b5929642a
gopkg.in/yaml.v3 v3.0.1
)
Expand All @@ -58,14 +58,14 @@ require (
github.com/nats-io/nsc/v2 v2.8.6-0.20231220104935-3f89317df670 // indirect
github.com/nsf/termbox-go v1.1.1 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/common v0.59.1 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/shopspring/decimal v1.4.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/time v0.6.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
38 changes: 20 additions & 18 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
Expand All @@ -104,12 +106,12 @@ github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zx
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/nats-io/jsm.go v0.1.1-0.20240830115755-5a5ef77efb68 h1:gz6zQrxxP0myID+NPgVVSwg0eLHJuF5hXhpFDZkp06E=
github.com/nats-io/jsm.go v0.1.1-0.20240830115755-5a5ef77efb68/go.mod h1:GqhrbjF7id/JW5arS8TZUbHwWTsxL98ubUAloC2f6QY=
github.com/nats-io/jsm.go v0.1.1-0.20240909112155-c79a81abfae8 h1:n7c7Sjo8bfYTsQhRuEa5uFsKl8mnw+yaWNQ1EVP4/7o=
github.com/nats-io/jsm.go v0.1.1-0.20240909112155-c79a81abfae8/go.mod h1:qarKt1X8221zgCOg+JcjkH1/i7+p3HQFRWNYv1lk3dI=
github.com/nats-io/jwt/v2 v2.5.8 h1:uvdSzwWiEGWGXf+0Q+70qv6AQdvcvxrv9hPM0RiPamE=
github.com/nats-io/jwt/v2 v2.5.8/go.mod h1:ZdWS1nZa6WMZfFwwgpEaqBV8EPGVgOTDHN/wTbz0Y5A=
github.com/nats-io/nats-server/v2 v2.11.0-dev.0.20240903162803-5761fcfba516 h1:e3mtFXm0ut18cyBEsL5BcuriFfAvKZvVi3dYHEhyfxY=
github.com/nats-io/nats-server/v2 v2.11.0-dev.0.20240903162803-5761fcfba516/go.mod h1:odNGaRFETFJYsviPk2grICKGgoObmMjgIBDwny/uVrc=
github.com/nats-io/nats-server/v2 v2.11.0-dev.0.20240907185554-7f92c34c1415 h1:gGXxOszQols/MkhpSc3Ec2RfP4T+aW0EBZkOinsHJ9M=
github.com/nats-io/nats-server/v2 v2.11.0-dev.0.20240907185554-7f92c34c1415/go.mod h1:odNGaRFETFJYsviPk2grICKGgoObmMjgIBDwny/uVrc=
github.com/nats-io/nats.go v1.37.0 h1:07rauXbVnnJvv1gfIyghFEo6lUcYRY0WXc3x7x0vUxE=
github.com/nats-io/nats.go v1.37.0/go.mod h1:Ubdu4Nh9exXdSz0RVWRFBbRfrbSxOYd26oF0wkWclB8=
github.com/nats-io/nkeys v0.4.7 h1:RwNJbbIdYCoClSDNY7QVKZlyb/wfT6ugvFCiKy6vDvI=
Expand All @@ -127,12 +129,12 @@ github.com/onsi/gomega v1.33.1 h1:dsYjIxxSR755MDmKVsaFQTE22ChNBcuuTWgkUDSubOk=
github.com/onsi/gomega v1.33.1/go.mod h1:U4R44UsT+9eLIaYRB2a5qajjtQYn0hauxvRm16AVYg0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v1.19.1 h1:wZWJDwK+NameRJuPGDhlnFgx8e8HN3XHQeLaYJFJBOE=
github.com/prometheus/client_golang v1.19.1/go.mod h1:mP78NwGzrVks5S2H6ab8+ZZGJLZUq1hoULYBAYBw1Ho=
github.com/prometheus/client_golang v1.20.3 h1:oPksm4K8B+Vt35tUhw6GbSNSgVlVSBH0qELP/7u83l4=
github.com/prometheus/client_golang v1.20.3/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
github.com/prometheus/common v0.55.0 h1:KEi6DK7lXW/m7Ig5i47x0vRzuBsHuvJdi5ee6Y3G1dc=
github.com/prometheus/common v0.55.0/go.mod h1:2SECS4xJG1kd8XF9IcM1gMX6510RAEL65zxzNImwdc8=
github.com/prometheus/common v0.59.1 h1:LXb1quJHWm1P6wq/U824uxYi4Sg0oGvNeUm1z5dJoX0=
github.com/prometheus/common v0.59.1/go.mod h1:GpWM7dewqmVYcd7SmRaiWVe9SSqjf0UrwnYnpEZNuT0=
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
Expand All @@ -159,8 +161,8 @@ github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5t
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20180807140117-3d87b88a115f/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190125153040-c74c464bbbf2/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand All @@ -178,8 +180,8 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand All @@ -193,18 +195,18 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.23.0 h1:F6D4vR+EHoL9/sWAWgAR1H2DcHr4PareCbAaCo1RpuU=
golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk=
golang.org/x/term v0.24.0 h1:Mh5cbb+Zk2hqqXNO7S1iTjEphVL+jb8ZWaqh/g+JWkM=
golang.org/x/term v0.24.0/go.mod h1:lOBK/LVxemqiMij05LGJ0tzNr8xlmwBRJ81PX6wVLH8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U=
golang.org/x/time v0.6.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.0.0-20180525024113-a5b4c53f6e8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
69 changes: 69 additions & 0 deletions internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,89 @@ package util
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"os"
"os/user"
"path/filepath"
"reflect"
"regexp"
"strconv"
"strings"

"github.com/AlecAivazis/survey/v2"
"github.com/nats-io/jsm.go"
"github.com/nats-io/nats.go"
"github.com/nats-io/natscli/options"

"github.com/nats-io/nkeys"
terminal "golang.org/x/term"
)

// RemoveReservedMetadata returns a new version of metadata with reserved keys starting with _nats. removed
func RemoveReservedMetadata(metadata map[string]string) map[string]string {
res := make(map[string]string)

for k, v := range metadata {
if strings.HasPrefix(k, "_nats.") {
continue
}
res[k] = v
}

if len(res) == 0 {
return nil
}

return res
}

// RequireAPILevel asserts if the meta leader supports api level lvl
func RequireAPILevel(m *jsm.Manager, lvl int, format string, a ...any) error {
mlvl, err := m.MetaApiLevel(false)
if err != nil {
return err
}

if mlvl >= lvl {
return nil
}

return fmt.Errorf(format, a...)
}

var semVerRe = regexp.MustCompile(`\Av?([0-9]+)\.?([0-9]+)?\.?([0-9]+)?`)

func versionComponents(version string) (major, minor, patch int, err error) {
m := semVerRe.FindStringSubmatch(version)
if m == nil {
return 0, 0, 0, errors.New("invalid semver")
}
major, err = strconv.Atoi(m[1])
if err != nil {
return -1, -1, -1, err
}
minor, err = strconv.Atoi(m[2])
if err != nil {
return -1, -1, -1, err
}
patch, err = strconv.Atoi(m[3])
if err != nil {
return -1, -1, -1, err
}
return major, minor, patch, err
}

// ServerMinVersion checks if the connected server meets certain version constraints
func ServerMinVersion(nc *nats.Conn, major, minor, patch int) bool {
smajor, sminor, spatch, _ := versionComponents(nc.ConnectedServerVersion())
if smajor < major || (smajor == major && sminor < minor) || (smajor == major && sminor == minor && spatch < patch) {
return false
}

return true
}

// XdgShareHome is where to store data like nsc stored
func XdgShareHome() (string, error) {
parent := os.Getenv("XDG_DATA_HOME")
Expand Down

0 comments on commit f49ac82

Please sign in to comment.