refactor(config): move XDG_DATA_HOME read into userenv

This commit is contained in:
balanza
2026-04-02 14:49:07 +02:00
committed by Emanuele De Cupis
parent e95ad0a2e2
commit 59ba7116e6
4 changed files with 23 additions and 38 deletions
+4 -16
View File
@@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"os"
"path/filepath"
"github.com/urfave/cli/v3"
@@ -64,11 +63,10 @@ func generateEntryAction(ctx context.Context, cmd *cli.Command, cfg *config.Valu
listCmd := commands.NewListCommand(cfg, containerManager)
opts := &commands.GenerateEntryOptions{
Verbose: cmd.Bool("verbose"),
Delete: cmd.Bool("delete"),
Root: cmd.Bool("root"),
DesktopEntryBaseDir: getDesktopEntryDir(),
DistroboxPath: distroboxPath,
Verbose: cmd.Bool("verbose"),
Delete: cmd.Bool("delete"),
Root: cmd.Bool("root"),
DistroboxPath: distroboxPath,
}
if cmd.Bool("all") {
opts.All = true
@@ -86,13 +84,3 @@ func generateEntryAction(ctx context.Context, cmd *cli.Command, cfg *config.Valu
return nil
}
// getDesktopEntryDir resolves the system path for the desktop entry file
func getDesktopEntryDir() string {
xdgDataHome := os.Getenv("XDG_DATA_HOME")
if xdgDataHome == "" {
home := os.Getenv("HOME")
return filepath.Join(home, ".local", "share")
}
return xdgDataHome
}
-15
View File
@@ -1,16 +1 @@
package config
import (
"os"
"path/filepath"
)
// GetDesktopEntryDir resolves the system path for the desktop entry file
func GetDesktopEntryDir() string {
xdgDataHome := os.Getenv("XDG_DATA_HOME")
if xdgDataHome == "" {
home := os.Getenv("HOME")
return filepath.Join(home, ".local", "share")
}
return xdgDataHome
}
+16 -5
View File
@@ -6,16 +6,18 @@ import (
"os"
"os/exec"
"os/user"
"path/filepath"
"strconv"
"strings"
)
type UserEnvironment struct {
User string
UserID string
GroupID string
Home string
Shell string
User string
UserID string
GroupID string
Home string
Shell string
DesktopEntryBaseDir string
}
// LoadUserEnvironment loads the user environment variables
@@ -25,6 +27,8 @@ type UserEnvironment struct {
// - USER
// - HOME
// - SHELL
//
//nolint:gocognit
func LoadUserEnvironment(ctx context.Context) *UserEnvironment {
env := &UserEnvironment{}
@@ -81,6 +85,13 @@ func LoadUserEnvironment(ctx context.Context) *UserEnvironment {
env.GroupID = strings.TrimSpace(string(gid))
}
// DESKTOP ENTRY DIR
if xdgDataHome := os.Getenv("XDG_DATA_HOME"); xdgDataHome != "" {
env.DesktopEntryBaseDir = xdgDataHome
} else {
env.DesktopEntryBaseDir = filepath.Join(env.Home, ".local", "share")
}
return env
}
+3 -2
View File
@@ -9,7 +9,7 @@ import (
"path/filepath"
"strings"
"github.com/89luca89/distrobox/internal/config"
"github.com/89luca89/distrobox/internal/userenv"
pkgconfig "github.com/89luca89/distrobox/pkg/config"
)
@@ -77,7 +77,8 @@ func (c *GenerateEntryCommand) Execute(
// Determine the desktop entry base dir
desktopEntryBaseDir := opts.DesktopEntryBaseDir
if desktopEntryBaseDir == "" {
desktopEntryBaseDir = config.GetDesktopEntryDir()
userEnv := userenv.LoadUserEnvironment(ctx)
desktopEntryBaseDir = userEnv.DesktopEntryBaseDir
}
if opts.Delete {