Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
55 changes: 55 additions & 0 deletions cmd/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package cmd

import (
"context"
"fmt"
"github.com/elasticpath/epcc-cli/external/httpclient"
"github.com/elasticpath/epcc-cli/external/json"
"github.com/elasticpath/epcc-cli/external/resources"
"github.com/spf13/cobra"
"io/ioutil"
"log"
"strings"
)

var delete = &cobra.Command{
Use: "delete [RESOURCE] [ID_1] [ID_2]",
Short: "Deletes a single resource.",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// Find Resource
resource, ok := resources.Resources[args[0]]
if !ok {
return fmt.Errorf("Could not find resource %s", args[0])
}

if resource.DeleteEntityInfo == nil {
return fmt.Errorf("Resource %s doesn't support DELETE", args[0])
}

for i := 1; i < len(args); i++ {
deleteURL := resource.DeleteEntityInfo.Url
deleteURL = strings.Replace(deleteURL, "%1", args[i], 1)

// Submit request
resp, err := httpclient.DoRequest(context.TODO(), "DELETE", deleteURL, "", nil)

if err != nil {
log.Println(err)
}
defer resp.Body.Close()

// Print the body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
}
// Check if error response
if resp.StatusCode >= 400 && resp.StatusCode <= 600 {
json.PrintJson(string(body))
log.Println(resp.Status)
}
}
return nil
},
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func init() {
docsCommand,
testJson,
get,
delete,
resourceListCommand,
)

Expand Down
Loading