From 5fae49a97154bfcaef831d428436f71000eeb862 Mon Sep 17 00:00:00 2001 From: Andrew Ayer Date: Sun, 29 Oct 2023 07:45:23 -0400 Subject: [PATCH] Simplify some code --- cmd/certspotter/main.go | 5 ----- monitor/config.go | 1 - monitor/notify.go | 9 +++++++-- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/cmd/certspotter/main.go b/cmd/certspotter/main.go index 0dae8c9..9730789 100644 --- a/cmd/certspotter/main.go +++ b/cmd/certspotter/main.go @@ -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 diff --git a/monitor/config.go b/monitor/config.go index d1bc430..1e0d60c 100644 --- a/monitor/config.go +++ b/monitor/config.go @@ -20,7 +20,6 @@ type Config struct { WatchList WatchList Verbose bool SaveCerts bool - SendmailPath string Script string ScriptDir string Email []string diff --git a/monitor/notify.go b/monitor/notify.go index 86cabca..f2c51fb 100644 --- a/monitor/notify.go +++ b/monitor/notify.go @@ -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")