Skip to content

Commit

Permalink
Merge pull request #889 from boris-ilijic/old-ctx
Browse files Browse the repository at this point in the history
Feature: Context's command for switching to previous (old) nats context
  • Loading branch information
ripienaar authored Oct 3, 2023
2 parents 794f821 + 07ba997 commit 59cff75
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 15 additions & 0 deletions cli/context_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 59cff75

Please sign in to comment.