Repository: nats-io/nsc
Pull request #540 (2023-01-25 15:37:25)

Add `select` Command
This PR adds the `select` verb to the `nsc` command. The goal was to more explicitly expose the functionality to switch operators and accounts and to make it as easy as possible.


Patch:
diff --git a/cmd/select.go b/cmd/select.go
new file mode 100644
index 0000000..2df75b0
--- /dev/null
+++ b/cmd/select.go
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2018-2022 The NATS Authors
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package cmd
+
+import (
+	"github.com/spf13/cobra"
+)
+
+func init() {
+	GetRootCmd().AddCommand(createSelecteCmd())
+}
+
+func createSelecteCmd() *cobra.Command {
+	cmd := &cobra.Command{
+		Use:   "select",
+		Short: "Set the current operator or account",
+	}
+	cmd.AddCommand(selectOperatorCmd())
+	cmd.AddCommand(selectAccountCmd())
+	return cmd
+}
+
+func selectOperatorCmd() *cobra.Command {
+	return &cobra.Command{
+		Use:   "operator",
+		Short: "set the operator",
+		Args: func(cmd *cobra.Command, args []string) error {
+			if err := cobra.ExactArgs(1)(cmd, args); err != nil {
+				return err
+			}
+
+			return nil
+		},
+		RunE: func(cmd *cobra.Command, args []string) error {
+			current := GetConfig()
+			if err := current.ContextConfig.Update("", args[0], ""); err != nil {
+				return err
+			}
+
+			return current.Save()
+		},
+	}
+}
+
+func selectAccountCmd() *cobra.Command {
+	return &cobra.Command{
+		Use:   "account",
+		Short: "set the account",
+		Args: func(cmd *cobra.Command, args []string) error {
+			if err := cobra.ExactArgs(1)(cmd, args); err != nil {
+				return err
+			}
+
+			return nil
+		},
+		RunE: func(cmd *cobra.Command, args []string) error {
+			current := GetConfig()
+			if err := current.ContextConfig.Update("", "", args[0]); err != nil {
+				return err
+			}
+
+			return current.Save()
+		},
+	}
+}


Test patch:
diff --git a/cmd/select_test.go b/cmd/select_test.go
new file mode 100644
index 0000000..387b66a
--- /dev/null
+++ b/cmd/select_test.go
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2018-2022 The NATS Authors
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package cmd
+
+import (
+	"testing"
+
+	"github.com/stretchr/testify/require"
+)
+
+func TestSelectAccount(t *testing.T) {
+	require := require.New(t)
+	ts := NewTestStore(t, "test")
+	defer ts.Done(t)
+
+	_, _, err := ExecuteCmd(selectAccountCmd(), "A")
+	require.NotNil(err)
+
+	ts.AddAccount(t, "A")
+	ts.AddAccount(t, "B")
+
+	_, _, err = ExecuteCmd(selectAccountCmd(), "A")
+	require.Nil(err)
+	conf := GetConfig()
+	require.Equal(conf.Account, "A")
+
+	_, _, err = ExecuteCmd(selectAccountCmd(), "B")
+	require.Nil(err)
+	conf = GetConfig()
+	require.Equal(conf.Account, "B")
+
+	_, _, err = ExecuteCmd(selectAccountCmd(), "NO")
+	require.NotNil(err)
+	require.Contains(err.Error(), "\"NO\" not in accounts for operator")
+
+	conf = GetConfig()
+	require.Equal(conf.Account, "B")
+}
+
+func TestSelectOperator(t *testing.T) {
+	require := require.New(t)
+	ts := NewTestStore(t, "test")
+	defer ts.Done(t)
+
+	_, _, err := ExecuteCmd(selectOperatorCmd(), "test")
+	require.Nil(err)
+
+	ts.AddOperator(t, "test2")
+	ts.AddOperator(t, "test3")
+
+	_, _, err = ExecuteCmd(selectOperatorCmd(), "test2")
+	require.Nil(err)
+	conf := GetConfig()
+	require.Equal(conf.Operator, "test2")
+
+	_, _, err = ExecuteCmd(selectOperatorCmd(), "test3")
+	require.Nil(err)
+	conf = GetConfig()
+	require.Equal(conf.Operator, "test3")
+
+	_, _, err = ExecuteCmd(selectOperatorCmd(), "NO")
+	require.NotNil(err)
+	require.Contains(err.Error(), "operator \"NO\" not in")
+
+	conf = GetConfig()
+	require.Equal(conf.Operator, "test3")
+}