From 99bc18bb249f76485645a49530fb6a8af9c7d5cd Mon Sep 17 00:00:00 2001 From: pshubham Date: Wed, 4 Dec 2024 17:24:27 +0530 Subject: [PATCH 1/2] Add default parent directory for storing logs and persistent data(WAL) --- config/config.go | 50 +++++++++++++++++++++++++++++++++++++++++++++ internal/cli/cli.go | 5 ++--- main.go | 6 ++++-- 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/config/config.go b/config/config.go index 98d1fc64f..05b0848a1 100644 --- a/config/config.go +++ b/config/config.go @@ -6,6 +6,7 @@ import ( "log/slog" "os" "path/filepath" + "runtime" "time" "github.com/dicedb/dice/internal/server/utils" @@ -85,9 +86,13 @@ auth.password = "" # Network Configuration network.io_buffer_length = 512 network.io_buffer_length_max = 51200` + linuxPath = "/var/lib/dicedb" + darwinPath = "~/Library/Application Support/dicedb" + windowsPath = "C:\\ProgramData\\dicedb" ) var ( + DefaultParentDir = "." CustomConfigFilePath = utils.EmptyStr CustomConfigDirPath = utils.EmptyStr ) @@ -229,6 +234,51 @@ func loadDiceConfig(configFilePath string) error { return parser.Loadconfig(DiceConfig) } +// ConfigureParentDirPaths Creates the default parent directory which can be used for logs and persistent data +func ConfigureParentDirPaths() { + var err error + DefaultParentDir, err = createDefaultParentDir() + if err != nil { + slog.Warn("Failed to create default preferences directory", slog.String("error", err.Error())) + } +} + +func createDefaultParentDir() (string, error) { + // Get the default directory path for the OS + prefDir := GetDefaultParentDirPath() + // Create the directory (MkdirAll handles "already exists" gracefully) + if err := os.MkdirAll(prefDir, os.ModePerm); err != nil { + return "", fmt.Errorf("failed to create preferences directory: %w", err) + } + + return prefDir, nil +} + +func GetDefaultParentDirPath() string { + switch runtime.GOOS { + case "linux": + return linuxPath + case "darwin": + return expandHomeDirDarwin(darwinPath) + case "windows": + return windowsPath + default: + slog.Warn("unsupported OS, defaulting to current directory") + return "." + } +} + +func expandHomeDirDarwin(path string) string { + if len(path) > 0 && path[0] == '~' { + home, err := os.UserHomeDir() + if err != nil { + return path // Fallback if home directory can't be resolved + } + return filepath.Join(home, path[1:]) + } + return path +} + func MergeFlags(flags *Config) { flagset := flag.CommandLine flagset.Visit(func(f *flag.Flag) { diff --git a/internal/cli/cli.go b/internal/cli/cli.go index 36f80cf15..360a0f4ab 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -123,7 +123,7 @@ func Execute() { fmt.Println(" -enable-watch Enable support for .WATCH commands and real-time reactivity (default: false)") fmt.Println(" -enable-profiling Enable profiling and capture critical metrics and traces in .prof files (default: false)") fmt.Println(" -log-level Log level, values: info, debug (default: \"info\")") - fmt.Println(" -log-dir Log directory path (default: \"/tmp/dicedb\")") + fmt.Printf(" -log-dir Log directory path (default: \"%s\")\n", config.GetDefaultParentDirPath()) fmt.Println(" -enable-persistence Enable write-ahead logging (default: false)") fmt.Println(" -restore-wal Restore the database from the WAL files (default: false)") fmt.Println(" -wal-engine WAL engine to use, values: sqlite, aof (default: \"null\")") @@ -137,6 +137,7 @@ func Execute() { } flag.Parse() + config.ConfigureParentDirPaths() if len(os.Args) > 2 { switch os.Args[1] { @@ -224,8 +225,6 @@ func Execute() { defaultConfig(&flagsConfig) } } - - defaultConfig(&flagsConfig) } func defaultConfig(flags *config.Config) { diff --git a/main.go b/main.go index 09744ae52..2efea5d18 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "net/http" "os" "os/signal" + "path/filepath" "runtime" "runtime/pprof" "runtime/trace" @@ -54,8 +55,9 @@ func main() { wl, _ = wal.NewNullWAL() if config.DiceConfig.Persistence.Enabled { + WALDir := filepath.Join(config.DefaultParentDir, config.DiceConfig.Persistence.WALDir) if config.DiceConfig.Persistence.WALEngine == "sqlite" { - _wl, err := wal.NewSQLiteWAL(config.DiceConfig.Persistence.WALDir) + _wl, err := wal.NewSQLiteWAL(WALDir) if err != nil { slog.Warn("could not create WAL with", slog.String("wal-engine", config.DiceConfig.Persistence.WALEngine), slog.Any("error", err)) sigs <- syscall.SIGKILL @@ -63,7 +65,7 @@ func main() { } wl = _wl } else if config.DiceConfig.Persistence.WALEngine == "aof" { - _wl, err := wal.NewAOFWAL(config.DiceConfig.Persistence.WALDir) + _wl, err := wal.NewAOFWAL(WALDir) if err != nil { slog.Warn("could not create WAL with", slog.String("wal-engine", config.DiceConfig.Persistence.WALEngine), slog.Any("error", err)) sigs <- syscall.SIGKILL From d8e49cbdbb1de24c70ac708276d78a4bbf60ba15 Mon Sep 17 00:00:00 2001 From: pshubham Date: Wed, 4 Dec 2024 18:29:48 +0530 Subject: [PATCH 2/2] Fix lint --- config/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index 05b0848a1..bb03b9cf9 100644 --- a/config/config.go +++ b/config/config.go @@ -269,7 +269,7 @@ func GetDefaultParentDirPath() string { } func expandHomeDirDarwin(path string) string { - if len(path) > 0 && path[0] == '~' { + if path != "" && path[0] == '~' { home, err := os.UserHomeDir() if err != nil { return path // Fallback if home directory can't be resolved