Simplify some code

This commit is contained in:
Andrew Ayer 2023-10-29 07:45:23 -04:00
parent f8040df68d
commit 5fae49a971
3 changed files with 7 additions and 8 deletions

View File

@ -201,16 +201,11 @@ func main() {
Verbose: flags.verbose,
Script: flags.script,
ScriptDir: defaultScriptDir(),
SendmailPath: "/usr/sbin/sendmail",
Email: flags.email,
Stdout: flags.stdout,
HealthCheckInterval: flags.healthcheck,
}
if envVar := os.Getenv("SENDMAIL_PATH"); envVar != "" {
config.SendmailPath = envVar
}
emailFileExists := false
if emailRecipients, err := readEmailFile(defaultEmailFile()); err == nil {
emailFileExists = true

View File

@ -20,7 +20,6 @@ type Config struct {
WatchList WatchList
Verbose bool
SaveCerts bool
SendmailPath string
Script string
ScriptDir string
Email []string

View File

@ -36,7 +36,7 @@ func notify(ctx context.Context, config *Config, notif notification) error {
}
if len(config.Email) > 0 {
if err := sendEmail(ctx, config.SendmailPath, config.Email, notif); err != nil {
if err := sendEmail(ctx, config.Email, notif); err != nil {
return err
}
}
@ -62,10 +62,15 @@ func writeToStdout(notif notification) {
os.Stdout.WriteString(notif.Text() + "\n")
}
func sendEmail(ctx context.Context, sendmailPath string, to []string, notif notification) error {
func sendEmail(ctx context.Context, to []string, notif notification) error {
stdin := new(bytes.Buffer)
stderr := new(bytes.Buffer)
sendmailPath := "/usr/sbin/sendmail"
if envVar := os.Getenv("SENDMAIL_PATH"); envVar != "" {
sendmailPath = envVar
}
fmt.Fprintf(stdin, "To: %s\n", strings.Join(to, ", "))
fmt.Fprintf(stdin, "Subject: [certspotter] %s\n", notif.Summary())
fmt.Fprintf(stdin, "Mime-Version: 1.0\n")