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