github.com/andrewsun2898/u-root@v6.0.1-0.20200616011413-4b2895c1b815+incompatible/cmds/core/elvish/eval/util.go (about)

     1  package eval
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/u-root/u-root/cmds/core/elvish/eval/vals"
     8  	"github.com/u-root/u-root/cmds/core/elvish/parse"
     9  	"github.com/u-root/u-root/cmds/core/elvish/util"
    10  )
    11  
    12  func throw(e error) {
    13  	util.Throw(e)
    14  }
    15  
    16  func throwf(format string, args ...interface{}) {
    17  	util.Throw(fmt.Errorf(format, args...))
    18  }
    19  
    20  func maybeThrow(err error) {
    21  	if err != nil {
    22  		util.Throw(err)
    23  	}
    24  }
    25  
    26  // mustScanToGo is like vals.ScanToGo, except that errors are turned into
    27  // exceptions.
    28  func mustScanToGo(src interface{}, ptr interface{}) {
    29  	maybeThrow(vals.ScanToGo(src, ptr))
    30  }
    31  
    32  func mustGetHome(uname string) string {
    33  	dir, err := util.GetHome(uname)
    34  	if err != nil {
    35  		throw(err)
    36  	}
    37  	return dir
    38  }
    39  
    40  func makeFlag(m parse.RedirMode) int {
    41  	switch m {
    42  	case parse.Read:
    43  		return os.O_RDONLY
    44  	case parse.Write:
    45  		return os.O_WRONLY | os.O_CREATE | os.O_TRUNC
    46  	case parse.ReadWrite:
    47  		return os.O_RDWR | os.O_CREATE
    48  	case parse.Append:
    49  		return os.O_WRONLY | os.O_CREATE | os.O_APPEND
    50  	default:
    51  		return -1
    52  	}
    53  }