@@ -376,37 +376,55 @@ func logError(msg string, err error, cfg *RuntimeConfig) {
376376 os .Exit (1 )
377377}
378378
379- func setSelfEnvs (cfg * RuntimeConfig ) error {
380-
381- setEnvIfExists := func (dir , envVar , oldEnvVar string ) error {
382- if _ , err := os .Stat (dir ); err == nil {
383- oldValue := getEnv (globalEnv , oldEnvVar )
384- if oldValue == "" {
385- oldValue = getEnv (globalEnv , envVar )
386- setEnv (& globalEnv , oldEnvVar , oldValue )
387- }
388- setEnv (& globalEnv , envVar , dir )
389- }
390- return nil
391- }
392-
393- setEnvIfExists (hiddenPath (cfg .selfPath , ".home" ), "HOME" , "REAL_HOME" )
394- setEnvIfExists (hiddenPath (cfg .selfPath , ".share" ), "XDG_DATA_HOME" , "REAL_XDG_DATA_HOME" )
395- setEnvIfExists (hiddenPath (cfg .selfPath , ".config" ), "XDG_CONFIG_HOME" , "REAL_XDG_CONFIG_HOME" )
396- setEnvIfExists (hiddenPath (cfg .selfPath , ".cache" ), "XDG_CACHE_HOME" , "REAL_XDG_CACHE_HOME" )
397-
398- envFile := hiddenPath (cfg .selfPath , ".env" )
399- if _ , err := os .Stat (envFile ); err == nil {
400- if envs , err := godotenv .Read (envFile ); err == nil {
401- for key , value := range envs {
402- globalEnv = append (globalEnv , fmt .Sprintf ("%s=%s" , key , value ))
403- }
404- } else {
405- return fmt .Errorf ("failed to load .env file: %w" , err )
406- }
407- }
379+ /* TODO:
380+ if there is a `.config` directory, then:
381+
382+ - check if `REAL_XDG_CONFIG_HOME` is set.
383+ - if NOT set, set it to `XDG_CONFIG_HOME` or the default: `$HOME/.config`
384+ - finally set `XDG_CONFIG_HOME` to the `.config` dir regardless of the previous condition.
385+
386+ TODO 2: Remove func hiddenPath()
387+ */
388+
389+ func setSelfEnv (cfg * RuntimeConfig , dirSuffix , envVar , oldEnvVar , defaultValue string ) error {
390+ dir := hiddenPath (cfg .selfPath , dirSuffix )
391+ if _ , err := os .Stat (dir ); err == nil {
392+ // Save the old value in REAL_* if not already set
393+ oldValue := getEnv (globalEnv , oldEnvVar )
394+ if oldValue == "" {
395+ oldValue = getEnv (globalEnv , envVar )
396+ if oldValue == "" {
397+ // Use the provided default value if neither REAL_* nor the envVar is set
398+ oldValue = defaultValue
399+ }
400+ setEnv (& globalEnv , oldEnvVar , oldValue )
401+ }
402+ // Override the environment variable with the new directory
403+ setEnv (& globalEnv , envVar , dir )
404+ }
405+ return nil
406+ }
408407
409- return nil
408+ func setSelfEnvs (cfg * RuntimeConfig ) error {
409+ // Set environment variables with XDG logic, in the correct order
410+ homeDir := getEnv (globalEnv , "HOME" )
411+ setSelfEnv (cfg , ".config" , "XDG_CONFIG_HOME" , "REAL_XDG_CONFIG_HOME" , filepath .Join (homeDir , ".config" ))
412+ setSelfEnv (cfg , ".share" , "XDG_DATA_HOME" , "REAL_XDG_DATA_HOME" , filepath .Join (homeDir , ".local" , "share" ))
413+ setSelfEnv (cfg , ".cache" , "XDG_CACHE_HOME" , "REAL_XDG_CACHE_HOME" , filepath .Join (homeDir , ".cache" ))
414+ setSelfEnv (cfg , ".home" , "HOME" , "REAL_HOME" , homeDir )
415+
416+ // Load .env file if it exists
417+ envFile := hiddenPath (cfg .selfPath , ".env" )
418+ if _ , err := os .Stat (envFile ); err == nil {
419+ if envs , err := godotenv .Read (envFile ); err != nil {
420+ return fmt .Errorf ("failed to load .env file: %w" , err )
421+ } else {
422+ for key , value := range envs {
423+ globalEnv = append (globalEnv , fmt .Sprintf ("%s=%s" , key , value ))
424+ }
425+ }
426+ }
427+ return nil
410428}
411429
412430func executeFile (args []string , cfg * RuntimeConfig ) error {
0 commit comments