Skip to content

Commit

Permalink
Fix/enhancing delete 486 (#880)
Browse files Browse the repository at this point in the history
* remove supportCORSRequest and support both CORS pre-flight and CORS requests by default

* enhance object delete
add -f,-a flag to delete object from a yaml file or delete all object

* Update cmd/client/command/object.go

don't use the short version to help users avoid faulty operation.

Co-authored-by: Bomin Zhang <[email protected]>

* enhance object delete
add parameter check

* enhance object delete
update delete all,avoid misusing curl

* Delete blank line

Co-authored-by: Bomin Zhang <[email protected]>

* Delete blank line

Co-authored-by: Bomin Zhang <[email protected]>

* Delete blank line

Co-authored-by: Bomin Zhang <[email protected]>

Co-authored-by: jiangshaohua <[email protected]>
Co-authored-by: Bomin Zhang <[email protected]>
  • Loading branch information
3 people committed Dec 28, 2022
1 parent 9b92b56 commit d5edf28
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
40 changes: 33 additions & 7 deletions cmd/client/command/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package command

import (
"errors"
"fmt"
"net/http"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -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 <object_name>",
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 <object_name> cannot be used together")
}
}

if len(args) != 0 && len(specFile) != 0 {
return errors.New("--file and <object_name> 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
}

Expand Down
20 changes: 20 additions & 0 deletions pkg/api/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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")

Expand Down

0 comments on commit d5edf28

Please sign in to comment.