From e7fcb2294cbf4a5bc57118bc4eb19ad5cf733019 Mon Sep 17 00:00:00 2001 From: Aaron Feledy Date: Wed, 15 Nov 2023 03:13:26 -0600 Subject: [PATCH] TT-448: add mservctl update command (#24) While mserv supports updating bundles, the mservctl client does not currently implement this functionality. This PR implements it by adding an mservctl update command to the tool. Usage: mservctl update 9c9ecec1-8f98-4c3f-88cd-ca3c27599e6b bundle.zip --- mservctl/cmd/update.go | 43 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 mservctl/cmd/update.go diff --git a/mservctl/cmd/update.go b/mservctl/cmd/update.go new file mode 100644 index 00000000..1b69c039 --- /dev/null +++ b/mservctl/cmd/update.go @@ -0,0 +1,43 @@ +package cmd + +import ( + "os" + "time" + + "github.com/spf13/cobra" + + "github.com/TykTechnologies/mserv/mservclient/client/mw" +) + +// updateCmd represents the update command +var updateCmd = &cobra.Command{ + Use: "update", + Short: "Updates a middleware on mserv", + Long: "Updates a middleware by ID", + Example: `mservctl update 13b0eb10-419f-40ef-838d-6d26bb2eeaa8 /path/to/bundle.zip`, + Args: cobra.ExactArgs(2), + Run: updateMiddleware, +} + +func init() { + rootCmd.AddCommand(updateCmd) +} + +func updateMiddleware(cmd *cobra.Command, args []string) { + file, err := os.Open(args[1]) + if err != nil { + log.WithError(err).Error("Couldn't open the bundle file") + return + } + defer file.Close() + + params := mw.NewMwUpdateParams().WithID(args[0]).WithUploadFile(file).WithTimeout(120 * time.Second) + + resp, err := mservapi.Mw.MwUpdate(params, defaultAuth()) + if err != nil { + log.WithError(err).Error("Couldn't update middleware") + return + } + + cmd.Printf("Middleware uploaded successfully, ID: %s\n", resp.GetPayload().Payload.BundleID) +}