-
Notifications
You must be signed in to change notification settings - Fork 1
/
validators.go
76 lines (66 loc) · 1.42 KB
/
validators.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
71
72
73
74
75
76
package main
import (
"errors"
"os"
"path/filepath"
"strconv"
"strings"
)
// TODO implement
func validConnectionParams(authMethod int, name, host, username, keyFile, password, port string) error {
var err error = nil
if err = isIP(host); err != nil {
return err
}
if err = isValidAuthMethod(authMethod, keyFile, password); err != nil {
return err
}
if err = isPort(port); err != nil {
return err
}
if authMethod == 1 {
if err = isValidFilePath(keyFile); err != nil {
return err
}
}
return nil
}
func isIP(ip string) error {
octets := strings.Split(ip, ".")
if len(octets) != 4 {
return errors.New("not valid ip address")
}
for _, octet := range octets {
if oc, err := strconv.Atoi(octet); err != nil || oc > 255 || oc < 0 {
return errors.New("not valid ip address")
}
}
return nil
}
func isValidFilePath(path string) error {
if !filepath.IsAbs(path) {
return errors.New("file path is not absolute")
}
if _, err := os.Stat(path); err != nil {
return errors.New("file path may not exist")
}
return nil
}
func isValidAuthMethod(method int, key, passw string) error {
if method == 0 {
if passw == "" {
return errors.New("password cannot be empty")
}
} else {
if key == "" {
return errors.New("public key cannot be empty")
}
}
return nil
}
func isPort(s string) error {
if _, err := strconv.Atoi(s); err != nil {
return errors.New("port should be number")
}
return nil
}