From 6d774f265745fbab9d2061976e7ee33b9d1b7cb3 Mon Sep 17 00:00:00 2001 From: Vaibhav Pathak Date: Thu, 21 Jul 2022 16:18:22 +0200 Subject: [PATCH 1/2] subcommand to delete added for deleting all resources --- cmd/delete.go | 25 +++++++++++++++++++++++-- cmd/root.go | 5 ++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/cmd/delete.go b/cmd/delete.go index e482217..0fd75ef 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -1,15 +1,36 @@ package cmd import ( - "github.com/spf13/cobra" + "log" + "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 { + log.Println("all flag is provided") + 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") + } + log.Println(kubeConfigPathDel) + 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{ From 97829724d07dc28d460f8a0b7bbf2c6ef70eca6a Mon Sep 17 00:00:00 2001 From: Vaibhav Pathak Date: Thu, 21 Jul 2022 16:41:41 +0200 Subject: [PATCH 2/2] removed some print msg --- cmd/delete.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/cmd/delete.go b/cmd/delete.go index 0fd75ef..b460575 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -1,8 +1,6 @@ package cmd import ( - "log" - "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/viveksinghggits/akcess/pkg/kube" @@ -15,7 +13,6 @@ var deleteCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) error { allFlag, _ := cmd.Flags().GetBool("all") if allFlag { - log.Println("all flag is provided") s, err := store.NewFileStore() if err != nil { return errors.Wrap(err, "Creating store instance") @@ -24,7 +21,6 @@ var deleteCmd = &cobra.Command{ if err != nil { return errors.Wrap(err, "Calling list from store") } - log.Println(kubeConfigPathDel) for _, c := range list { kube.DeleteResources(c.Id, kubeConfigPathDel) }