From 2ca7933ee4052d66ccdbe7a4de6a0f11617bcf33 Mon Sep 17 00:00:00 2001 From: Danny Turcotte Date: Tue, 26 Jan 2021 22:32:26 -0500 Subject: [PATCH 1/4] feat: add download action --- internal/cli/actions.go | 56 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/internal/cli/actions.go b/internal/cli/actions.go index 86570915f..e9c298c1d 100644 --- a/internal/cli/actions.go +++ b/internal/cli/actions.go @@ -24,6 +24,7 @@ func actionsCmd(cli *cli) *cobra.Command { cmd.AddCommand(testActionCmd(cli)) cmd.AddCommand(createActionCmd(cli)) cmd.AddCommand(deployActionCmd(cli)) + cmd.AddCommand(downloadActionCmd(cli)) cmd.AddCommand(listActionVersionsCmd(cli)) cmd.AddCommand(triggersCmd(cli)) @@ -162,6 +163,61 @@ func deployActionCmd(cli *cli) *cobra.Command { return cmd } +func downloadActionCmd(cli *cli) *cobra.Command { + var actionId string + var versionId string + var path string + + cmd := &cobra.Command{ + Use: "download", + Short: "Download the action version", + Long: `$ auth0 actions download --name --version `, + RunE: func(cmd *cobra.Command, args []string) error { + var version *management.ActionVersion + err := ansi.Spinner(fmt.Sprintf("Downloading action: %s, version: %s", actionId, versionId), func() (err error) { + if version, err = cli.api.ActionVersion.Read(actionId, versionId); err != nil { + return err + } + + if version.ID == "" { + version.ID = "draft" + } + return nil + }) + + cli.renderer.Infof("Code downloaded to %s/code.js", path) + + if err != nil { + return err + } + + if err := ioutil.WriteFile(path+"/code.js", []byte(version.Code), 0644); err != nil { + return err + } + + version.Code = "code.js" + metadata, err := json.MarshalIndent(version, "", " ") + if err != nil { + return err + } + + if err := ioutil.WriteFile(path+"/metadata.json", metadata, 0644); err != nil { + return err + } + + return nil + }, + } + + cmd.Flags().StringVar(&actionId, "name", "", "Action ID to deploy") + cmd.Flags().StringVarP(&versionId, "version", "v", "draft", "Version ID of the action to deploy or draft, default: draft") + cmd.Flags().StringVarP(&path, "path", "p", "./", "Path to save the action content") + + mustRequireFlags(cmd, "name") + + return cmd +} + func listActionVersionsCmd(cli *cli) *cobra.Command { var actionId string From 360efecfdc6a3cbd81b591eb7f12548e7318d549 Mon Sep 17 00:00:00 2001 From: Danny Turcotte Date: Tue, 26 Jan 2021 22:39:44 -0500 Subject: [PATCH 2/4] add confirm --- internal/cli/actions.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/internal/cli/actions.go b/internal/cli/actions.go index e9c298c1d..228833c93 100644 --- a/internal/cli/actions.go +++ b/internal/cli/actions.go @@ -8,6 +8,7 @@ import ( "github.com/auth0/auth0-cli/internal/ansi" "github.com/auth0/auth0-cli/internal/auth0" + "github.com/auth0/auth0-cli/internal/prompt" "github.com/auth0/auth0-cli/internal/validators" "github.com/spf13/cobra" "gopkg.in/auth0.v5/management" @@ -173,6 +174,11 @@ func downloadActionCmd(cli *cli) *cobra.Command { Short: "Download the action version", Long: `$ auth0 actions download --name --version `, RunE: func(cmd *cobra.Command, args []string) error { + cli.renderer.Infof("It will overwrite files in %s", path) + if confirmed := prompt.Confirm("Do you wish to proceed?"); !confirmed { + return nil + } + var version *management.ActionVersion err := ansi.Spinner(fmt.Sprintf("Downloading action: %s, version: %s", actionId, versionId), func() (err error) { if version, err = cli.api.ActionVersion.Read(actionId, versionId); err != nil { From 5f4f94695bc0d4d52e0ac2bed825b302e6613c42 Mon Sep 17 00:00:00 2001 From: Danny Turcotte Date: Tue, 26 Jan 2021 22:55:52 -0500 Subject: [PATCH 3/4] fix error handling --- internal/cli/actions.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/cli/actions.go b/internal/cli/actions.go index 228833c93..1272ca944 100644 --- a/internal/cli/actions.go +++ b/internal/cli/actions.go @@ -191,12 +191,12 @@ func downloadActionCmd(cli *cli) *cobra.Command { return nil }) - cli.renderer.Infof("Code downloaded to %s/code.js", path) - if err != nil { return err } + cli.renderer.Infof("Code downloaded to %s/code.js", path) + if err := ioutil.WriteFile(path+"/code.js", []byte(version.Code), 0644); err != nil { return err } From 7592bbccaa6401ae59a1571021be410c5680ddfb Mon Sep 17 00:00:00 2001 From: Danny Turcotte Date: Tue, 26 Jan 2021 23:01:12 -0500 Subject: [PATCH 4/4] remove code attribute --- go.mod | 2 +- go.sum | 2 ++ internal/cli/actions.go | 2 +- vendor/gopkg.in/auth0.v5/management/actions.go | 2 +- vendor/modules.txt | 4 ++-- 5 files changed, 7 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 44c92913e..c3dd7c13c 100644 --- a/go.mod +++ b/go.mod @@ -26,4 +26,4 @@ require ( // replace gopkg.in/auth0.v5 => ../auth0 -replace gopkg.in/auth0.v5 => github.com/go-auth0/auth0 v1.3.1-0.20210127020221-38fd79682c4a +replace gopkg.in/auth0.v5 => github.com/go-auth0/auth0 v1.3.1-0.20210127040011-acb7c665d062 diff --git a/go.sum b/go.sum index 4c9f4bf0a..3b3a7ff89 100644 --- a/go.sum +++ b/go.sum @@ -88,6 +88,8 @@ github.com/go-auth0/auth0 v1.3.1-0.20210126214458-2807092480f8 h1:x5rbuD/ay7ieXn github.com/go-auth0/auth0 v1.3.1-0.20210126214458-2807092480f8/go.mod h1:QQ9fgGj2Wpza15+Ho3mM6amMeKfhzHo2cixcOqdkoKk= github.com/go-auth0/auth0 v1.3.1-0.20210127020221-38fd79682c4a h1:TAnbIrM2gdxfqT4pdPso61aO74mS+mNuIbRDYhfSNKI= github.com/go-auth0/auth0 v1.3.1-0.20210127020221-38fd79682c4a/go.mod h1:QQ9fgGj2Wpza15+Ho3mM6amMeKfhzHo2cixcOqdkoKk= +github.com/go-auth0/auth0 v1.3.1-0.20210127040011-acb7c665d062 h1:WOCTdmjxtCBWcSL34/v0lzvyqGvlPlH76ns86EUuF98= +github.com/go-auth0/auth0 v1.3.1-0.20210127040011-acb7c665d062/go.mod h1:QQ9fgGj2Wpza15+Ho3mM6amMeKfhzHo2cixcOqdkoKk= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= diff --git a/internal/cli/actions.go b/internal/cli/actions.go index 1272ca944..f87b33e97 100644 --- a/internal/cli/actions.go +++ b/internal/cli/actions.go @@ -201,7 +201,7 @@ func downloadActionCmd(cli *cli) *cobra.Command { return err } - version.Code = "code.js" + version.Code = "" metadata, err := json.MarshalIndent(version, "", " ") if err != nil { return err diff --git a/vendor/gopkg.in/auth0.v5/management/actions.go b/vendor/gopkg.in/auth0.v5/management/actions.go index 20daa8d33..f65d537fe 100644 --- a/vendor/gopkg.in/auth0.v5/management/actions.go +++ b/vendor/gopkg.in/auth0.v5/management/actions.go @@ -40,7 +40,7 @@ const ( type ActionVersion struct { ID string `json:"id,omitempty"` Action *Action `json:"action,omitempty"` - Code string `json:"code"` + Code string `json:"code,omitempty"` Dependencies []Dependency `json:"dependencies,omitempty"` Runtime string `json:"runtime,omitempty"` Status VersionStatus `json:"status,omitempty"` diff --git a/vendor/modules.txt b/vendor/modules.txt index ce40a8c49..5589291a0 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -138,7 +138,7 @@ google.golang.org/protobuf/reflect/protoreflect google.golang.org/protobuf/reflect/protoregistry google.golang.org/protobuf/runtime/protoiface google.golang.org/protobuf/runtime/protoimpl -# gopkg.in/auth0.v5 v5.8.0 => github.com/go-auth0/auth0 v1.3.1-0.20210127020221-38fd79682c4a +# gopkg.in/auth0.v5 v5.8.0 => github.com/go-auth0/auth0 v1.3.1-0.20210127040011-acb7c665d062 ## explicit gopkg.in/auth0.v5 gopkg.in/auth0.v5/internal/client @@ -149,4 +149,4 @@ gopkg.in/auth0.v5/management # gopkg.in/yaml.v2 v2.2.8 ## explicit gopkg.in/yaml.v2 -# gopkg.in/auth0.v5 => github.com/go-auth0/auth0 v1.3.1-0.20210127020221-38fd79682c4a +# gopkg.in/auth0.v5 => github.com/go-auth0/auth0 v1.3.1-0.20210127040011-acb7c665d062