github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/internal/runners/deploy/path_lin_win.go (about)

     1  // +build !darwin
     2  
     3  package deploy
     4  
     5  import (
     6  	"os"
     7  	"strings"
     8  
     9  	"github.com/ActiveState/cli/internal/fileutils"
    10  	"github.com/ActiveState/cli/internal/locale"
    11  	"github.com/thoas/go-funk"
    12  )
    13  
    14  // usablePath will find the first writable directory under PATH
    15  // If on PATH and writable /usr/local/bin or /usr/bin are returned
    16  func usablePath() (string, error) {
    17  	paths := strings.Split(os.Getenv("PATH"), string(os.PathListSeparator))
    18  	if len(paths) == 0 {
    19  		return "", locale.NewInputError("err_deploy_path_empty", "Your system does not have any PATH entries configured, so symlinks can not be created.")
    20  	}
    21  
    22  	preferredPaths := []string{
    23  		"/usr/local/bin",
    24  		"/usr/bin",
    25  	}
    26  	var result string
    27  	for _, path := range paths {
    28  		if path == "" || (!fileutils.IsDir(path) && !fileutils.FileExists(path)) || !fileutils.IsWritable(path) {
    29  			continue
    30  		}
    31  
    32  		// check for preferred PATHs
    33  		if funk.Contains(preferredPaths, path) {
    34  			return path, nil
    35  		}
    36  		// use the first available directory in PATH
    37  		if result == "" {
    38  			result = path
    39  		}
    40  	}
    41  
    42  	if result != "" {
    43  		return result, nil
    44  	}
    45  
    46  	return "", locale.NewInputError("err_deploy_path_noperm", "No permission to create symlinks on any of the PATH entries: {{.V0}}.", os.Getenv("PATH"))
    47  }