Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DXCDT-323: Allow updating an email template when it does not exist #611

Merged
merged 5 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"),
Comment on lines +207 to +211
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Is it necessary to instantiate with empty values here if the emailTemplate gets fully constructed below?

Copy link
Contributor Author

@sergiught sergiught Jan 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly yes 😞 as otherwise this line https://github.com/auth0/auth0-cli/pull/611/files#diff-7c49d78e329a015986302ea95a143d68ca7d933a41514ca964b46bddcd22f31aR215 will panic when accessing oldTemplate.From. We can't replace it with the safe access getter GetFrom due to all the prompts using pointers for default values. That should be refactored in a separate dedicated PR IMO.

}
}

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