diff --git a/README.md b/README.md index 415c085d..b5b57751 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,15 @@ Known contexts: The context is selected as default, use `nats context --help` to see how to add, remove and edit contexts. +To switch to another context we can use: +``` +nats ctx select localhost +``` +To switch context back to previous one, we can use `context previous` subcommand: +``` +nats ctx -- - +``` + ### Configuration file nats-cli stores contextes in `~/.config/nats/context`. Those contextes are stored as JSON documents. You can find the description and expected value for this configuration file by running `nats --help` and look for the global flags. diff --git a/cli/context_command.go b/cli/context_command.go index dc671ff6..6f4c5bd9 100644 --- a/cli/context_command.go +++ b/cli/context_command.go @@ -86,6 +86,8 @@ func configureCtxCommand(app commandHost) { validate := context.Command("validate", "Validate one or all contexts").Action(c.validateCommand) validate.Arg("name", "Validate a specific context, validates all when not supplied").StringVar(&c.name) validate.Flag("connect", "Attempts to connect to NATS using the context while validating").UnNegatableBoolVar(&c.activate) + + context.Command("previous", "switch to the previous context").Alias("-").Action(c.switchPreviousCtx) } func init() { @@ -568,6 +570,19 @@ func (c *ctxCommand) removeCommand(_ *fisk.ParseContext) error { return natscontext.DeleteContext(c.name) } +func (c *ctxCommand) switchPreviousCtx(pc *fisk.ParseContext) error { + ctxToSwitch := natscontext.PreviousContext() + if ctxToSwitch == "" { + return c.showCommand(pc) + } + + if err := natscontext.SelectContext(ctxToSwitch); err != nil { + return err + } + + return c.showCommand(pc) +} + func (c *ctxCommand) selectCommand(pc *fisk.ParseContext) error { known := natscontext.KnownContexts() diff --git a/go.mod b/go.mod index f3ddffc0..fb932a40 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,7 @@ require ( github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 github.com/klauspost/compress v1.17.0 github.com/mattn/go-isatty v0.0.19 - github.com/nats-io/jsm.go v0.1.1-0.20230929093219-c157aaec4932 + github.com/nats-io/jsm.go v0.1.1-0.20231002155422-ec2ddc62d18e github.com/nats-io/jwt/v2 v2.5.2 github.com/nats-io/nats-server/v2 v2.10.1 github.com/nats-io/nats.go v1.30.0 diff --git a/go.sum b/go.sum index ab820700..40c76f0e 100644 --- a/go.sum +++ b/go.sum @@ -80,8 +80,8 @@ github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d h1:5PJl274Y63IEHC+7izoQ github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= github.com/minio/highwayhash v1.0.2 h1:Aak5U0nElisjDCfPSG79Tgzkn2gl66NxOMspRrKnA/g= github.com/minio/highwayhash v1.0.2/go.mod h1:BQskDq+xkJ12lmlUUi7U0M5Swg3EWR+dLTk+kldvVxY= -github.com/nats-io/jsm.go v0.1.1-0.20230929093219-c157aaec4932 h1:qZy9YlK9iw6we9cOhwz9KcSXozhuVPkNKlK1ZOA0Jwo= -github.com/nats-io/jsm.go v0.1.1-0.20230929093219-c157aaec4932/go.mod h1:hB4Qd+IKoRvAAWTOI1HkCy4wotjFwOIT+codHCFOZqk= +github.com/nats-io/jsm.go v0.1.1-0.20231002155422-ec2ddc62d18e h1:sSdswSrXLmxZ2gT37lmBizmqGw3QiHHi5+0zjJ8MIYc= +github.com/nats-io/jsm.go v0.1.1-0.20231002155422-ec2ddc62d18e/go.mod h1:hB4Qd+IKoRvAAWTOI1HkCy4wotjFwOIT+codHCFOZqk= github.com/nats-io/jwt/v2 v2.5.2 h1:DhGH+nKt+wIkDxM6qnVSKjokq5t59AZV5HRcFW0zJwU= github.com/nats-io/jwt/v2 v2.5.2/go.mod h1:24BeQtRwxRV8ruvC4CojXlx/WQ/VjuwlYiH+vu/+ibI= github.com/nats-io/nats-server/v2 v2.10.1 h1:MIJ614dhOIdo71iSzY8ln78miXwrYvlvXHUyS+XdKZQ=