Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/enhancing delete 486 #880

Merged
merged 9 commits into from
Dec 28, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 35 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,50 @@ 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")

coderabbit214 marked this conversation as resolved.
Show resolved Hide resolved
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) {

coderabbit214 marked this conversation as resolved.
Show resolved Hide resolved
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

args[0] could be invalid after this change.
and delete is different from create or update as it only requires a name instead of full YAML spec, so it is much more easier for the user to make a fault operation. and we should do more checks for the arguments, like what to do if there're both --all and spec file in the command line?

},
}

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
21 changes: 21 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,
coderabbit214 marked this conversation as resolved.
Show resolved Hide resolved
Method: "DELETE",
Handler: s.deleteObjects,
},
{
Path: StatusObjectPrefix,
Method: "GET",
Expand Down Expand Up @@ -151,6 +156,22 @@ 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)
}

coderabbit214 marked this conversation as resolved.
Show resolved Hide resolved
}

func (s *Server) getObject(w http.ResponseWriter, r *http.Request) {
name := chi.URLParam(r, "name")

Expand Down