forked from hyperhq/hyperd
/
client.go
133 lines (120 loc) · 3 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package client
import (
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"os"
"path/filepath"
"reflect"
"strings"
"text/template"
"time"
"github.com/docker/docker/cliconfig"
"github.com/docker/docker/pkg/homedir"
"github.com/hyperhq/runv/lib/term"
)
type HyperClient struct {
proto string
addr string
configFile *cliconfig.ConfigFile
scheme string
in io.ReadCloser
out io.Writer
err io.Writer
inFd uintptr
outFd uintptr
isTerminalIn bool
isTerminalOut bool
tlsConfig *tls.Config
transport *http.Transport
}
var funcMap = template.FuncMap{
"json": func(v interface{}) string {
a, _ := json.Marshal(v)
return string(a)
},
}
func (cli *HyperClient) getMethod(args ...string) (func(...string) error, bool) {
camelArgs := make([]string, len(args))
for i, s := range args {
if len(s) == 0 {
return nil, false
}
camelArgs[i] = strings.ToUpper(s[:1]) + strings.ToLower(s[1:])
}
methodName := "HyperCmd" + strings.Join(camelArgs, "")
method := reflect.ValueOf(cli).MethodByName(methodName)
if !method.IsValid() {
return nil, false
}
return method.Interface().(func(...string) error), true
}
// Cmd executes the specified command.
func (cli *HyperClient) Cmd(args ...string) error {
if len(args) > 1 {
method, exists := cli.getMethod(args[:2]...)
if exists {
return method(args[2:]...)
}
}
if len(args) > 0 {
method, exists := cli.getMethod(args[0])
if !exists {
fmt.Printf("%s: '%s' is not a %s command. See '%s --help'.\n", os.Args[0], args[0], os.Args[0], os.Args[0])
os.Exit(1)
}
return method(args[1:]...)
}
return cli.HyperCmdHelp()
}
func NewHyperClient(proto, addr string, tlsConfig *tls.Config) *HyperClient {
var (
inFd uintptr
outFd uintptr
isTerminalIn = false
isTerminalOut = false
scheme = "http"
)
if tlsConfig != nil {
scheme = "https"
}
// The transport is created here for reuse during the client session
tran := &http.Transport{
TLSClientConfig: tlsConfig,
}
// Why 32? See issue 8035
timeout := 32 * time.Second
if proto == "unix" {
// no need in compressing for local communications
tran.DisableCompression = true
tran.Dial = func(_, _ string) (net.Conn, error) {
return net.DialTimeout(proto, addr, timeout)
}
} else {
tran.Proxy = http.ProxyFromEnvironment
tran.Dial = (&net.Dialer{Timeout: timeout}).Dial
}
inFd, isTerminalIn = term.GetFdInfo(os.Stdin)
outFd, isTerminalOut = term.GetFdInfo(os.Stdout)
clifile, err := cliconfig.Load(filepath.Join(homedir.Get(), ".docker"))
if err != nil {
fmt.Fprintf(os.Stdout, "WARNING: Error loading config file %v\n", err)
}
return &HyperClient{
proto: proto,
addr: addr,
configFile: clifile,
in: os.Stdin,
out: os.Stdout,
err: os.Stdout,
inFd: inFd,
outFd: outFd,
isTerminalIn: isTerminalIn,
isTerminalOut: isTerminalOut,
scheme: scheme,
transport: tran,
}
}