github.com/inazumav/sing-box@v0.0.0-20230926072359-ab51429a14f1/constant/path.go (about)

     1  package constant
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  
     7  	"github.com/sagernet/sing/common/rw"
     8  )
     9  
    10  const dirName = "sing-box"
    11  
    12  var resourcePaths []string
    13  
    14  func FindPath(name string) (string, bool) {
    15  	name = os.ExpandEnv(name)
    16  	if rw.FileExists(name) {
    17  		return name, true
    18  	}
    19  	for _, dir := range resourcePaths {
    20  		if path := filepath.Join(dir, dirName, name); rw.FileExists(path) {
    21  			return path, true
    22  		}
    23  		if path := filepath.Join(dir, name); rw.FileExists(path) {
    24  			return path, true
    25  		}
    26  	}
    27  	return name, false
    28  }
    29  
    30  func init() {
    31  	resourcePaths = append(resourcePaths, ".")
    32  	if home := os.Getenv("HOME"); home != "" {
    33  		resourcePaths = append(resourcePaths, home)
    34  	}
    35  	if userConfigDir, err := os.UserConfigDir(); err == nil {
    36  		resourcePaths = append(resourcePaths, userConfigDir)
    37  	}
    38  	if userCacheDir, err := os.UserCacheDir(); err == nil {
    39  		resourcePaths = append(resourcePaths, userCacheDir)
    40  	}
    41  }