diff --git a/cmd/delete.go b/cmd/delete.go index 1a11696c..1d0f5b9a 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -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" ) @@ -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)) }, } diff --git a/cmd/get.go b/cmd/get.go index 04fb466a..17030a96 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -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"