-
Notifications
You must be signed in to change notification settings - Fork 11
/
client.go
98 lines (84 loc) · 1.7 KB
/
client.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package fanuc
import (
"fmt"
"net"
"net/url"
"os"
)
type ErrorClient interface {
Errors() ([]Error, error)
}
type FrameClient interface {
Frames() ([]Frame, error)
}
type IOClient interface {
IO(...Type) ([]IO, error)
}
type NumericRegisterClient interface {
NumericRegisters() ([]NumericRegister, error)
}
type PositionRegisterClient interface {
PositionRegisters() ([]PositionRegister, error)
}
type Client interface {
ErrorClient
FrameClient
IOClient
NumericRegisterClient
PositionRegisterClient
TPPrograms() ([]string, error)
TPPositions(programName string) ([]Position, error)
}
func isDir(s string) bool {
info, err := os.Stat(s)
if err != nil {
return false
}
if info.IsDir() {
return true
}
return false
}
func isIP(s string) bool {
ip := net.ParseIP(s)
if ip != nil {
return true
}
return false
}
func isURL(s string) bool {
_, err := url.ParseRequestURI(s)
if err != nil {
return false
}
return true
}
// NewClient returns a *FileClient or *HTTPClient depending on the provided
// argument. Local directories will return a *FileClient. Valid IP addresses
// or valid URL return an *HTTPClient.
// on the provided argument. Local directories will return a
func NewClient(target string) (Client, error) {
switch {
case isDir(target):
c, err := NewFileClient(target)
if err != nil {
return nil, err
}
return c, nil
case isIP(target):
url := fmt.Sprintf("http://%s", target)
c, err := NewHTTPClient(url)
if err != nil {
return nil, err
}
return c, nil
case isURL(target):
c, err := NewHTTPClient(target)
if err != nil {
return nil, err
}
return c, nil
default:
return nil, fmt.Errorf("%q is not a valid directory or host", target)
}
}