github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/builtins/core/open/system.go (about)

     1  package open
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/lmorg/murex/lang"
     8  	"github.com/lmorg/murex/utils/which"
     9  )
    10  
    11  func openSystemCommand(p *lang.Process, path string) error {
    12  	cmd, err := openCommand()
    13  	if err != nil {
    14  		return fmt.Errorf("cannot open '%s': %s", path, err.Error())
    15  	}
    16  
    17  	fork := p.Fork(lang.F_DEFAULTS)
    18  	block := fmt.Sprintf(`%s %s`, cmd, path)
    19  	exitNum, err := fork.Execute([]rune(block))
    20  	p.ExitNum = exitNum
    21  	return err
    22  }
    23  
    24  var openCommands = []string{
    25  	"open", "xdg-open",
    26  }
    27  
    28  func openCommand() (string, error) {
    29  	for i := range openCommands {
    30  		openPath := which.Which(openCommands[i])
    31  		if openPath != "" {
    32  			return openPath, nil
    33  		}
    34  	}
    35  
    36  	return "", errors.New("cannot locate any external open handlers, eg `open` or `open-xdg`")
    37  }