-
Notifications
You must be signed in to change notification settings - Fork 232
/
common.go
175 lines (156 loc) · 4.12 KB
/
common.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package daemon
import (
"io"
"net"
"os"
"runtime"
"strconv"
"strings"
"syscall"
"github.com/logrusorgru/aurora/v3"
"encr.dev/cli/daemon/run"
"encr.dev/cli/internal/onboarding"
"encr.dev/pkg/errlist"
meta "encr.dev/proto/encore/parser/meta/v1"
)
// OnStart implements run.EventListener.
func (s *Server) OnStart(r *run.Run) {}
// OnReload implements run.EventListener.
func (s *Server) OnReload(r *run.Run) {}
// OnStop implements run.EventListener.
func (s *Server) OnStop(r *run.Run) {}
// OnStdout implements run.EventListener.
func (s *Server) OnStdout(r *run.Run, line []byte) {
s.mu.Lock()
slog, ok := s.streams[r.ID]
s.mu.Unlock()
if ok {
slog.Stdout(true).Write(line)
}
}
// OnStderr implements run.EventListener.
func (s *Server) OnStderr(r *run.Run, line []byte) {
s.mu.Lock()
slog, ok := s.streams[r.ID]
s.mu.Unlock()
if ok {
slog.Stderr(true).Write(line)
}
}
func (s *Server) OnError(r *run.Run, err *errlist.List) {
s.mu.Lock()
slog, ok := s.streams[r.ID]
s.mu.Unlock()
if ok {
slog.Error(err)
}
}
func showFirstRunExperience(run *run.Run, md *meta.Data, stdout io.Writer) {
if state, err := onboarding.Load(); err == nil {
if !state.FirstRun.IsSet() {
// Is there a suitable endpoint to call?
var rpc *meta.RPC
var command string
for _, svc := range md.Svcs {
for _, r := range svc.Rpcs {
if cmd := genCurlCommand(run, md, r); rpc == nil || len(command) < len(cmd) {
rpc = r
command = cmd
}
}
}
if rpc != nil {
state.FirstRun.Set()
if err := state.Write(); err == nil {
stdout.Write([]byte(aurora.Sprintf("\nHint: make an API call by running: %s\n", aurora.Cyan(command))))
}
}
}
}
}
// findAvailableAddr attempts to find an available host:port that's near
// the given startAddr.
func findAvailableAddr(startAddr string) (host string, port int, ok bool) {
host, portStr, err := net.SplitHostPort(startAddr)
if err != nil {
host = "localhost"
portStr = "4000"
}
startPort, err := strconv.Atoi(portStr)
if err != nil {
startPort = 4000
}
for p := startPort + 1; p <= startPort+10 && p <= 65535; p++ {
addr := host + ":" + strconv.Itoa(p)
ln, err := net.Listen("tcp", addr)
if err == nil {
ln.Close()
return host, p, true
}
}
return "", 0, false
}
func genCurlCommand(run *run.Run, md *meta.Data, rpc *meta.RPC) string {
var payload []byte
method := rpc.HttpMethods[0]
switch method {
case "GET", "HEAD", "DELETE":
// doesn't use HTTP body payloads
default:
payload = genSchema(md, rpc.RequestSchema)
}
var segments []string
for _, seg := range rpc.Path.Segments {
var v string
switch seg.Type {
default:
v = "foo"
case meta.PathSegment_LITERAL:
v = seg.Value
case meta.PathSegment_WILDCARD, meta.PathSegment_FALLBACK:
v = "foo"
case meta.PathSegment_PARAM:
switch seg.ValueType {
case meta.PathSegment_STRING:
v = "foo"
case meta.PathSegment_BOOL:
v = "true"
case meta.PathSegment_INT8, meta.PathSegment_INT16, meta.PathSegment_INT32, meta.PathSegment_INT64,
meta.PathSegment_UINT8, meta.PathSegment_UINT16, meta.PathSegment_UINT32, meta.PathSegment_UINT64:
v = "1"
case meta.PathSegment_UUID:
v = "be23a21f-d12c-432c-91ec-fb8a52e23967" // some random UUID
default:
v = "foo"
}
}
segments = append(segments, v)
}
parts := []string{"curl"}
if (payload != nil && method != "POST") || (payload == nil && method != "GET") {
parts = append(parts, " -X ", method)
}
path := "/" + strings.Join(segments, "/")
parts = append(parts, " http://", run.ListenAddr, path)
if payload != nil {
parts = append(parts, " -d '", string(payload), "'")
}
return strings.Join(parts, "")
}
// errIsAddrInUse reports whether the error is due to the address already being in use.
func errIsAddrInUse(err error) bool {
if opErr, ok := err.(*net.OpError); ok {
if syscallErr, ok := opErr.Err.(*os.SyscallError); ok {
if errno, ok := syscallErr.Err.(syscall.Errno); ok {
const WSAEADDRINUSE = 10048
switch {
case errno == syscall.EADDRINUSE:
return true
case runtime.GOOS == "windows" && errno == WSAEADDRINUSE:
return true
}
}
}
}
return false
}