Skip to content

Commit

Permalink
feat: Actions improvement (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
turcottedanny authored Jan 27, 2021
1 parent a24b55e commit 730fae8
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 43 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
91 changes: 64 additions & 27 deletions internal/cli/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -499,7 +536,7 @@ func updateActionCmd(cli *cli) *cobra.Command {
Long: `$ auth0 actions update
Updates an existing action:
$ auth0 actions update --name <actionid> --file action.js --dependency [email protected]
$ auth0 actions update --name <actionid> --file action.js --dependency [email protected]
`,
RunE: func(cmd *cobra.Command, args []string) error {
if shouldPrompt(cmd, actionFile) && shouldPrompt(cmd, actionScript) {
Expand Down Expand Up @@ -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)
},
Expand Down Expand Up @@ -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 <post-login> --file <bindings.json>`,
Use: "update",
Short: "Update actions by flow",
Long: `auth0 actions flows update --trigger <post-login> --file <bindings.json>`,
PreRun: func(cmd *cobra.Command, args []string) {
prepareInteractivity(cmd)
},
Expand Down Expand Up @@ -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
}
Expand All @@ -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 <post-login> --action <action_id>`,
Use: "bind",
Short: "Bind an action to a flow",
Long: `auth0 actions bind --trigger <post-login> --action <action_id>`,
PreRun: func(cmd *cobra.Command, args []string) {
prepareInteractivity(cmd)
},
Expand Down
33 changes: 20 additions & 13 deletions internal/display/actions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package display

import (
"fmt"
"strings"

"github.com/auth0/auth0-cli/internal/ansi"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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})
}
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions vendor/gopkg.in/auth0.v5/management/actions.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 730fae8

Please sign in to comment.