From 48c73ae97b5a3c36d2e9c7b6c99ba1ada0b15d9c Mon Sep 17 00:00:00 2001 From: Ralph Slooten Date: Fri, 12 Apr 2024 14:47:47 +1200 Subject: [PATCH] Chore: Switch database flag/env to `--database` / `MP_DATABASE` The original `--db-file` / `MP_DATA_FILE`, although deprecated, won't be removed any time soon to ensure backwards compatibility with existing integrations --- cmd/reindex.go | 2 +- cmd/root.go | 22 ++++++++++++---------- config/config.go | 8 ++++---- internal/stats/stats.go | 2 +- internal/storage/database.go | 4 ++-- internal/storage/testing.go | 2 +- server/server_test.go | 2 +- 7 files changed, 22 insertions(+), 20 deletions(-) diff --git a/cmd/reindex.go b/cmd/reindex.go index e1120c0..3c18c37 100644 --- a/cmd/reindex.go +++ b/cmd/reindex.go @@ -19,7 +19,7 @@ If you have several thousand messages in your mailbox, then it is advised to shu Mailpit while you reindex as this process will likely result in database locking issues.`, Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { - config.DataFile = args[0] + config.Database = args[0] config.MaxMessages = 0 if err := storage.InitDB(); err != nil { diff --git a/cmd/root.go b/cmd/root.go index d88fc54..e38c5dc 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -80,8 +80,8 @@ func init() { // load environment variables initConfigFromEnv() - rootCmd.Flags().StringVarP(&config.DataFile, "db-file", "d", config.DataFile, "Database file to store persistent data") - rootCmd.Flags().StringVar(&config.TenantID, "db-tenant-id", config.TenantID, "Database tenant ID to isolate data") + rootCmd.Flags().StringVarP(&config.Database, "database", "d", config.Database, "Database to store persistent data") + rootCmd.Flags().StringVar(&config.TenantID, "tenant-id", config.TenantID, "Database tenant ID to isolate data") rootCmd.Flags().IntVarP(&config.MaxMessages, "max", "m", config.MaxMessages, "Max number of messages to store") rootCmd.Flags().BoolVar(&config.UseMessageDates, "use-message-dates", config.UseMessageDates, "Use message dates as the received dates") rootCmd.Flags().BoolVar(&config.IgnoreDuplicateIDs, "ignore-duplicate-ids", config.IgnoreDuplicateIDs, "Ignore duplicate messages (by Message-Id)") @@ -133,6 +133,10 @@ func init() { rootCmd.Flags().StringVar(&config.WebhookURL, "webhook-url", config.WebhookURL, "Send a webhook request for new messages") rootCmd.Flags().IntVar(&webhook.RateLimit, "webhook-limit", webhook.RateLimit, "Limit webhook requests per second") + // DEPRECATED FLAG 2024/04/12 - but will not be removed to maintain backwards compatibility + rootCmd.Flags().StringVar(&config.Database, "db-file", config.Database, "Database file to store persistent data") + rootCmd.Flags().Lookup("db-file").Hidden = true + // DEPRECATED FLAGS 2023/03/12 rootCmd.Flags().StringVar(&config.UITLSCert, "ui-ssl-cert", config.UITLSCert, "SSL certificate for web UI - requires ui-ssl-key") rootCmd.Flags().StringVar(&config.UITLSKey, "ui-ssl-key", config.UITLSKey, "SSL key for web UI - requires ui-ssl-cert") @@ -156,11 +160,11 @@ func init() { // Load settings from environment func initConfigFromEnv() { // General - if len(os.Getenv("MP_DB_FILE")) > 0 { - config.DataFile = os.Getenv("MP_DB_FILE") + if len(os.Getenv("MP_DATABASE")) > 0 { + config.Database = os.Getenv("MP_DATABASE") } - config.TenantID = os.Getenv("MP_DB_TENANT") + config.TenantID = os.Getenv("MP_TENANT_ID") if len(os.Getenv("MP_MAX_MESSAGES")) > 0 { config.MaxMessages, _ = strconv.Atoi(os.Getenv("MP_MAX_MESSAGES")) @@ -229,7 +233,6 @@ func initConfigFromEnv() { if getEnabledFromEnv("MP_SMTP_REQUIRE_TLS") { config.SMTPRequireTLS = true } - if getEnabledFromEnv("MP_SMTP_AUTH_ALLOW_INSECURE") { config.SMTPAuthAllowInsecure = true } @@ -295,12 +298,11 @@ func initConfigFromEnv() { // load deprecated settings from environment and warn func initDeprecatedConfigFromEnv() { - // deprecated 2024/04/08 + // deprecated 2024/04/12 - but will not be removed to maintain backwards compatibility if len(os.Getenv("MP_DATA_FILE")) > 0 { - // do not warn - this will remain for quite some time - // logger.Log().Warn("ENV MP_DATA_FILE has been deprecated, use MP_DB_FILE") - config.DataFile = os.Getenv("MP_DATA_FILE") + config.Database = os.Getenv("MP_DATA_FILE") } + // deprecated 2023/03/12 if len(os.Getenv("MP_UI_SSL_CERT")) > 0 { logger.Log().Warn("ENV MP_UI_SSL_CERT has been deprecated, use MP_UI_TLS_CERT") diff --git a/config/config.go b/config/config.go index cfee030..c5522cb 100644 --- a/config/config.go +++ b/config/config.go @@ -26,8 +26,8 @@ var ( // HTTPListen to listen on : HTTPListen = "[::]:8025" - // DataFile for mail (optional) - DataFile string + // Database for mail (optional) + Database string // TenantID is an optional prefix to be applied to all database tables, // allowing multiple isolated instances of Mailpit to share a database. @@ -189,8 +189,8 @@ func VerifyConfig() error { cssFontRestriction, cssFontRestriction, ) - if DataFile != "" && isDir(DataFile) { - DataFile = filepath.Join(DataFile, "mailpit.db") + if Database != "" && isDir(Database) { + Database = filepath.Join(Database, "mailpit.db") } TenantID = strings.TrimSpace(TenantID) diff --git a/internal/stats/stats.go b/internal/stats/stats.go index cfbc5b8..d4ff888 100644 --- a/internal/stats/stats.go +++ b/internal/stats/stats.go @@ -94,7 +94,7 @@ func Load() AppInformation { } } - info.Database = config.DataFile + info.Database = config.Database info.DatabaseSize = storage.DbSize() info.Messages = storage.CountTotal() info.Unread = storage.CountUnread() diff --git a/internal/storage/database.go b/internal/storage/database.go index 863faa4..699aff9 100644 --- a/internal/storage/database.go +++ b/internal/storage/database.go @@ -39,7 +39,7 @@ var ( // InitDB will initialise the database func InitDB() error { - p := config.DataFile + p := config.Database var dsn string if p == "" { @@ -61,7 +61,7 @@ func InitDB() error { logger.Log().Debugf("[db] opening database %s", p) } - config.DataFile = p + config.Database = p var err error diff --git a/internal/storage/testing.go b/internal/storage/testing.go index 8665d14..a8d3f12 100644 --- a/internal/storage/testing.go +++ b/internal/storage/testing.go @@ -19,7 +19,7 @@ var ( func setup() { logger.NoLogging = true config.MaxMessages = 0 - config.DataFile = os.Getenv("MP_DATA_FILE") + config.Database = os.Getenv("MP_DATABASE") if err := InitDB(); err != nil { panic(err) diff --git a/server/server_test.go b/server/server_test.go index 2a7ebbc..206a1c4 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -205,7 +205,7 @@ func TestAPIv1Search(t *testing.T) { func setup() { logger.NoLogging = true config.MaxMessages = 0 - config.DataFile = os.Getenv("MP_DATA_FILE") + config.Database = os.Getenv("MP_DATABASE") if err := storage.InitDB(); err != nil { panic(err)