Skip to content

Commit

Permalink
feat: support updating an action with new versions (#75)
Browse files Browse the repository at this point in the history
* feat: support updating an action with new versions

* add prompt to action update cmd, fix action version display issue
  • Loading branch information
iamjem authored Jan 27, 2021
1 parent 84435d6 commit ea6efa2
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 27 deletions.
142 changes: 116 additions & 26 deletions internal/cli/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func actionsCmd(cli *cli) *cobra.Command {
cmd.AddCommand(listActionsCmd(cli))
cmd.AddCommand(testActionCmd(cli))
cmd.AddCommand(createActionCmd(cli))
cmd.AddCommand(updateActionCmd(cli))
cmd.AddCommand(deployActionCmd(cli))
cmd.AddCommand(downloadActionCmd(cli))
cmd.AddCommand(listActionVersionsCmd(cli))
Expand Down Expand Up @@ -399,7 +400,7 @@ Create a new action:
}
}

if shouldPrompt(cmd, actionFile) {
if shouldPrompt(cmd, actionFile) && shouldPrompt(cmd, actionScript) {
input := prompt.TextInput(actionFile, "Action File:", "File containing the action source code.", false)

if err := prompt.AskOne(input, &flags); err != nil {
Expand Down Expand Up @@ -447,33 +448,13 @@ Create a new action:
return err
}

if flags.CreateVersion {
if err := cli.api.ActionVersion.Create(auth0.StringValue(action.ID), version); err != nil {
return err
}

// TODO(iamjem): this is a hack since the SDK won't decode 202 responses
list, err := cli.api.ActionVersion.List(auth0.StringValue(action.ID))
if err != nil {
return err
}

if len(list.Versions) > 0 {
version = list.Versions[0]
}
} else {
if err := cli.api.ActionVersion.UpsertDraft(auth0.StringValue(action.ID), version); err != nil {
return err
}

// TODO(iamjem): this is a hack since the SDK won't decode 202 responses
draft, err := cli.api.ActionVersion.ReadDraft(auth0.StringValue(action.ID))
if err != nil {
return err
}
version = draft
created, err := createActionVersion(cli.api, auth0.StringValue(action.ID), !flags.CreateVersion, version)
if err != nil {
return err
}

version = created

return nil
})

Expand Down Expand Up @@ -503,6 +484,83 @@ Create a new action:
return cmd
}

func updateActionCmd(cli *cli) *cobra.Command {
var flags struct {
Name string
File string
Script string
Dependency []string
CreateVersion bool
}

cmd := &cobra.Command{
Use: "update",
Short: "Updates an existing action",
Long: `$ auth0 actions update
Updates an existing action:
$ 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) {
input := prompt.TextInput(actionFile, "Action File:", "File containing the action source code.", false)

if err := prompt.AskOne(input, &flags); err != nil {
return err
}
}

source, err := sourceFromFileOrScript(flags.File, flags.Script)
if err != nil {
return err
}

dependencies, err := validators.Dependencies(flags.Dependency)
if err != nil {
return err
}

version := &management.ActionVersion{
Code: source,
Dependencies: dependencies,
Runtime: "node12",
}

err = ansi.Spinner("Updating action", func() error {
created, err := createActionVersion(cli.api, flags.Name, !flags.CreateVersion, version)
if err != nil {
return err
}

version = created
return nil
})

if err != nil {
return err
}

cli.renderer.ActionVersion(version)

return nil
},
}

cmd.Flags().StringVarP(&flags.Name, actionName, "n", "", "Action ID to update.")
cmd.Flags().StringVarP(&flags.File, actionFile, "f", "", "File containing the action source code.")
cmd.Flags().StringVarP(&flags.Script, actionScript, "s", "", "Raw source code for the action.")
cmd.Flags().StringSliceVarP(&flags.Dependency, actionDependency, "d", nil, "Dependency for the source code (<name>@<semver>).")
// TODO: This name is kind of overloaded since it could also refer to the version of the trigger (though there's only v1's at this time)
cmd.Flags().BoolVarP(&flags.CreateVersion, actionVersion, "v", false, "Create an explicit action version from the source code instead of a draft.")

mustRequireFlags(cmd, actionName)
if err := cmd.MarkFlagFilename(actionFile); err != nil {
panic(err)
}

return cmd
}

func showTriggerCmd(cli *cli) *cobra.Command {
var flags struct {
Trigger string
Expand Down Expand Up @@ -738,3 +796,35 @@ func sourceFromFileOrScript(file, script string) (string, error) {

return "", errNoSource
}

func createActionVersion(api *auth0.API, actionID string, draft bool, version *management.ActionVersion) (*management.ActionVersion, error) {
var v *management.ActionVersion
if draft {
if err := api.ActionVersion.UpsertDraft(actionID, version); err != nil {
return nil, err
}

// TODO(iamjem): this is a hack since the SDK won't decode 202 responses
draft, err := api.ActionVersion.ReadDraft(actionID)
if err != nil {
return nil, err
}
v = draft
} else {
if err := api.ActionVersion.Create(actionID, version); err != nil {
return nil, err
}

// TODO(iamjem): this is a hack since the SDK won't decode 202 responses
list, err := api.ActionVersion.List(actionID)
if err != nil {
return nil, err
}

if len(list.Versions) > 0 {
v = list.Versions[0]
}
}

return v, nil
}
2 changes: 1 addition & 1 deletion internal/display/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (r *Renderer) ActionVersion(version *management.ActionVersion) {
v := &actionVersionView{
ID: version.ID,
ActionID: auth0.StringValue(version.Action.ID),
ActionName: 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)),
Expand Down

0 comments on commit ea6efa2

Please sign in to comment.