From cc9fba7adfecc9ad737a5e5f86b28da35ef0d397 Mon Sep 17 00:00:00 2001 From: Ralph Slooten Date: Wed, 21 Sep 2022 15:41:43 +1200 Subject: [PATCH] Fix: Normalize running binary name detection (Windows) This prevents invoking sendmail when the executed name differs from the actual binary name (eg: running `mailpit` instead of `mailpit.exe`). See #14 --- main.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index cfbbce4..7371c1f 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "os" "path/filepath" + "strings" "github.com/axllent/mailpit/cmd" sendmail "github.com/axllent/mailpit/sendmail/cmd" @@ -15,10 +16,19 @@ func main() { } // running directly - if filepath.Base(exec) == filepath.Base(os.Args[0]) { + if normalize(filepath.Base(exec)) == normalize(filepath.Base(os.Args[0])) { cmd.Execute() } else { // symlinked sendmail.Run() } } + +// Normalize returns a lowercase string stripped of the file extension (if exists). +// Used for detecting Windows commands which ignores letter casing and `.exe`. +// eg: "MaIlpIT.Exe" returns "mailpit" +func normalize(s string) string { + s = strings.ToLower(s) + + return strings.TrimSuffix(s, filepath.Ext(s)) +}