Skip to content

Commit

Permalink
feat: add unset context command (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
nideshchitrakar authored Sep 29, 2023
1 parent d22fdfb commit f3b1baf
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/cmd/context/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func New() *cobra.Command {
cmdList,
cmdRemove,
cmdSet,
cmdUnset,
)

return cmd
Expand Down
7 changes: 7 additions & 0 deletions pkg/cmd/context/testdata/unset.extra-args.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Error: accepts 1 arg(s), received 2
Usage:
unset CONTEXT_NAME [flags]

Flags:
-h, --help help for unset

1 change: 1 addition & 0 deletions pkg/cmd/context/testdata/unset.nonexisting.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Error: cannot unset 'foo' from current context: no such context
48 changes: 48 additions & 0 deletions pkg/cmd/context/unset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Synse CLI
// Copyright (c) 2023 Vapor IO
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package context

import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/vapor-ware/synse-cli/pkg/config"
"github.com/vapor-ware/synse-cli/pkg/utils"
"github.com/vapor-ware/synse-cli/pkg/utils/exit"
)

var cmdUnset = &cobra.Command{
Use: "unset CONTEXT_NAME",
Short: "Unset the current context",
Long: utils.Doc(`
Unset the current active context for the CLI.
`),
SuggestFor: []string{
"change",
},
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
exit.FromCmd(cmd).Err(unsetContext(args[0]))
},
}

func unsetContext(name string) error {
log.WithFields(log.Fields{
"name": name,
}).Debug("unsetting current context")

return config.UnsetCurrentContext(name)
}
100 changes: 100 additions & 0 deletions pkg/cmd/context/unset_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Synse CLI
// Copyright (c) 2023 Vapor IO
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package context

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/vapor-ware/synse-cli/internal/test"
"github.com/vapor-ware/synse-cli/pkg/config"
)

func TestCmdUnset_extraArgs(t *testing.T) {
defer func() {
config.Purge()
resetFlags()
}()

result := test.Cmd(cmdUnset).Args(
"foo",
"bar",
).Run(t)
result.AssertErr()
result.AssertGolden("unset.extra-args.golden")

assert.Len(t, config.GetContexts(), 0)
assert.Len(t, config.GetCurrentContext(), 0)
}

func TestCmdUnset_existingCtx(t *testing.T) {
defer func() {
config.Purge()
resetFlags()
}()

assert.NoError(t, config.AddContext(&config.ContextRecord{
Name: "test-ctx",
Type: "server",
Context: config.Context{
Address: "0.0.0.0",
ClientCert: "/tmp/test/dir",
},
}))
assert.NoError(t, config.SetCurrentContext("test-ctx"))

assert.Len(t, config.GetContexts(), 1)
assert.Len(t, config.GetCurrentContext(), 1)

result := test.Cmd(cmdUnset).Args(
"test-ctx",
).Run(t)
result.AssertNoErr()
result.AssertGolden("empty.golden")

assert.Len(t, config.GetContexts(), 1)
assert.Len(t, config.GetCurrentContext(), 0)
}

func TestCmdUnset_nonexistingCtx(t *testing.T) {
defer func() {
config.Purge()
resetFlags()
}()

assert.NoError(t, config.AddContext(&config.ContextRecord{
Name: "test-ctx",
Type: "server",
Context: config.Context{
Address: "0.0.0.0",
ClientCert: "/tmp/test/dir",
},
}))

assert.Len(t, config.GetContexts(), 1)
assert.Len(t, config.GetCurrentContext(), 0)

result := test.Cmd(cmdUnset).Args(
"foo",
).Run(t)
result.AssertNoErr()
result.AssertExited()
result.AssertGolden("unset.nonexisting.golden")

assert.Len(t, config.GetContexts(), 1)
assert.Len(t, config.GetCurrentContext(), 0)
}
28 changes: 28 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"os"
"path/filepath"

"github.com/pkg/errors"

Check failure on line 24 in pkg/config/config.go

View workflow job for this annotation

GitHub Actions / snapshot-build

other declaration of errors

Check failure on line 24 in pkg/config/config.go

View workflow job for this annotation

GitHub Actions / lint

other declaration of errors

Check failure on line 24 in pkg/config/config.go

View workflow job for this annotation

GitHub Actions / unit-test

other declaration of errors

"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"

Check failure on line 27 in pkg/config/config.go

View workflow job for this annotation

GitHub Actions / snapshot-build

errors redeclared in this block

Check failure on line 27 in pkg/config/config.go

View workflow job for this annotation

GitHub Actions / snapshot-build

"github.com/pkg/errors" imported and not used

Check failure on line 27 in pkg/config/config.go

View workflow job for this annotation

GitHub Actions / lint

errors redeclared in this block

Check failure on line 27 in pkg/config/config.go

View workflow job for this annotation

GitHub Actions / lint

"github.com/pkg/errors" imported and not used

Check failure on line 27 in pkg/config/config.go

View workflow job for this annotation

GitHub Actions / unit-test

errors redeclared in this block

Check failure on line 27 in pkg/config/config.go

View workflow job for this annotation

GitHub Actions / unit-test

"github.com/pkg/errors" imported and not used
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -251,6 +253,32 @@ func SetCurrentContext(name string) error {
return config.SetCurrentContext(name)
}

// UnsetCurrentContext unsets the named context from the current active context. If
// the given name does not correspond to a ContextRecord, an error is returned.
func (c *Config) UnsetCurrentContext(name string) error {
var context *ContextRecord
for _, ctx := range c.Contexts {
if ctx.Name == name {
context = &ctx
break
}
}
if context == nil {
return fmt.Errorf("cannot unset '%s' from current context: no such context", name)
}

if c.CurrentContext[context.Type] == context.Name {
delete(c.CurrentContext, context.Type)
}
log.WithField("context", name).Debug("unset current context")
return nil
}

// UnsetCurrentContext unsets the current context from the default configuration.
func UnsetCurrentContext(name string) error {
return config.UnsetCurrentContext(name)
}

// GetCurrentContext gets the ContextRecords for the current context, if set.
func (c *Config) GetCurrentContext() map[string]*ContextRecord {
var current = make(map[string]*ContextRecord)
Expand Down

0 comments on commit f3b1baf

Please sign in to comment.