Skip to content

Commit

Permalink
commend to delete a context
Browse files Browse the repository at this point in the history
  • Loading branch information
pmenglund committed Oct 23, 2023
1 parent 8ecc854 commit aafb2ae
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
30 changes: 30 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion cmd/verbs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
14 changes: 14 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit aafb2ae

Please sign in to comment.