diff --git a/pkg/config/config.go b/pkg/config/config.go index 6eaab3acd..272d9e0a3 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -287,37 +287,33 @@ func (k Key) setDefault(i interface{}) { viper.SetDefault(string(k), i) } -// Tries different methods to figure out the binary folder. -// Copied and adopted from https://github.com/speedata/publisher/commit/3b668668d57edef04ea854d5bbd58f83eb1b799f -func getBinaryDirLocation() string { - // First, check if the standard library gives us the path. This will work 99% of the time. - ex, err := os.Executable() - if err == nil { +// getRootpathLocation determines the default root path for Vikunja data. +// It prefers the current working directory, which respects systemd's +// WorkingDirectory= setting and is the most intuitive default. +// Falls back to the binary's directory if Getwd fails. +func getRootpathLocation() string { + // Prefer working directory — this respects systemd WorkingDirectory= + // and is the intuitive default for most deployment scenarios. + if wd, err := os.Getwd(); err == nil { + return wd + } + + // Fall back to the binary's directory. + if ex, err := os.Executable(); err == nil { return filepath.Dir(ex) } - // Then check if the binary was run with a full path and use that if that's the case. - if strings.Contains(os.Args[0], "/") { - binDir, err := filepath.Abs(filepath.Dir(os.Args[0])) - if err != nil { - log.Fatal(err) - } - return binDir - } - + // Last resort: search $PATH. exeSuffix := "" if runtime.GOOS == "windows" { exeSuffix = ".exe" } - - // All else failing, search for a vikunja binary in the current $PATH. - // This can give wrong results. - exeLocation, err := exec.LookPath("vikunja" + exeSuffix) - if err != nil { - log.Fatal(err) + if exeLocation, err := exec.LookPath("vikunja" + exeSuffix); err == nil { + return filepath.Dir(exeLocation) } - return filepath.Dir(exeLocation) + log.Fatal("Could not determine root path. Set service.rootpath in your config.") + return "" } // InitDefaultConfig sets default config values @@ -339,7 +335,7 @@ func InitDefaultConfig() { ServicePublicURL.setDefault("") ServiceEnableCaldav.setDefault(true) - ServiceRootpath.setDefault(getBinaryDirLocation()) + ServiceRootpath.setDefault(getRootpathLocation()) ServiceMaxItemsPerPage.setDefault(50) ServiceMotd.setDefault("") ServiceEnableLinkSharing.setDefault(true) diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go new file mode 100644 index 000000000..28b7cba12 --- /dev/null +++ b/pkg/config/config_test.go @@ -0,0 +1,34 @@ +// Vikunja is a to-do list application to facilitate your life. +// Copyright 2018-present Vikunja and contributors. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package config + +import ( + "os" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestGetRootpathLocation(t *testing.T) { + // The function should return the current working directory + expected, err := os.Getwd() + require.NoError(t, err) + + result := getRootpathLocation() + assert.Equal(t, expected, result) +}