From 2a287316e9393087442174b7fd4ff1101c2ea2f2 Mon Sep 17 00:00:00 2001 From: sshaw Date: Tue, 29 Mar 2022 19:28:24 -0400 Subject: [PATCH] Add support for deleting script tags --- README.md | 5 +-- cmd/scripttags/scripttags.go | 65 +++++++++++++++++++++++++++++++++++- cmd/webhooks/webhooks.go | 2 ++ 3 files changed, 69 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 523fa79..8f2da1d 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/cmd/scripttags/scripttags.go b/cmd/scripttags/scripttags.go index c1bf706..7707005 100644 --- a/cmd/scripttags/scripttags.go +++ b/cmd/scripttags/scripttags.go @@ -2,6 +2,8 @@ package scripttags; import ( "fmt" + "regexp" + "strconv" "strings" "github.com/urfave/cli/v2" @@ -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) @@ -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", diff --git a/cmd/webhooks/webhooks.go b/cmd/webhooks/webhooks.go index e2c0ef3..c049798 100644 --- a/cmd/webhooks/webhooks.go +++ b/cmd/webhooks/webhooks.go @@ -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)