From aafb2aeebee4c1418f21ed9a7f91a04415d7634b Mon Sep 17 00:00:00 2001 From: Martin Englund Date: Mon, 23 Oct 2023 14:21:16 -0700 Subject: [PATCH] commend to delete a context --- cmd/config.go | 30 ++++++++++++++++++++++++++++++ cmd/verbs.go | 3 ++- config/config.go | 14 ++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/cmd/config.go b/cmd/config.go index 9db736a..78d3e3f 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -70,6 +70,36 @@ func listContext[T apiserver](out io.Writer, current string, m map[string]T) { } } +func newDeleteContextCmd() *cobra.Command { + cmd := cobra.Command{ + Use: "context NAME", + Aliases: []string{"ctx"}, + Short: "delete an authentication context", + Annotations: group("context"), + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + cfg, err := config.Load() + if err != nil { + return err + } + + if err = cfg.DeleteContext(args[0]); err != nil { + return err + } + + if err = config.Store(cfg); err != nil { + return err + } + + _, _ = fmt.Fprintf(cmd.OutOrStdout(), "context %s deleted\n", args[0]) + + return nil + }, + } + + return &cmd +} + func newCreateContextCmd() *cobra.Command { cmd := cobra.Command{ Use: "context NAME", diff --git a/cmd/verbs.go b/cmd/verbs.go index f0f168f..c2a1062 100644 --- a/cmd/verbs.go +++ b/cmd/verbs.go @@ -164,8 +164,9 @@ func addVerbs(root *cobra.Command) { getCmd.AddCommand(newGetWorkspaceCmd()) listCmd.AddCommand(newListWorkspacesCmd()) - // config + // context createCmd.AddCommand(newCreateContextCmd()) + deleteCmd.AddCommand(newDeleteContextCmd()) listCmd.AddCommand(newListContextsCmd()) useCmd.AddCommand(newUseContextCmd()) diff --git a/config/config.go b/config/config.go index f9763d4..e1c206a 100644 --- a/config/config.go +++ b/config/config.go @@ -77,6 +77,20 @@ func (c *Config) AsOptions(override string) ([]rockset.RockOption, error) { return nil, fmt.Errorf("%w", NotFoundErr) } +func (c *Config) DeleteContext(name string) error { + if _, found := c.Tokens[name]; found { + delete(c.Tokens, name) + return nil + } + + if _, found := c.Keys[name]; found { + delete(c.Keys, name) + return nil + } + + return fmt.Errorf("context %s: %w", name, NotFoundErr) +} + func (c *Config) AddToken(name string, token Token) error { if _, found := c.Tokens[name]; found { return ContextAlreadyExistErr