mirror of
https://github.com/89luca89/distrobox.git
synced 2026-08-17 16:34:42 -05:00
refactor(config): remove unused dependency
This commit is contained in:
committed by
Alessio Biancalana
parent
ec99b5d884
commit
f311f760df
@@ -3,7 +3,6 @@ module github.com/89luca89/distrobox
|
||||
go 1.25.3
|
||||
|
||||
require (
|
||||
github.com/joho/godotenv v1.5.1
|
||||
github.com/stretchr/testify v1.11.1
|
||||
github.com/urfave/cli/v3 v3.5.0
|
||||
gopkg.in/ini.v1 v1.67.0
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||
|
||||
@@ -1,31 +1,11 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
// LoadConfig loads configuration files in order, with later files taking priority.
|
||||
// Environment variables are NOT overwritten by config files.
|
||||
func LoadConfig() error {
|
||||
configFilePaths, err := getConfigFilePaths()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get config file paths: %w", err)
|
||||
}
|
||||
|
||||
if err := godotenv.Load(configFilePaths...); err != nil {
|
||||
if !errors.Is(err, os.ErrNotExist) {
|
||||
return fmt.Errorf("failed to load config: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetDesktopEntryDir resolves the system path for the desktop entry file
|
||||
func GetDesktopEntryDir() string {
|
||||
xdgDataHome := os.Getenv("XDG_DATA_HOME")
|
||||
@@ -45,37 +25,3 @@ func GetDistroboxPath() (string, error) {
|
||||
}
|
||||
return distroboxPath, nil
|
||||
}
|
||||
|
||||
// getConfigFilePaths returns a list of configuration file paths in order of priority.
|
||||
func getConfigFilePaths() ([]string, error) {
|
||||
execPath, err := os.Executable()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get executable path: %w", err)
|
||||
}
|
||||
|
||||
execPath, err = filepath.EvalSymlinks(execPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to evaluate symlinks for executable path: %w", err)
|
||||
}
|
||||
|
||||
selfDir := filepath.Dir(execPath)
|
||||
|
||||
xdgConfigHome := os.Getenv("XDG_CONFIG_HOME")
|
||||
if xdgConfigHome == "" {
|
||||
xdgConfigHome = filepath.Join(os.Getenv("HOME"), ".config")
|
||||
}
|
||||
|
||||
home := os.Getenv("HOME")
|
||||
|
||||
// Highest priority first
|
||||
return []string{
|
||||
filepath.Join(home, ".distroboxrc"),
|
||||
filepath.Join(xdgConfigHome, "distrobox", "distrobox.conf"),
|
||||
"/etc/distrobox/distrobox.conf",
|
||||
"/usr/local/share/distrobox/distrobox.conf",
|
||||
"/usr/etc/distrobox/distrobox.conf",
|
||||
"/usr/share/defaults/distrobox/distrobox.conf",
|
||||
"/usr/share/distrobox/distrobox.conf",
|
||||
filepath.Join(selfDir, "..", "share", "distrobox", "distrobox.conf"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
package config_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/89luca89/distrobox/internal/config"
|
||||
)
|
||||
|
||||
func TestLoadConfig_NoConfigFiles(t *testing.T) {
|
||||
// LoadConfig should not error when no config files exist
|
||||
err := config.LoadConfig()
|
||||
assert.NoError(t, err, "LoadConfig() should not error when no config files exist")
|
||||
}
|
||||
|
||||
func TestLoadConfig_LoadsEnvFromFile(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
configFile := filepath.Join(tmpDir, ".distroboxrc")
|
||||
|
||||
content := []byte("TEST_VAR=hello_from_config\n")
|
||||
err := os.WriteFile(configFile, content, 0o644)
|
||||
require.NoError(t, err, "failed to write temp config")
|
||||
|
||||
t.Setenv("HOME", tmpDir)
|
||||
os.Unsetenv("TEST_VAR")
|
||||
|
||||
err = config.LoadConfig()
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, "hello_from_config", os.Getenv("TEST_VAR"))
|
||||
}
|
||||
|
||||
func TestLoadConfig_EnvVarOverridesFile(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
configFile := filepath.Join(tmpDir, ".distroboxrc")
|
||||
|
||||
content := []byte("TEST_VAR=hello_from_config\n")
|
||||
err := os.WriteFile(configFile, content, 0o644)
|
||||
require.NoError(t, err, "failed to write temp config")
|
||||
|
||||
t.Setenv("HOME", tmpDir)
|
||||
t.Setenv("TEST_VAR", "hello_from_env")
|
||||
|
||||
err = config.LoadConfig()
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, "hello_from_env", os.Getenv("TEST_VAR"))
|
||||
}
|
||||
Reference in New Issue
Block a user