Skip to content

Commit

Permalink
Add support for deleting script tags
Browse files Browse the repository at this point in the history
  • Loading branch information
sshaw committed Mar 29, 2022
1 parent c34aea3 commit 2a28731
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@ ScriptTag utilities
sdt scripttags command [command options] [arguments...]

COMMANDS:
ls List scripttags for the given shop
help, h Shows a list of commands or help for one command
delete, del, rm, d Delete the given ScriptTag
list, ls List scripttags for the given shop
help, h Shows a list of commands or help for one command

OPTIONS:
--help, -h show help (default: false)
Expand Down
65 changes: 64 additions & 1 deletion cmd/scripttags/scripttags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package scripttags;

import (
"fmt"
"regexp"
"strconv"
"strings"

"github.com/urfave/cli/v2"
Expand All @@ -11,7 +13,60 @@ import (
"github.com/ScreenStaring/shopify-dev-tools/cmd"
)

type listScriptTagOptions struct {
Src string `url:"src"`
}

var Cmd cli.Command
// Allow for protocol relative URLs
var scriptTagURL = regexp.MustCompile(`(?i)\A(?:https:)?//[\da-z]`)

func deleteAction(c *cli.Context) error {
if(c.Args().Len() == 0) {
return fmt.Errorf("You must supply an script tag id or URL")
}

var ids[] int64
var err error

client := cmd.NewShopifyClient(c)

if(scriptTagURL.MatchString(c.Args().Get(0))) {
options := listScriptTagOptions{Src: c.Args().Get(0)}
tags, err := client.ScriptTag.List(options)

if err != nil {
return fmt.Errorf("Cannot list script tag with URL %s: %s", options.Src, err)
}

if len(tags) == 0 {
return fmt.Errorf("Cannot find script tag with URL %s", options.Src)
}

// Delete all with givv
for _, tag := range tags {
ids = append(ids, tag.ID)
}
} else {
id, err := strconv.ParseInt(c.Args().Get(0), 10, 64)
if err != nil {
return fmt.Errorf("Script tag id '%s' is invalid: must be an int", c.Args().Get(0))
}

ids = append(ids, id)
}

for _, id := range ids {
err = client.ScriptTag.Delete(id)
if err != nil {
return fmt.Errorf("Cannot delete script tag: %s", err)
}

fmt.Printf("Script tag %d deleted\n", id)
}

return nil
}

func listAction(c *cli.Context) error {
hooks, err := cmd.NewShopifyClient(c).ScriptTag.List(nil)
Expand Down Expand Up @@ -49,7 +104,15 @@ func init() {
Usage: "ScriptTag utilities",
Subcommands: []*cli.Command{
{
Name: "ls",
Name: "delete",
Aliases: []string{"del", "rm", "d"},
Flags: append(cmd.Flags),
Action: deleteAction,
Usage: "Delete the given ScriptTag",
},
{
Name: "list",
Aliases: []string{"ls"},
Flags: append(cmd.Flags),
Action: listAction,
Usage: "List scripttags for the given shop",
Expand Down
2 changes: 2 additions & 0 deletions cmd/webhooks/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ func createAction(c *cli.Context) error {
Topic: c.String("topic"),
Fields: c.StringSlice("fields"),
Format: format(c),
// Not supported bu bold!
//ApiVersion: c.String("api-version"),
}

hook, err := cmd.NewShopifyClient(c).Webhook.Create(options)
Expand Down

0 comments on commit 2a28731

Please sign in to comment.