diff --git a/cmd/delete.go b/cmd/delete.go index e482217..b460575 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -1,15 +1,32 @@ package cmd import ( + "github.com/pkg/errors" "github.com/spf13/cobra" - "github.com/viveksinghggits/akcess/pkg/kube" + "github.com/viveksinghggits/akcess/pkg/store" ) var deleteCmd = &cobra.Command{ Use: "delete", Short: "Delete the kubernetes resources that were made specific allow command", RunE: func(cmd *cobra.Command, args []string) error { - return kube.DeleteResources(delIdentifier, kubeConfigPathDel) + allFlag, _ := cmd.Flags().GetBool("all") + if allFlag { + s, err := store.NewFileStore() + if err != nil { + return errors.Wrap(err, "Creating store instance") + } + list, err := s.List() + if err != nil { + return errors.Wrap(err, "Calling list from store") + } + for _, c := range list { + kube.DeleteResources(c.Id, kubeConfigPathDel) + } + } else { + kube.DeleteResources(delIdentifier, kubeConfigPathDel) + } + return nil }, } diff --git a/cmd/root.go b/cmd/root.go index e30a687..e001b5e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -34,6 +34,7 @@ var ( res = []string{} delIdentifier string kubeConfigPathDel string + delAll bool // VERSION will be overridden by ldflags when we build the project using goreleaser VERSION = "DEV" ) @@ -55,10 +56,12 @@ func init() { deleteCmd.Flags().StringVarP(&delIdentifier, "id", "i", "", "Id for which the k8s resources should be deleted. Can be figured out from list command") deleteCmd.Flags().StringVarP(&kubeConfigPathDel, "kubeconfig", "k", "", "Path to kubeconfig file") + deleteCmd.Flags().BoolVarP(&delAll, "all", "a", false, "flag to delete all resource") // required flags for delete command // `akcess delete -id` is a valid command and takes `d` as value of -i flag // `akcess delete -id abc` is also valid and value of -i flag is d and abc id discarded - deleteCmd.MarkFlagRequired("id") + // Removing id as required so that delete all flag can work. + //deleteCmd.MarkFlagRequired("id") } var versionCmd = &cobra.Command{