github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/config/profile/paths.go (about)

     1  package profile
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  
     8  	"github.com/lmorg/murex/utils/consts"
     9  	"github.com/lmorg/murex/utils/home"
    10  )
    11  
    12  const (
    13  	// default locations
    14  	preloadFileName = ".murex_preload"
    15  	moduleDirName   = ".murex_modules/"
    16  	profileFileName = ".murex_profile"
    17  
    18  	// PreloadEnvVar environmental variable name for the preload profile override path. It's stored as a constant so typos are caught by the compiler
    19  	PreloadEnvVar = "MUREX_PRELOAD"
    20  
    21  	// ModuleEnvVar environmental variable name for the module override path. It's stored as a constant so typos are caught by the compiler
    22  	ModuleEnvVar = "MUREX_MODULES"
    23  
    24  	// ProfileEnvVar environmental variable name for the profile override path. It's stored as a constant so typos are caught by the compiler
    25  	ProfileEnvVar = "MUREX_PROFILE"
    26  )
    27  
    28  // PreloadPath returns the path of the preload profile
    29  func PreloadPath() string {
    30  	return validateProfilePath(PreloadEnvVar, preloadFileName)
    31  }
    32  
    33  // ProfilePath returns the path of your murex profile
    34  func ProfilePath() string {
    35  	return validateProfilePath(ProfileEnvVar, profileFileName)
    36  }
    37  
    38  func validateProfilePath(envvar, defaultFileName string) string {
    39  	path := os.Getenv(envvar)
    40  	if strings.TrimSpace(path) == "" {
    41  		return home.MyDir + consts.PathSlash + defaultFileName
    42  	}
    43  
    44  	fi, err := os.Stat(path)
    45  	if err != nil {
    46  		fmt.Fprintf(os.Stderr,
    47  			"Override path specified in %s does not exist: '%s'!\n  Assuming this is intentional, a new file will be created.\n",
    48  			envvar, path)
    49  		return path
    50  	}
    51  
    52  	if fi.IsDir() {
    53  		path += consts.PathSlash + defaultFileName
    54  	}
    55  
    56  	return path
    57  }
    58  
    59  // ModulePath returns the install path of the murex modules / packages
    60  func ModulePath() string {
    61  	path := os.Getenv(ModuleEnvVar)
    62  	if strings.TrimSpace(path) == "" {
    63  		return home.MyDir + consts.PathSlash + moduleDirName
    64  	}
    65  
    66  	fi, err := os.Stat(path)
    67  	if err != nil {
    68  		fmt.Fprintf(os.Stderr,
    69  			"Override path specified in %s does not exist: '%s'!\n  Assuming this is intentional, a new directory will be created.\n",
    70  			ModuleEnvVar, path)
    71  		return addTrailingSlash(path)
    72  	}
    73  
    74  	if !fi.IsDir() {
    75  		fmt.Fprintf(os.Stderr,
    76  			"Override path specified in %s is a file, directory expected: '%s'!\n  Falling back to default path.\n",
    77  			ModuleEnvVar, path)
    78  		return home.MyDir + consts.PathSlash + moduleDirName
    79  	}
    80  
    81  	return addTrailingSlash(path)
    82  }
    83  
    84  func addTrailingSlash(path string) string {
    85  	if strings.HasSuffix(path, consts.PathSlash) {
    86  		return path
    87  	}
    88  	return path + consts.PathSlash
    89  }