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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
enhance object delete
add -f,-a flag to delete object from a yaml file or delete all object
  • Loading branch information
jiangshaohua committed Dec 22, 2022
commit a1830b817115d661757395302e13bdef926ed6c5
30 changes: 20 additions & 10 deletions cmd/client/command/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,33 @@ 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>",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("requires one object name to be deleted")
Use: "delete",
Short: "Delete an object from a yaml file or name",
Run: func(cmd *cobra.Command, args []string) {

coderabbit214 marked this conversation as resolved.
Show resolved Hide resolved
if allFlag {
handleRequest(http.MethodDelete, makeURL(objectsURL), nil, cmd)
return
}

return nil
},
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
}

Run: func(cmd *cobra.Command, args []string) {
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", "a", false, "Delete all object.")
coderabbit214 marked this conversation as resolved.
Show resolved Hide resolved
return cmd
}

Expand Down
17 changes: 17 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.deleteAllObject,
},
{
Path: StatusObjectPrefix,
Method: "GET",
Expand Down Expand Up @@ -151,6 +156,18 @@ func (s *Server) deleteObject(w http.ResponseWriter, r *http.Request) {
s.upgradeConfigVersion(w, r)
}

func (s *Server) deleteAllObject(w http.ResponseWriter, r *http.Request) {
s.Lock()
defer s.Unlock()

specs := s._listObjects()
for _, spec := range specs {
s._deleteObject(spec.Name())
}

coderabbit214 marked this conversation as resolved.
Show resolved Hide resolved
s.upgradeConfigVersion(w, r)
}

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

Expand Down