Skip to content

Commit

Permalink
DXCDT-323: Allow updating an email template when it does not exist (#611
Browse files Browse the repository at this point in the history
)
  • Loading branch information
sergiught authored Jan 18, 2023
1 parent 9df2719 commit 7184197
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
5 changes: 5 additions & 0 deletions internal/auth0/email_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
29 changes: 23 additions & 6 deletions internal/cli/email_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -192,12 +192,24 @@ 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)
mErr, ok := err.(management.Error)
if !ok || mErr.Status() != http.StatusNotFound {
return fmt.Errorf("failed to get the email template '%s': %w", inputs.Template, err)
}

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 {
Expand Down Expand Up @@ -234,6 +246,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
Expand All @@ -252,7 +265,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)
}
Expand Down

0 comments on commit 7184197

Please sign in to comment.