-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck.go
69 lines (61 loc) · 1.24 KB
/
check.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
package env
import (
"errors"
"net"
"os"
)
// checkedValue wraps a Value and runs fn on any values passed to Set
// before calling the underlying Value.Set.
type checkedValue struct {
fn func(string) error
Value
}
func (v checkedValue) Set(x string) error {
if err := v.fn(x); err != nil {
return err
}
return v.Value.Set(x)
}
// isNonEmpty checks if x is a non-empty string.
func isNonEmpty(x string) error {
if x == "" {
return errors.New("empty")
}
return nil
}
// isBindAddr checks if x is a valid bind address.
//
// A valid bind addresses is of the form host:port,
// and port must be non-empty.
func isBindAddr(x string) error {
_, port, err := net.SplitHostPort(x)
if err != nil {
return err
}
if port == "" {
return errors.New("empty port")
}
return nil
}
// isDialAddr checks if x is a valid bind address.
//
// A valid bind addresses is of the form host:port,
// and port must be non-empty.
func isDialAddr(x string) error {
host, port, err := net.SplitHostPort(x)
if err != nil {
return err
}
if host == "" {
return errors.New("empty host")
}
if port == "" {
return errors.New("empty port")
}
return nil
}
// isPath checks if x is a valid path.
func isPath(x string) error {
_, err := os.Stat(x)
return err
}