git » chasquid » commit f51b449

chasquid: Move the certificate loading logic in a separate function

author Alberto Bertogli
2023-09-17 09:58:25 UTC
committer Alberto Bertogli
2023-09-17 09:58:25 UTC
parent b9e222f6eb436be5ee2e4c513dd49b627ae5ca05

chasquid: Move the certificate loading logic in a separate function

This patch moves the top-level certificate loading logic out of main and
into a separate function.

This is only for readability and consistency with how we handle domains
(which have a similar structure already). There are no logic changes.

chasquid.go +24 -19

diff --git a/chasquid.go b/chasquid.go
index c1ade7d..6e4f1ec 100644
--- a/chasquid.go
+++ b/chasquid.go
@@ -99,25 +99,7 @@ func main() {
 
 		name := info.Name()
 		dir := filepath.Join("certs/", name)
-		log.Infof("  %s", name)
-
-		// Ignore directories that don't have both keys.
-		// We warn about this because it can be hard to debug otherwise.
-		certPath := filepath.Join(dir, "fullchain.pem")
-		if _, err := os.Stat(certPath); err != nil {
-			log.Infof("    skipping: %v", err)
-			continue
-		}
-		keyPath := filepath.Join(dir, "privkey.pem")
-		if _, err := os.Stat(keyPath); err != nil {
-			log.Infof("    skipping: %v", err)
-			continue
-		}
-
-		err := s.AddCerts(certPath, keyPath)
-		if err != nil {
-			log.Fatalf("    %v", err)
-		}
+		loadCert(name, dir, s)
 	}
 
 	// Load domains from "domains/".
@@ -265,6 +247,29 @@ func signalHandler(dinfo *domaininfo.DB, srv *smtpsrv.Server) {
 	}
 }
 
+// Helper to load a single certificate configuration into the server.
+func loadCert(name, dir string, s *smtpsrv.Server) {
+	log.Infof("  %s", name)
+
+	// Ignore directories that don't have both keys.
+	// We warn about this because it can be hard to debug otherwise.
+	certPath := filepath.Join(dir, "fullchain.pem")
+	if _, err := os.Stat(certPath); err != nil {
+		log.Infof("    skipping: %v", err)
+		return
+	}
+	keyPath := filepath.Join(dir, "privkey.pem")
+	if _, err := os.Stat(keyPath); err != nil {
+		log.Infof("    skipping: %v", err)
+		return
+	}
+
+	err := s.AddCerts(certPath, keyPath)
+	if err != nil {
+		log.Fatalf("    %v", err)
+	}
+}
+
 // Helper to load a single domain configuration into the server.
 func loadDomain(name, dir string, s *smtpsrv.Server) {
 	log.Infof("  %s", name)