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

Actions improvement #73

Merged
merged 12 commits into from
Jan 27, 2021
56 changes: 53 additions & 3 deletions internal/cli/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io/ioutil"
"os"
"strconv"

"github.com/auth0/auth0-cli/internal/ansi"
"github.com/auth0/auth0-cli/internal/auth0"
Expand Down Expand Up @@ -141,6 +142,13 @@ func deployActionCmd(cli *cli) *cobra.Command {
Short: "Deploys the action version",
Long: `$ auth0 actions deploy --name <actionid> --version <versionid>`,
RunE: func(cmd *cobra.Command, args []string) error {
if versionId == "" {
var err error
versionId, err = askVersion(cli, actionId)
if err != nil {
return err
}
}
var version *management.ActionVersion
err := ansi.Spinner(fmt.Sprintf("Deploying action: %s, version: %s", actionId, versionId), func() (err error) {
version, err = cli.api.ActionVersion.Deploy(actionId, versionId)
Expand All @@ -158,13 +166,46 @@ func deployActionCmd(cli *cli) *cobra.Command {
}

cmd.Flags().StringVar(&actionId, "name", "", "Action ID to deploy")
cmd.Flags().StringVarP(&versionId, "version", "v", "draft", "Version ID of the action to deploy")
cmd.Flags().StringVarP(&versionId, "version", "v", "", "Version ID of the action to deploy")

mustRequireFlags(cmd, "name")

return cmd
}

func askVersion(cli *cli, actionId string) (string, error) {
var versionId string
versions, err := cli.api.ActionVersion.List(actionId)
if err != nil {
return "", err
}

var options []string
options = append(options, "draft")

for _, v := range versions.Versions {
options = append(options, fmt.Sprint(v.Number))
turcottedanny marked this conversation as resolved.
Show resolved Hide resolved
}

var versionNumber string
if err = prompt.AskOne(prompt.SelectInput("Actions version", "Choose a version", options, "draft"), &versionNumber); err != nil {
return "", err
}

if versionNumber == "draft" {
versionId = "draft"
} else {
i, err := strconv.Atoi(versionNumber)
if err != nil {
return "", err
}

versionId = versions.Versions[len(versions.Versions)-i].ID
turcottedanny marked this conversation as resolved.
Show resolved Hide resolved
}

return versionId, nil
}

func downloadActionCmd(cli *cli) *cobra.Command {
var actionId string
var versionId string
Expand All @@ -175,6 +216,15 @@ func downloadActionCmd(cli *cli) *cobra.Command {
Short: "Download the action version",
Long: `$ auth0 actions download --name <actionid> --version <versionid | draft>`,
RunE: func(cmd *cobra.Command, args []string) error {

if versionId == "" {
var err error
versionId, err = askVersion(cli, actionId)
if err != nil {
return err
}
}

cli.renderer.Infof("It will overwrite files in %s", path)
if confirmed := prompt.Confirm("Do you wish to proceed?"); !confirmed {
return nil
Expand Down Expand Up @@ -217,7 +267,7 @@ func downloadActionCmd(cli *cli) *cobra.Command {
}

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(&versionId, "version", "v", "", "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")
Expand Down Expand Up @@ -272,7 +322,7 @@ func createActionCmd(cli *cli) *cobra.Command {
Long: `$ auth0 actions create
Creates a new action:

$ auth0 actions create --name my-action --trigger post-login --file action.js --dependency [email protected]
$ auth0 actions create --name my-action --trigger post-login --file action.js --dependency [email protected]
`,
RunE: func(cmd *cobra.Command, args []string) error {
if err := validators.TriggerID(trigger); err != nil {
Expand Down
9 changes: 9 additions & 0 deletions internal/prompt/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ func askOne(prompt survey.Prompt, response interface{}) error {
return survey.AskOne(prompt, response, stdErrWriter, icons)
}

func SelectInput(name string, message string, options []string, defaultValue string) *survey.Question {
input := &survey.Question{
Name: name,
Prompt: &survey.Select{Message: message, Options: options, Default: defaultValue},
}

return input
}

func TextInput(name string, message string, help string, value string, required bool) *survey.Question {
input := &survey.Question{
Name: name,
Expand Down