infra/cli/email_alias.go

82 lines
2.6 KiB
Go
Raw Normal View History

2021-04-08 22:12:45 +01:00
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/badoux/checkmail"
"github.com/brianvoe/gofakeit/v6"
"github.com/golang/glog"
"github.com/pkg/errors"
)
const (
addEmailAliasUseCase = "add-email-alias"
emailAliasFlagName = "forward-to"
fromEmailDomainFlagName = "from-domain"
emailAliasesConfigFileRelative = "/modules/kubernetes/mailserver/extra/aliases.txt"
)
func addEmailAlias(gitFs *GitFS, to, fromDomain string) (string, error) {
if err := checkmail.ValidateFormat(to); err != nil {
return "", errors.Wrapf(err, fmt.Sprintf("failed to create new email aliases because invalid input format: %s", to))
}
if err := checkmail.ValidateHost(to); err != nil {
return "", errors.Wrapf(err, fmt.Sprintf("failed to create new email aliases because domain for %s does not exist", to))
}
aliasEmail := generateRandomEmail(fromDomain)
2021-04-09 19:56:52 +01:00
glog.Infof("adding %s -> %s alias to %s", aliasEmail, to, emailAliasesConfigFileRelative)
2021-04-08 22:12:45 +01:00
// Read existing contents
fRead, err := (*gitFs.fs).OpenFile(emailAliasesConfigFileRelative, os.O_RDONLY, 0644)
if err != nil {
return "", errors.Wrapf(err, "failed to open file where email aliases are recorded")
}
fileContentsBytes, err := ioutil.ReadAll(fRead)
if err != nil {
return "", errors.Wrapf(err, "failed to read existing aliases file")
}
2021-04-09 20:47:09 +01:00
glog.Infof("current aliases file contents: \n%s", string(fileContentsBytes))
2021-04-09 21:07:50 +01:00
defer fRead.Close()
2021-04-08 22:12:45 +01:00
2021-04-09 21:07:50 +01:00
newContents := getAddedAliasContents(string(fileContentsBytes), aliasEmail, to)
2021-04-08 22:12:45 +01:00
// Write new contents
2021-04-08 22:12:45 +01:00
fWrite, err := (*gitFs.fs).OpenFile(emailAliasesConfigFileRelative, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
2021-04-08 22:12:45 +01:00
if err != nil {
return "", errors.Wrapf(err, "failed to open file where new email alias will be added")
}
2021-04-09 19:56:52 +01:00
glog.Infof("writing new contents to file: \n%s", newContents)
2021-04-09 21:07:50 +01:00
if _, err = fWrite.Write([]byte(newContents)); err != nil {
2021-04-08 22:12:45 +01:00
return "", errors.Wrapf(err, "failed to write config to file")
}
2021-04-09 21:07:50 +01:00
defer fWrite.Close()
2021-04-08 22:12:45 +01:00
return aliasEmail, nil
}
func generateRandomEmail(fromDomain string) string {
return fmt.Sprintf("%s-%s-generated%s", strings.ToLower(gofakeit.Adverb()), strings.ToLower(gofakeit.FirstName()), fromDomain)
}
2021-04-09 19:56:52 +01:00
func getPostfixAlias(from, to string) string {
2021-04-08 22:12:45 +01:00
return fmt.Sprintf("%s %s", from, to)
}
func getAddedAliasContents(currentContents, from, to string) string {
lines := strings.Split(currentContents, "\n")
newLines := []string{}
for _, l := range lines {
2021-04-09 19:56:52 +01:00
l = strings.TrimSpace(l)
if l == "" {
continue
}
if strings.HasSuffix(l, to) {
continue
2021-04-08 22:12:45 +01:00
}
2021-04-09 19:56:52 +01:00
newLines = append(newLines, l)
2021-04-08 22:12:45 +01:00
}
2021-04-09 19:56:52 +01:00
newLines = append(newLines, getPostfixAlias(from, to))
2021-04-08 22:12:45 +01:00
return strings.Join(newLines, "\n") + "\n"
2021-04-08 22:12:45 +01:00
}