From af39c252f3c0bcebd52ca109893c0087440fd821 Mon Sep 17 00:00:00 2001 From: Sergiu Ghitea <28300158+sergiught@users.noreply.github.com> Date: Wed, 18 Jan 2023 12:08:49 +0100 Subject: [PATCH 1/3] Allow updating an email template when it does not exist --- internal/auth0/email_template.go | 5 +++++ internal/cli/email_templates.go | 28 ++++++++++++++++++++++------ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/internal/auth0/email_template.go b/internal/auth0/email_template.go index 205445243..6c71949cc 100644 --- a/internal/auth0/email_template.go +++ b/internal/auth0/email_template.go @@ -3,6 +3,11 @@ package auth0 import "github.com/auth0/go-auth0/management" type EmailTemplateAPI interface { + // Create an email template. + // + // See: https://auth0.com/docs/api/management/v2#!/Email_Templates/post_email_templates + Create(template *management.EmailTemplate, opts ...management.RequestOption) error + // Read an email template by pre-defined name. // // These names are `verify_email`, `reset_email`, `welcome_email`, diff --git a/internal/cli/email_templates.go b/internal/cli/email_templates.go index 5b7e7781d..48c47a45e 100644 --- a/internal/cli/email_templates.go +++ b/internal/cli/email_templates.go @@ -2,11 +2,13 @@ package cli import ( "fmt" + "net/http" "github.com/auth0/go-auth0/management" "github.com/spf13/cobra" "github.com/auth0/auth0-cli/internal/ansi" + "github.com/auth0/auth0-cli/internal/auth0" "github.com/auth0/auth0-cli/internal/prompt" ) @@ -133,13 +135,11 @@ func showEmailTemplateCmd(cli *cli) *cobra.Command { } var email *management.EmailTemplate - - if err := ansi.Waiting(func() error { - var err error + if err := ansi.Waiting(func() (err error) { email, err = cli.api.EmailTemplate.Read(apiEmailTemplateFor(inputs.Template)) return err }); err != nil { - return fmt.Errorf("Unable to get the email template '%s': %w", inputs.Template, err) + return fmt.Errorf("failed to get the email template '%s': %w", inputs.Template, err) } cli.renderer.EmailTemplateShow(email) @@ -192,12 +192,23 @@ func updateEmailTemplateCmd(cli *cli) *cobra.Command { } var oldTemplate *management.EmailTemplate + templateExists := true err := ansi.Waiting(func() (err error) { oldTemplate, err = cli.api.EmailTemplate.Read(apiEmailTemplateFor(inputs.Template)) return err }) if err != nil { - return fmt.Errorf("failed to get the email template '%s': %w", inputs.Template, err) + if mErr, ok := err.(management.Error); ok && mErr.Status() != http.StatusNotFound { + return fmt.Errorf("failed to get the email template '%s': %w", inputs.Template, err) + } else { + templateExists = false + oldTemplate = &management.EmailTemplate{ + From: auth0.String(""), + Subject: auth0.String(""), + Enabled: auth0.Bool(false), + Syntax: auth0.String("liquid"), + } + } } if err := emailTemplateFrom.AskU(cmd, &inputs.From, oldTemplate.From); err != nil { @@ -234,6 +245,7 @@ func updateEmailTemplateCmd(cli *cli) *cobra.Command { emailTemplate := &management.EmailTemplate{ Enabled: &inputs.Enabled, Template: &template, + Syntax: oldTemplate.Syntax, } if inputs.Body != "" { emailTemplate.Body = &inputs.Body @@ -252,7 +264,11 @@ func updateEmailTemplateCmd(cli *cli) *cobra.Command { } if err = ansi.Waiting(func() error { - return cli.api.EmailTemplate.Update(template, emailTemplate) + if templateExists { + return cli.api.EmailTemplate.Update(template, emailTemplate) + } + + return cli.api.EmailTemplate.Create(emailTemplate) }); err != nil { return fmt.Errorf("failed to update the email template '%s': %w", inputs.Template, err) } From ed569733b372e6c56196324d731562b99cc4c1d2 Mon Sep 17 00:00:00 2001 From: Sergiu Ghitea <28300158+sergiught@users.noreply.github.com> Date: Wed, 18 Jan 2023 14:02:15 +0100 Subject: [PATCH 2/3] Improve error handling on email templates update --- internal/cli/email_templates.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/internal/cli/email_templates.go b/internal/cli/email_templates.go index 48c47a45e..75238b5d1 100644 --- a/internal/cli/email_templates.go +++ b/internal/cli/email_templates.go @@ -198,8 +198,15 @@ func updateEmailTemplateCmd(cli *cli) *cobra.Command { return err }) if err != nil { - if mErr, ok := err.(management.Error); ok && mErr.Status() != http.StatusNotFound { - return fmt.Errorf("failed to get the email template '%s': %w", inputs.Template, err) + wrappedError := fmt.Errorf("failed to get the email template '%s': %w", inputs.Template, err) + + mErr, ok := err.(management.Error) + if !ok { + return wrappedError + } + + if mErr.Status() != http.StatusNotFound { + return wrappedError } else { templateExists = false oldTemplate = &management.EmailTemplate{ From ad1086eb87d633b6dce8352acf8b045624b2c281 Mon Sep 17 00:00:00 2001 From: Sergiu Ghitea <28300158+sergiught@users.noreply.github.com> Date: Wed, 18 Jan 2023 17:45:25 +0100 Subject: [PATCH 3/3] Refactor if statement --- internal/cli/email_templates.go | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/internal/cli/email_templates.go b/internal/cli/email_templates.go index 75238b5d1..ccfb50281 100644 --- a/internal/cli/email_templates.go +++ b/internal/cli/email_templates.go @@ -198,23 +198,17 @@ func updateEmailTemplateCmd(cli *cli) *cobra.Command { return err }) if err != nil { - wrappedError := fmt.Errorf("failed to get the email template '%s': %w", inputs.Template, err) - mErr, ok := err.(management.Error) - if !ok { - return wrappedError + if !ok || mErr.Status() != http.StatusNotFound { + return fmt.Errorf("failed to get the email template '%s': %w", inputs.Template, err) } - if mErr.Status() != http.StatusNotFound { - return wrappedError - } else { - templateExists = false - oldTemplate = &management.EmailTemplate{ - From: auth0.String(""), - Subject: auth0.String(""), - Enabled: auth0.Bool(false), - Syntax: auth0.String("liquid"), - } + templateExists = false + oldTemplate = &management.EmailTemplate{ + From: auth0.String(""), + Subject: auth0.String(""), + Enabled: auth0.Bool(false), + Syntax: auth0.String("liquid"), } }