From dcabbeef821dbab5c143309ba962d7d63ab5671e Mon Sep 17 00:00:00 2001 From: Boris Ilijic Date: Mon, 2 Oct 2023 00:49:59 +0200 Subject: [PATCH 1/4] Add previous ctx command --- cli/context_command.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/cli/context_command.go b/cli/context_command.go index dc671ff6..bba29c74 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.switchOldCtx) } func init() { @@ -568,6 +570,19 @@ func (c *ctxCommand) removeCommand(_ *fisk.ParseContext) error { return natscontext.DeleteContext(c.name) } +func (c *ctxCommand) switchOldCtx(pc *fisk.ParseContext) error { + ctxToSwitch := natscontext.OldCtx() + 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() From f4fdf0155adf0fc8877faed57fd7af754c32ccb6 Mon Sep 17 00:00:00 2001 From: Boris Ilijic Date: Mon, 2 Oct 2023 22:03:43 +0200 Subject: [PATCH 2/4] Update jsm.go dependency to last commit in main --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) 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= From edce34283ca44df216317aa2a24e3c75876d4872 Mon Sep 17 00:00:00 2001 From: Boris Ilijic Date: Mon, 2 Oct 2023 22:07:22 +0200 Subject: [PATCH 3/4] Align naming: old -> previous --- cli/context_command.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cli/context_command.go b/cli/context_command.go index bba29c74..6f4c5bd9 100644 --- a/cli/context_command.go +++ b/cli/context_command.go @@ -87,7 +87,7 @@ func configureCtxCommand(app commandHost) { 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.switchOldCtx) + context.Command("previous", "switch to the previous context").Alias("-").Action(c.switchPreviousCtx) } func init() { @@ -570,8 +570,8 @@ func (c *ctxCommand) removeCommand(_ *fisk.ParseContext) error { return natscontext.DeleteContext(c.name) } -func (c *ctxCommand) switchOldCtx(pc *fisk.ParseContext) error { - ctxToSwitch := natscontext.OldCtx() +func (c *ctxCommand) switchPreviousCtx(pc *fisk.ParseContext) error { + ctxToSwitch := natscontext.PreviousContext() if ctxToSwitch == "" { return c.showCommand(pc) } From 07ba99723852bd69f86e847c1a2df00957ce32ec Mon Sep 17 00:00:00 2001 From: Boris Ilijic Date: Mon, 2 Oct 2023 23:10:48 +0200 Subject: [PATCH 4/4] Add docs for ctx previous command --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) 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.