Skip to content

Commit 2a28731

Browse files
committed
Add support for deleting script tags
1 parent c34aea3 commit 2a28731

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,9 @@ ScriptTag utilities
126126
sdt scripttags command [command options] [arguments...]
127127

128128
COMMANDS:
129-
ls List scripttags for the given shop
130-
help, h Shows a list of commands or help for one command
129+
delete, del, rm, d Delete the given ScriptTag
130+
list, ls List scripttags for the given shop
131+
help, h Shows a list of commands or help for one command
131132

132133
OPTIONS:
133134
--help, -h show help (default: false)

cmd/scripttags/scripttags.go

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package scripttags;
22

33
import (
44
"fmt"
5+
"regexp"
6+
"strconv"
57
"strings"
68

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

16+
type listScriptTagOptions struct {
17+
Src string `url:"src"`
18+
}
19+
1420
var Cmd cli.Command
21+
// Allow for protocol relative URLs
22+
var scriptTagURL = regexp.MustCompile(`(?i)\A(?:https:)?//[\da-z]`)
23+
24+
func deleteAction(c *cli.Context) error {
25+
if(c.Args().Len() == 0) {
26+
return fmt.Errorf("You must supply an script tag id or URL")
27+
}
28+
29+
var ids[] int64
30+
var err error
31+
32+
client := cmd.NewShopifyClient(c)
33+
34+
if(scriptTagURL.MatchString(c.Args().Get(0))) {
35+
options := listScriptTagOptions{Src: c.Args().Get(0)}
36+
tags, err := client.ScriptTag.List(options)
37+
38+
if err != nil {
39+
return fmt.Errorf("Cannot list script tag with URL %s: %s", options.Src, err)
40+
}
41+
42+
if len(tags) == 0 {
43+
return fmt.Errorf("Cannot find script tag with URL %s", options.Src)
44+
}
45+
46+
// Delete all with givv
47+
for _, tag := range tags {
48+
ids = append(ids, tag.ID)
49+
}
50+
} else {
51+
id, err := strconv.ParseInt(c.Args().Get(0), 10, 64)
52+
if err != nil {
53+
return fmt.Errorf("Script tag id '%s' is invalid: must be an int", c.Args().Get(0))
54+
}
55+
56+
ids = append(ids, id)
57+
}
58+
59+
for _, id := range ids {
60+
err = client.ScriptTag.Delete(id)
61+
if err != nil {
62+
return fmt.Errorf("Cannot delete script tag: %s", err)
63+
}
64+
65+
fmt.Printf("Script tag %d deleted\n", id)
66+
}
67+
68+
return nil
69+
}
1570

1671
func listAction(c *cli.Context) error {
1772
hooks, err := cmd.NewShopifyClient(c).ScriptTag.List(nil)
@@ -49,7 +104,15 @@ func init() {
49104
Usage: "ScriptTag utilities",
50105
Subcommands: []*cli.Command{
51106
{
52-
Name: "ls",
107+
Name: "delete",
108+
Aliases: []string{"del", "rm", "d"},
109+
Flags: append(cmd.Flags),
110+
Action: deleteAction,
111+
Usage: "Delete the given ScriptTag",
112+
},
113+
{
114+
Name: "list",
115+
Aliases: []string{"ls"},
53116
Flags: append(cmd.Flags),
54117
Action: listAction,
55118
Usage: "List scripttags for the given shop",

cmd/webhooks/webhooks.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ func createAction(c *cli.Context) error {
113113
Topic: c.String("topic"),
114114
Fields: c.StringSlice("fields"),
115115
Format: format(c),
116+
// Not supported bu bold!
117+
//ApiVersion: c.String("api-version"),
116118
}
117119

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

0 commit comments

Comments
 (0)