diff --git a/cmd/client/command/object.go b/cmd/client/command/object.go index 4586f1ac96..ba35dcbaf9 100644 --- a/cmd/client/command/object.go +++ b/cmd/client/command/object.go @@ -19,6 +19,7 @@ package command import ( "errors" + "fmt" "net/http" "github.com/spf13/cobra" @@ -96,23 +97,48 @@ func updateObjectCmd() *cobra.Command { } func deleteObjectCmd() *cobra.Command { + var specFile string + var allFlag bool cmd := &cobra.Command{ - Use: "delete", - Short: "Delete an object", - Example: "egctl object delete ", + Use: "delete", + Short: "Delete an object from a yaml file or name", Args: func(cmd *cobra.Command, args []string) error { - if len(args) != 1 { - return errors.New("requires one object name to be deleted") + if allFlag { + if len(specFile) != 0 { + return errors.New("--all and --file cannot be used together") + } + if len(args) != 0 { + return errors.New("--all and cannot be used together") + } + } + + if len(args) != 0 && len(specFile) != 0 { + return errors.New("--file and cannot be used together") } return nil }, - Run: func(cmd *cobra.Command, args []string) { + if allFlag { + handleRequest(http.MethodDelete, makeURL(objectsURL+fmt.Sprintf("?all=%v", true)), nil, cmd) + return + } + + if len(specFile) != 0 { + visitor := buildSpecVisitor(specFile, cmd) + visitor.Visit(func(s *spec) error { + handleRequest(http.MethodDelete, makeURL(objectURL, s.Name), nil, cmd) + return nil + }) + visitor.Close() + return + } + handleRequest(http.MethodDelete, makeURL(objectURL, args[0]), nil, cmd) }, } - + cmd.Flags().StringVarP(&specFile, "file", "f", "", "A yaml file specifying the object.") + cmd.Flags().BoolVarP(&allFlag, "all", "", false, "Delete all object.") return cmd } diff --git a/pkg/api/object.go b/pkg/api/object.go index 4c4aa8f81d..33193252e0 100644 --- a/pkg/api/object.go +++ b/pkg/api/object.go @@ -72,6 +72,11 @@ func (s *Server) objectAPIEntries() []*Entry { Method: "DELETE", Handler: s.deleteObject, }, + { + Path: ObjectPrefix, + Method: "DELETE", + Handler: s.deleteObjects, + }, { Path: StatusObjectPrefix, Method: "GET", @@ -151,6 +156,21 @@ func (s *Server) deleteObject(w http.ResponseWriter, r *http.Request) { s.upgradeConfigVersion(w, r) } +func (s *Server) deleteObjects(w http.ResponseWriter, r *http.Request) { + allFlag := r.URL.Query().Get("all") + if allFlag == "true" { + s.Lock() + defer s.Unlock() + + specs := s._listObjects() + for _, spec := range specs { + s._deleteObject(spec.Name()) + } + + s.upgradeConfigVersion(w, r) + } +} + func (s *Server) getObject(w http.ResponseWriter, r *http.Request) { name := chi.URLParam(r, "name")