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
53 changes: 29 additions & 24 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
"github.com/elasticpath/epcc-cli/external/httpclient"
"github.com/elasticpath/epcc-cli/external/json"
"github.com/elasticpath/epcc-cli/external/resources"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"io/ioutil"
"log"
"strconv"
"strings"
)

Expand All @@ -27,29 +28,33 @@ var delete = &cobra.Command{
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)
}
deleteURL := resource.DeleteEntityInfo.Url
idCount := strings.Count(deleteURL, "%")
if len(args)-1 != idCount {
return fmt.Errorf("Not enough args")
}
return nil

for i := 0; i < idCount; i++ {
deleteURL = strings.Replace(deleteURL, "%"+strconv.Itoa(i+1), args[i+1], 1)
}

// Submit request
resp, err := httpclient.DoRequest(context.TODO(), "DELETE", deleteURL, "", nil)
if err != nil {
return fmt.Errorf("Got error %s", err.Error())
}
defer resp.Body.Close()

// Print the body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
// Check if error response
if resp.StatusCode >= 400 && resp.StatusCode <= 600 {
log.Println(resp.Status)
}

return json.PrintJson(string(body))
},
}
2 changes: 1 addition & 1 deletion cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"github.com/elasticpath/epcc-cli/external/httpclient"
"github.com/elasticpath/epcc-cli/external/json"
"github.com/elasticpath/epcc-cli/external/resources"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"io/ioutil"
"log"
"net/url"
"strconv"
"strings"
Expand Down