diff --git a/internal/cli/generate-entry.go b/internal/cli/generate-entry.go index 04c1bbde..cf57e4dc 100644 --- a/internal/cli/generate-entry.go +++ b/internal/cli/generate-entry.go @@ -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 -} diff --git a/internal/config/config.go b/internal/config/config.go index e416c950..d912156b 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -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 -} diff --git a/internal/userenv/user_environment.go b/internal/userenv/user_environment.go index 9970c4e9..5b7f4030 100644 --- a/internal/userenv/user_environment.go +++ b/internal/userenv/user_environment.go @@ -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 } diff --git a/pkg/commands/generate_entry.go b/pkg/commands/generate_entry.go index aac5834f..21ee0739 100644 --- a/pkg/commands/generate_entry.go +++ b/pkg/commands/generate_entry.go @@ -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 {