From 730fae8f5e1c725e5fcae4fdf8e997a0fad635bd Mon Sep 17 00:00:00 2001 From: Danny Date: Wed, 27 Jan 2021 14:33:28 -0500 Subject: [PATCH] feat: Actions improvement (#73) --- go.mod | 2 +- go.sum | 2 + internal/cli/actions.go | 91 +++++++++++++------ internal/display/actions.go | 33 ++++--- .../gopkg.in/auth0.v5/management/actions.go | 1 + vendor/modules.txt | 4 +- 6 files changed, 90 insertions(+), 43 deletions(-) diff --git a/go.mod b/go.mod index 66946a4de..b74c472b3 100644 --- a/go.mod +++ b/go.mod @@ -28,4 +28,4 @@ require ( // replace gopkg.in/auth0.v5 => ../auth0 -replace gopkg.in/auth0.v5 => github.com/go-auth0/auth0 v1.3.1-0.20210127122814-819354e637e9 +replace gopkg.in/auth0.v5 => github.com/go-auth0/auth0 v1.3.1-0.20210127175916-f1d24c8c0f88 diff --git a/go.sum b/go.sum index 71f49f466..f3c17822e 100644 --- a/go.sum +++ b/go.sum @@ -89,6 +89,8 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-auth0/auth0 v1.3.1-0.20210127122814-819354e637e9 h1:/Lolghe1pufQWJ+JcmX0UToYIr7bpNpDzo6PoVbFLX8= github.com/go-auth0/auth0 v1.3.1-0.20210127122814-819354e637e9/go.mod h1:QQ9fgGj2Wpza15+Ho3mM6amMeKfhzHo2cixcOqdkoKk= +github.com/go-auth0/auth0 v1.3.1-0.20210127175916-f1d24c8c0f88 h1:pygD+OcQJTK7vDGzre8nrk+P1GDyLJvGw+NgN+vA08w= +github.com/go-auth0/auth0 v1.3.1-0.20210127175916-f1d24c8c0f88/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 461789657..ce6631492 100644 --- a/internal/cli/actions.go +++ b/internal/cli/actions.go @@ -40,21 +40,22 @@ func actionsCmd(cli *cli) *cobra.Command { cmd.AddCommand(deployActionCmd(cli)) cmd.AddCommand(downloadActionCmd(cli)) cmd.AddCommand(listActionVersionsCmd(cli)) - cmd.AddCommand(triggersCmd(cli)) + cmd.AddCommand(bindActionCmd(cli)) + + cmd.AddCommand(flowsCmd(cli)) return cmd } -func triggersCmd(cli *cli) *cobra.Command { +func flowsCmd(cli *cli) *cobra.Command { cmd := &cobra.Command{ - Use: "triggers", - Short: "Manage resources for action triggers", + Use: "flows", + Short: "Manages resources for action flows", } cmd.SetUsageTemplate(resourceUsageTemplate()) - cmd.AddCommand(showTriggerCmd(cli)) - cmd.AddCommand(reorderTriggerCmd(cli)) - cmd.AddCommand(createTriggerCmd(cli)) + cmd.AddCommand(showFlowCmd(cli)) + cmd.AddCommand(updateFlowCmd(cli)) return cmd } @@ -196,11 +197,11 @@ func deployActionCmd(cli *cli) *cobra.Command { } if shouldPrompt(cmd, actionVersion) { - input := prompt.TextInputDefault(actionVersion, "Version Id:", "Version ID of the action to deploy. Default: draft", "draft", false) - - if err := prompt.AskOne(input, &flags); err != nil { + version, err := askVersion(cli, flags.ID) + if err != nil { return err } + flags.Version = version } var version *management.ActionVersion @@ -226,6 +227,42 @@ func deployActionCmd(cli *cli) *cobra.Command { return cmd } +func renderVersionOptionText(v *management.ActionVersion) string { + deployed := "" + + if v.Deployed { + deployed = "[DEPLOYED]" + } + + return fmt.Sprintf("#%d %s %s", v.Number, v.ID, deployed) +} + +func askVersion(cli *cli, actionId string) (string, error) { + // var versionId string + versions, err := cli.api.ActionVersion.List(actionId) + if err != nil { + return "", err + } + + optChoices := make(map[string]string) + options := make([]string, 0) + options = append(options, "Draft") + optChoices["Draft"] = "draft" + + for _, v := range versions.Versions { + optText := renderVersionOptionText(v) + optChoices[optText] = v.ID + options = append(options, optText) + } + + var versionLabel string + if err = prompt.AskOne(prompt.SelectInput("Actions version", "Choose a version", "Select the version number you want to choose for this action", options, true), &versionLabel); err != nil { + return "", err + } + + return optChoices[versionLabel], nil +} + func downloadActionCmd(cli *cli) *cobra.Command { var flags struct { ID string @@ -250,11 +287,11 @@ func downloadActionCmd(cli *cli) *cobra.Command { } if shouldPrompt(cmd, actionVersion) { - input := prompt.TextInputDefault(actionVersion, "Version Id:", "Version ID of the action to deploy or draft. Default: draft", "draft", false) - - if err := prompt.AskOne(input, &flags); err != nil { + version, err := askVersion(cli, flags.ID) + if err != nil { return err } + flags.Version = version } if shouldPrompt(cmd, actionPath) { @@ -307,7 +344,7 @@ func downloadActionCmd(cli *cli) *cobra.Command { } cmd.Flags().StringVarP(&flags.ID, actionID, "i", "", "Action ID to download.") - cmd.Flags().StringVarP(&flags.Version, actionVersion, "v", "draft", "Version ID of the action to deploy or draft. Default: draft") + cmd.Flags().StringVarP(&flags.Version, actionVersion, "v", "", "Version ID of the action to deploy or draft. Default: draft") cmd.Flags().StringVarP(&flags.Path, actionPath, "p", "./", "Path to save the action content.") mustRequireFlags(cmd, actionID) @@ -499,7 +536,7 @@ func updateActionCmd(cli *cli) *cobra.Command { Long: `$ auth0 actions update Updates an existing action: - $ auth0 actions update --name --file action.js --dependency lodash@4.17.19 + $ auth0 actions update --name --file action.js --dependency lodash@4.17.19 `, RunE: func(cmd *cobra.Command, args []string) error { if shouldPrompt(cmd, actionFile) && shouldPrompt(cmd, actionScript) { @@ -561,15 +598,15 @@ Updates an existing action: return cmd } -func showTriggerCmd(cli *cli) *cobra.Command { +func showFlowCmd(cli *cli) *cobra.Command { var flags struct { Trigger string } cmd := &cobra.Command{ Use: "show", - Short: "Show actions by trigger", - Long: `auth0 actions triggers show --trigger post-login`, + Short: "Shows actions by flow", + Long: `auth0 actions flows --trigger post-login`, PreRun: func(cmd *cobra.Command, args []string) { prepareInteractivity(cmd) }, @@ -613,16 +650,16 @@ func showTriggerCmd(cli *cli) *cobra.Command { return cmd } -func reorderTriggerCmd(cli *cli) *cobra.Command { +func updateFlowCmd(cli *cli) *cobra.Command { var flags struct { File string Trigger string } cmd := &cobra.Command{ - Use: "reorder", - Short: "Reorder actions by trigger", - Long: `auth0 actions triggers reorder --trigger --file `, + Use: "update", + Short: "Update actions by flow", + Long: `auth0 actions flows update --trigger --file `, PreRun: func(cmd *cobra.Command, args []string) { prepareInteractivity(cmd) }, @@ -660,7 +697,7 @@ func reorderTriggerCmd(cli *cli) *cobra.Command { return err } - err = ansi.Spinner("Reordering actions", func() (err error) { + err = ansi.Spinner("Updating actions", func() (err error) { if _, err = cli.api.ActionBinding.Update(triggerID, list.Bindings); err != nil { return err } @@ -686,16 +723,16 @@ func reorderTriggerCmd(cli *cli) *cobra.Command { return cmd } -func createTriggerCmd(cli *cli) *cobra.Command { +func bindActionCmd(cli *cli) *cobra.Command { var flags struct { Action string Trigger string } cmd := &cobra.Command{ - Use: "create", - Short: "Bind an action to a trigger", - Long: `auth0 actions triggers create --trigger --action `, + Use: "bind", + Short: "Bind an action to a flow", + Long: `auth0 actions bind --trigger --action `, PreRun: func(cmd *cobra.Command, args []string) { prepareInteractivity(cmd) }, diff --git a/internal/display/actions.go b/internal/display/actions.go index 66fec75d2..4959e05ef 100644 --- a/internal/display/actions.go +++ b/internal/display/actions.go @@ -1,6 +1,7 @@ package display import ( + "fmt" "strings" "github.com/auth0/auth0-cli/internal/ansi" @@ -38,20 +39,22 @@ func (v *triggerView) AsTableRow() []string { } type actionVersionView struct { + Number string ID string ActionID string ActionName string Runtime string Status string + Deployed string CreatedAt string } func (v *actionVersionView) AsTableHeader() []string { - return []string{"ID", "Action ID", "Action Name", "Runtime", "Status", "Created At"} + return []string{"Number", "ID", "Action ID", "Action Name", "Runtime", "Status", "Created At", "Deployed"} } func (v *actionVersionView) AsTableRow() []string { - return []string{v.getID(), v.ActionID, v.ActionName, v.Runtime, v.Status, v.CreatedAt} + return []string{v.Number, v.getID(), v.ActionID, v.ActionName, v.Runtime, v.Status, v.CreatedAt, v.Deployed} } func (v *actionVersionView) getID() string { @@ -124,17 +127,28 @@ func (r *Renderer) ActionTriggersList(bindings []*management.ActionBinding) { r.Results(res) } -func (r *Renderer) ActionVersion(version *management.ActionVersion) { - r.Heading(ansi.Bold(r.Tenant), "action version\n") +func ActionVersionView(version *management.ActionVersion) *actionVersionView { + deployed := "" + if version.Deployed { + deployed = "✓" + } - v := &actionVersionView{ + return &actionVersionView{ + Number: fmt.Sprint(version.Number), ID: version.ID, ActionID: auth0.StringValue(version.Action.ID), ActionName: auth0.StringValue(version.Action.Name), Runtime: auth0.StringValue(&version.Runtime), Status: string(version.Status), + Deployed: deployed, CreatedAt: timeAgo(auth0.TimeValue(version.CreatedAt)), } +} + +func (r *Renderer) ActionVersion(version *management.ActionVersion) { + r.Heading(ansi.Bold(r.Tenant), "action version\n") + + v := ActionVersionView(version) r.Results([]View{v}) } @@ -144,14 +158,7 @@ func (r *Renderer) ActionVersionList(list []*management.ActionVersion) { var res []View for _, version := range list { - res = append(res, &actionVersionView{ - ID: auth0.StringValue(&version.ID), - ActionID: auth0.StringValue(version.Action.ID), - ActionName: auth0.StringValue(version.Action.Name), - Runtime: auth0.StringValue(&version.Runtime), - Status: string(version.Status), - CreatedAt: timeAgo(auth0.TimeValue(version.CreatedAt)), - }) + res = append(res, ActionVersionView(version)) } r.Results(res) diff --git a/vendor/gopkg.in/auth0.v5/management/actions.go b/vendor/gopkg.in/auth0.v5/management/actions.go index b591ff390..c5a5b1969 100644 --- a/vendor/gopkg.in/auth0.v5/management/actions.go +++ b/vendor/gopkg.in/auth0.v5/management/actions.go @@ -42,6 +42,7 @@ type ActionVersion struct { Action *Action `json:"action,omitempty"` Code string `json:"code,omitempty"` Dependencies []Dependency `json:"dependencies"` + Deployed bool `json:"deployed,omitempty"` Runtime string `json:"runtime,omitempty"` Status VersionStatus `json:"status,omitempty"` Number int `json:"number,omitempty"` diff --git a/vendor/modules.txt b/vendor/modules.txt index a15b7672d..5e30de075 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -178,7 +178,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.20210127122814-819354e637e9 +# gopkg.in/auth0.v5 v5.8.0 => github.com/go-auth0/auth0 v1.3.1-0.20210127175916-f1d24c8c0f88 ## explicit gopkg.in/auth0.v5 gopkg.in/auth0.v5/internal/client @@ -189,4 +189,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.20210127122814-819354e637e9 +# gopkg.in/auth0.v5 => github.com/go-auth0/auth0 v1.3.1-0.20210127175916-f1d24c8c0f88