mirror of
https://github.com/go-vikunja/vikunja.git
synced 2026-03-12 01:59:34 -05:00
fix: prefer working directory for service.rootpath default
When running as a systemd service, the binary is often in /usr/local/bin but WorkingDirectory points to a data directory like /var/lib/vikunja. Previously, rootpath defaulted to the binary's directory, causing Vikunja to attempt creating files/db in /usr/local/bin. Now os.Getwd() is preferred, which respects systemd's WorkingDirectory and is the intuitive default for all deployment scenarios.
This commit is contained in:
@@ -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)
|
||||
|
||||
34
pkg/config/config_test.go
Normal file
34
pkg/config/config_test.go
Normal file
@@ -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 <https://www.gnu.org/licenses/>.
|
||||
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user