@@ -2,6 +2,8 @@ package scripttags;
22
33import (
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+
1420var 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
1671func 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" ,
0 commit comments