-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add unset context command (#244)
- Loading branch information
1 parent
d22fdfb
commit f3b1baf
Showing
6 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,6 +59,7 @@ func New() *cobra.Command { | |
cmdList, | ||
cmdRemove, | ||
cmdSet, | ||
cmdUnset, | ||
) | ||
|
||
return cmd | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Error: cannot unset 'foo' from current context: no such context |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters