Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: actions download #64

Merged
merged 4 commits into from
Jan 27, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions internal/cli/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -24,6 +25,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))

Expand Down Expand Up @@ -162,6 +164,66 @@ 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 <actionid> --version <versionid | draft>`,
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 {
return err
}

if version.ID == "" {
version.ID = "draft"
}
return nil
})

cli.renderer.Infof("Code downloaded to %s/code.js", path)

if err != nil {
turcottedanny marked this conversation as resolved.
Show resolved Hide resolved
return err
}

if err := ioutil.WriteFile(path+"/code.js", []byte(version.Code), 0644); err != nil {
return err
}

version.Code = "code.js"
turcottedanny marked this conversation as resolved.
Show resolved Hide resolved
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

Expand Down