Skip to content

Commit

Permalink
feat: actions triggers reorder (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
turcottedanny authored Jan 26, 2021
1 parent 2e2020e commit 9d6ee1d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 17 deletions.
1 change: 1 addition & 0 deletions internal/auth0/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
79 changes: 64 additions & 15 deletions internal/cli/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func triggersCmd(cli *cli) *cobra.Command {

cmd.SetUsageTemplate(resourceUsageTemplate())
cmd.AddCommand(showTriggerCmd(cli))
cmd.AddCommand(reorderTriggerCmd(cli))

return cmd
}
Expand Down Expand Up @@ -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
Expand All @@ -74,27 +97,13 @@ func testActionCmd(cli *cli) *cobra.Command {
Short: "Test an action draft against a payload",
Long: `$ auth0 actions test --name <actionid> --file <payload.json>`,
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
})
Expand Down Expand Up @@ -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 <post-login> --file <bindings.json>`,
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
}
6 changes: 4 additions & 2 deletions internal/display/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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),
})
Expand Down

0 comments on commit 9d6ee1d

Please sign in to comment.