github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/shell/autocomplete/execs_unix.go (about)

     1  //go:build !windows && !plan9
     2  // +build !windows,!plan9
     3  
     4  package autocomplete
     5  
     6  import (
     7  	"os"
     8  	"strings"
     9  
    10  	"github.com/lmorg/murex/utils/consts"
    11  	"github.com/phayes/permbits"
    12  )
    13  
    14  func listExes(path string, exes map[string]bool) {
    15  	// if WSL then using a Windows lookup rather than POSIX
    16  	for i := range wslMounts {
    17  		if strings.HasPrefix(path, wslMounts[i]) {
    18  			listExesWindows(path, exes)
    19  			return
    20  		}
    21  	}
    22  
    23  	// POSIX lookup
    24  	files, _ := os.ReadDir(path)
    25  	for _, f := range files {
    26  		if f.IsDir() {
    27  			continue
    28  		}
    29  		fi, _ := f.Info()
    30  		perm := permbits.FileMode(fi.Mode())
    31  		switch {
    32  		case perm.OtherExecute() && fi.Mode().IsRegular():
    33  			exes[f.Name()] = true
    34  		case perm.OtherExecute() && fi.Mode()&os.ModeSymlink != 0:
    35  			ln, err := os.Readlink(path + consts.PathSlash + f.Name())
    36  			if err != nil {
    37  				continue
    38  			}
    39  			if ln[0] != consts.PathSlash[0] {
    40  				ln = path + consts.PathSlash + ln
    41  			}
    42  			info, err := os.Stat(ln)
    43  			if err != nil {
    44  				continue
    45  			}
    46  			perm := permbits.FileMode(info.Mode())
    47  			if perm.OtherExecute() && info.Mode().IsRegular() {
    48  				exes[f.Name()] = true
    49  			}
    50  
    51  		default:
    52  			/*|| perm.GroupExecute()||perm.UserExecute() need to check what user and groups you are in first */
    53  		}
    54  	}
    55  }
    56  
    57  func matchExes(s string, exes map[string]bool) (items []string) {
    58  	for name := range exes {
    59  		if strings.HasPrefix(name, s) {
    60  			if name != consts.NamedPipeProcName {
    61  				items = append(items, name[len(s):])
    62  			}
    63  		}
    64  	}
    65  
    66  	return
    67  }