From 9d6ee1dda3992f462e60512429bf5caccbb31264 Mon Sep 17 00:00:00 2001 From: Danny Date: Tue, 26 Jan 2021 12:30:43 -0500 Subject: [PATCH] feat: actions triggers reorder (#37) --- internal/auth0/actions.go | 1 + internal/cli/actions.go | 79 ++++++++++++++++++++++++++++++------- internal/display/actions.go | 6 ++- 3 files changed, 69 insertions(+), 17 deletions(-) diff --git a/internal/auth0/actions.go b/internal/auth0/actions.go index 4ac3628cd..a9af35768 100644 --- a/internal/auth0/actions.go +++ b/internal/auth0/actions.go @@ -21,4 +21,5 @@ type ActionVersionAPI interface { type ActionBindingAPI interface { List(triggerID management.TriggerID, opts ...management.RequestOption) (c *management.ActionBindingList, err error) + Update(triggerID management.TriggerID, v *management.ActionBindingList) error } diff --git a/internal/cli/actions.go b/internal/cli/actions.go index df8e2e658..270fc55f9 100644 --- a/internal/cli/actions.go +++ b/internal/cli/actions.go @@ -36,6 +36,7 @@ func triggersCmd(cli *cli) *cobra.Command { cmd.SetUsageTemplate(resourceUsageTemplate()) cmd.AddCommand(showTriggerCmd(cli)) + cmd.AddCommand(reorderTriggerCmd(cli)) return cmd } @@ -63,6 +64,28 @@ Lists your existing actions. To create one try: return cmd } +func readJsonFile(filePath string, out interface{}) error { + // Open our jsonFile + jsonFile, err := os.Open(filePath) + // if we os.Open returns an error then handle it + if err != nil { + return err + } + // defer the closing of our jsonFile so that we can parse it later on + defer jsonFile.Close() + + byteValue, err := ioutil.ReadAll(jsonFile) + if err != nil { + return err + } + + if err := json.Unmarshal(byteValue, out); err != nil { + return err + } + + return nil +} + func testActionCmd(cli *cli) *cobra.Command { var actionId string var versionId string @@ -74,27 +97,13 @@ func testActionCmd(cli *cli) *cobra.Command { Short: "Test an action draft against a payload", Long: `$ auth0 actions test --name --file `, RunE: func(cmd *cobra.Command, args []string) error { - // Open our jsonFile - jsonFile, err := os.Open(payloadFile) - // if we os.Open returns an error then handle it - if err != nil { - return err - } - // defer the closing of our jsonFile so that we can parse it later on - defer jsonFile.Close() - - byteValue, err := ioutil.ReadAll(jsonFile) + err := readJsonFile(payloadFile, &payload) if err != nil { return err } - if err := json.Unmarshal(byteValue, &payload); err != nil { - return err - } - var result management.Object err = ansi.Spinner(fmt.Sprintf("Testing action: %s, version: %s", actionId, versionId), func() error { - fmt.Println(payload) result, err = cli.api.ActionVersion.Test(actionId, versionId, payload) return err }) @@ -207,3 +216,43 @@ func showTriggerCmd(cli *cli) *cobra.Command { return cmd } + +func reorderTriggerCmd(cli *cli) *cobra.Command { + var trigger string + var bindingsFile string + + cmd := &cobra.Command{ + Use: "reorder", + Short: "Reorders actions by trigger", + Long: `$ auth0 actions triggers reorder --trigger --file `, + RunE: func(cmd *cobra.Command, args []string) error { + if err := validators.TriggerID(trigger); err != nil { + return err + } + + triggerID := management.TriggerID(trigger) + + var list *management.ActionBindingList + err := readJsonFile(bindingsFile, &list) + if err != nil { + return err + } + + err = ansi.Spinner("Loading actions", func() error { + return cli.api.ActionBinding.Update(triggerID, list) + }) + + if err != nil { + return err + } + + cli.renderer.ActionTriggersList(list.Bindings) + return nil + }, + } + + cmd.Flags().StringVarP(&trigger, "trigger", "t", string(management.PostLogin), "Trigger type for action.") + cmd.Flags().StringVarP(&bindingsFile, "file", "f", "", "File containing the bindings") + + return cmd +} diff --git a/internal/display/actions.go b/internal/display/actions.go index ac22b3a65..95dab9588 100644 --- a/internal/display/actions.go +++ b/internal/display/actions.go @@ -24,16 +24,17 @@ func (v *actionView) AsTableRow() []string { } type triggerView struct { + ID string ActionID string DisplayName string } func (v *triggerView) AsTableHeader() []string { - return []string{"Action ID", "Action Name"} + return []string{"ID", "Action ID", "Action Name"} } func (v *triggerView) AsTableRow() []string { - return []string{v.ActionID, v.DisplayName} + return []string{v.ID, v.ActionID, v.DisplayName} } func (r *Renderer) ActionList(actions []*management.Action) { @@ -88,6 +89,7 @@ func (r *Renderer) ActionTriggersList(bindings []*management.ActionBinding) { var res []View for _, b := range bindings { res = append(res, &triggerView{ + ID: auth0.StringValue(b.ID), ActionID: auth0.StringValue(b.Action.ID), DisplayName: auth0.StringValue(b.DisplayName), })