github.com/telepresenceio/telepresence/v2@v2.20.0-pro.6.0.20240517030216-236ea954e789/pkg/filelocation/app.go (about)

     1  package filelocation
     2  
     3  import (
     4  	"context"
     5  	"path/filepath"
     6  )
     7  
     8  const appName = "telepresence"
     9  
    10  // AppUserLogDir returns the directory to use for application-specific
    11  // user-specific log files.
    12  //
    13  //   - On Darwin, it returns "$HOME/Library/Logs/telepresence".  Specified by:
    14  //     https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/MacOSXDirectories/MacOSXDirectories.html
    15  //
    16  //   - On everything else, it returns "{{AppUserCacheDir}}/logs" (using the
    17  //     appropriate path separator, if not "/").
    18  //
    19  // If the location cannot be determined (for example, $HOME is not defined),
    20  // then it will return an error.
    21  func AppUserLogDir(ctx context.Context) string {
    22  	if logDir, ok := ctx.Value(logCtxKey{}).(string); ok && logDir != "" {
    23  		return logDir
    24  	}
    25  	switch goos(ctx) {
    26  	case "darwin":
    27  		return filepath.Join(UserHomeDir(ctx), "Library", "Logs", appName)
    28  	default: // Unix
    29  		return filepath.Join(AppUserCacheDir(ctx), "logs")
    30  	}
    31  }
    32  
    33  // AppUserCacheDir returns the directory to use for application-specific
    34  // user-specific cache data.
    35  //
    36  // On all platforms, this returns "{{UserCacheDir}}/telepresence" (using the
    37  // appropriate path separator, if not "/").
    38  //
    39  // If the location cannot be determined (for example, $HOME is not defined),
    40  // then it will return an error.
    41  func AppUserCacheDir(ctx context.Context) string {
    42  	if cacheDir, ok := ctx.Value(cacheCtxKey{}).(string); ok && cacheDir != "" {
    43  		return cacheDir
    44  	}
    45  	return filepath.Join(UserCacheDir(ctx), appName)
    46  }
    47  
    48  // AppUserConfigDir returns the directory to use for application-specific
    49  // user-specific configuration data.
    50  //
    51  // On all platforms, this returns "{{UserConfigDir}}/telepresence" (using the
    52  // appropriate path separator, if not "/").
    53  //
    54  // If the location cannot be determined (for example, $HOME is not defined),
    55  // then it will return an error.
    56  func AppUserConfigDir(ctx context.Context) string {
    57  	if configDir, ok := ctx.Value(configCtxKey{}).(string); ok && configDir != "" {
    58  		return configDir
    59  	}
    60  	return filepath.Join(UserConfigDir(ctx), appName)
    61  }
    62  
    63  // AppSystemConfigDirs returns a list of directories to search for
    64  // application-specific (but not user-specific) configuration data.
    65  //
    66  // On all platforms, this returns the list from SystemConfigDirs, with
    67  // "/telepresence" appended to each directory (using the appropriate path
    68  // separator, if not "/").
    69  //
    70  // If the location cannot be determined, then it will return an error.
    71  func AppSystemConfigDirs(ctx context.Context) []string {
    72  	if sysConfigDirs, ok := ctx.Value(sysConfigsCtxKey{}).([]string); ok && sysConfigDirs != nil {
    73  		return sysConfigDirs
    74  	}
    75  	dirs := systemConfigDirs()
    76  	ret := make([]string, 0, len(dirs))
    77  	for _, dir := range dirs {
    78  		ret = append(ret, filepath.Join(dir, appName))
    79  	}
    80  	return ret
    81  }