From ea6efa2b3938531151cb1974aaf9160b98b68e24 Mon Sep 17 00:00:00 2001 From: Jeremy West Date: Wed, 27 Jan 2021 12:43:31 -0600 Subject: [PATCH] feat: support updating an action with new versions (#75) * feat: support updating an action with new versions * add prompt to action update cmd, fix action version display issue --- internal/cli/actions.go | 142 +++++++++++++++++++++++++++++------- internal/display/actions.go | 2 +- 2 files changed, 117 insertions(+), 27 deletions(-) diff --git a/internal/cli/actions.go b/internal/cli/actions.go index 686312656..461789657 100644 --- a/internal/cli/actions.go +++ b/internal/cli/actions.go @@ -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)) @@ -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 { @@ -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 }) @@ -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 --file action.js --dependency lodash@4.17.19 +`, + 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 (@).") + // 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 @@ -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 +} diff --git a/internal/display/actions.go b/internal/display/actions.go index 7333ecb19..66fec75d2 100644 --- a/internal/display/actions.go +++ b/internal/display/actions.go @@ -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)),