mirror of
https://github.com/89luca89/distrobox.git
synced 2026-08-19 01:14:49 -05:00
refactor(config): move XDG_DATA_HOME read into userenv
This commit is contained in:
committed by
Emanuele De Cupis
parent
e95ad0a2e2
commit
59ba7116e6
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user