-
Notifications
You must be signed in to change notification settings - Fork 4
/
os.go
70 lines (60 loc) · 1.5 KB
/
os.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2018 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by the Blue Oak Model License
// license that can be found in the LICENSE file.
package funcmap
import (
"errors"
"io/ioutil"
"os"
)
// Getenv retrieves the value of the environment variable
// named by the key.
func Getenv(key interface{}) (string, error) {
sk, err := toStringE(key)
if err != nil {
return "", err
}
return os.Getenv(sk), nil
}
// ReadDir reads the directory named by dirname and returns
// a list of directory entries sorted by filename.
func ReadDir(dirname interface{}) ([]os.FileInfo, error) {
sd, err := toStringE(dirname)
if err != nil {
return nil, err
}
return ioutil.ReadDir(sd)
}
// ReadFile reads the directory named by dirname and returns
// a list of directory entries sorted by filename.
func ReadFile(filename interface{}) ([]byte, error) {
sf, err := toStringE(filename)
if err != nil {
return nil, err
}
return ioutil.ReadFile(sf)
}
// FileExists returns true if the file exists.
func FileExists(name interface{}) (bool, error) {
sn, err := toStringE(name)
if err != nil {
return false, err
}
_, err = os.Stat(sn)
return err == nil, nil
}
// Stat returns the os.FileInfo structure describing file.
func Stat(i interface{}) (os.FileInfo, error) {
path, err := toStringE(i)
if err != nil {
return nil, err
}
if path == "" {
return nil, errors.New("fileStat needs a path to a file")
}
r, err := os.Stat(path)
if err != nil {
return nil, err
}
return r, nil
}