Skip to content

Commit

Permalink
Merge branch 'main' into feature/delete-confirmation
Browse files Browse the repository at this point in the history
  • Loading branch information
Widcket authored Jan 26, 2021
2 parents 4ea5624 + 9d6ee1d commit 9436a1b
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 18 deletions.
3 changes: 0 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.1 h1:JFrFEBb2xKufg6XkJsJr+WbKb4FQlURi5RUcBveYu9k=
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
Expand Down Expand Up @@ -165,7 +164,6 @@ github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvW
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty v1.1.4/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand Down Expand Up @@ -500,7 +498,6 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
Expand Down
5 changes: 5 additions & 0 deletions internal/auth0/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ type ActionVersionAPI interface {
Delete(actionID string, id string, opts ...management.RequestOption) error
Test(actionID string, id string, payload management.Object) (management.Object, error)
}

type ActionBindingAPI interface {
List(triggerID management.TriggerID, opts ...management.RequestOption) (c *management.ActionBindingList, err error)
Update(triggerID management.TriggerID, v *management.ActionBindingList) error
}
2 changes: 2 additions & 0 deletions internal/auth0/auth0.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
type API struct {
Action ActionAPI
ActionVersion ActionVersionAPI
ActionBinding ActionBindingAPI
Client ClientAPI
Connection ConnectionAPI
Log LogAPI
Expand All @@ -21,6 +22,7 @@ func NewAPI(m *management.Management) *API {
return &API{
Action: m.Action,
ActionVersion: m.ActionVersion,
ActionBinding: m.ActionBinding,
Client: m.Client,
Connection: m.Connection,
Log: m.Log,
Expand Down
127 changes: 112 additions & 15 deletions internal/cli/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ func actionsCmd(cli *cli) *cobra.Command {
cmd.AddCommand(listActionsCmd(cli))
cmd.AddCommand(testActionCmd(cli))
cmd.AddCommand(createActionCmd(cli))
cmd.AddCommand(triggersCmd(cli))

return cmd
}

func triggersCmd(cli *cli) *cobra.Command {
cmd := &cobra.Command{
Use: "triggers",
Short: "manage resources for action triggers.",
}

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

return cmd
}
Expand Down Expand Up @@ -50,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 @@ -61,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
err := readJsonFile(payloadFile, &payload)
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([]byte(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 @@ -159,3 +181,78 @@ Creates a new action:

return cmd
}

func showTriggerCmd(cli *cli) *cobra.Command {
var trigger string

cmd := &cobra.Command{
Use: "show",
Short: "Show actions by trigger",
Long: `$ auth0 actions triggers show --trigger post-login`,
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 := ansi.Spinner("Loading actions", func() error {
var err error
list, err = cli.api.ActionBinding.List(triggerID)
return err
})

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.")

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
}
30 changes: 30 additions & 0 deletions internal/display/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ func (v *actionView) AsTableRow() []string {
return []string{v.ID, v.Name, v.Type, v.CreatedAt}
}

type triggerView struct {
ID string
ActionID string
DisplayName string
}

func (v *triggerView) AsTableHeader() []string {
return []string{"ID", "Action ID", "Action Name"}
}

func (v *triggerView) AsTableRow() []string {
return []string{v.ID, v.ActionID, v.DisplayName}
}

func (r *Renderer) ActionList(actions []*management.Action) {
r.Heading(ansi.Bold(r.Tenant), "actions\n")

Expand Down Expand Up @@ -68,3 +82,19 @@ func (r *Renderer) ActionCreate(action *management.Action) {

r.Results([]View{v})
}

func (r *Renderer) ActionTriggersList(bindings []*management.ActionBinding) {
r.Heading(ansi.Bold(r.Tenant), "triggers\n")

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),
})

}

r.Results(res)
}

0 comments on commit 9436a1b

Please sign in to comment.