@@ -7,12 +7,10 @@ package main
7
7
import (
8
8
_ "embed"
9
9
"path/filepath"
10
- "runtime"
11
10
"strconv"
12
11
"time"
13
12
14
13
"context"
15
- "log"
16
14
"net/http"
17
15
"net/url"
18
16
"os"
@@ -25,26 +23,22 @@ import (
25
23
"github.com/gitpod-io/local-app/pkg/bastion"
26
24
"github.com/improbable-eng/grpc-web/go/grpcweb"
27
25
"github.com/sirupsen/logrus"
28
- cli "github.com/urfave/cli/v2"
29
- keyring "github.com/zalando/go-keyring"
26
+ "github.com/urfave/cli/v2"
27
+ "github.com/zalando/go-keyring"
30
28
"google.golang.org/grpc"
31
29
)
32
30
33
31
var (
34
32
// Version : current version
35
- Version string = strings .TrimSpace (version )
33
+ Version = strings .TrimSpace (version )
36
34
//go:embed version.txt
37
35
version string
38
36
)
39
37
40
38
func main () {
41
39
sshConfig := os .Getenv ("GITPOD_LCA_SSH_CONFIG" )
42
40
if sshConfig == "" {
43
- if runtime .GOOS == "windows" {
44
- sshConfig = filepath .Join (os .TempDir (), "gitpod_ssh_config" )
45
- } else {
46
- sshConfig = filepath .Join ("/tmp" , "gitpod_ssh_config" )
47
- }
41
+ sshConfig = filepath .Join (os .TempDir (), "gitpod_ssh_config" )
48
42
}
49
43
50
44
app := cli.App {
@@ -124,8 +118,17 @@ func main() {
124
118
if c .Bool ("mock-keyring" ) {
125
119
keyring .MockInit ()
126
120
}
127
- return run (c .String ("gitpod-host" ), c .String ("ssh_config" ), c .Int ("api-port" ), c .Bool ("allow-cors-from-port" ),
128
- c .Bool ("auto-tunnel" ), c .String ("auth-redirect-url" ), c .Bool ("verbose" ), c .Duration ("auth-timeout" ), c .Duration ("timeout" ))
121
+ return run (runOptions {
122
+ origin : c .String ("gitpod-host" ),
123
+ sshConfigPath : c .String ("ssh_config" ),
124
+ apiPort : c .Int ("api-port" ),
125
+ allowCORSFromPort : c .Bool ("allow-cors-from-port" ),
126
+ autoTunnel : c .Bool ("auto-tunnel" ),
127
+ authRedirectURL : c .String ("auth-redirect-url" ),
128
+ verbose : c .Bool ("verbose" ),
129
+ authTimeout : c .Duration ("auth-timeout" ),
130
+ localAppTimeout : c .Duration ("timeout" ),
131
+ })
129
132
},
130
133
Flags : []cli.Flag {
131
134
& cli.PathFlag {
@@ -139,7 +142,7 @@ func main() {
139
142
}
140
143
err := app .Run (os .Args )
141
144
if err != nil {
142
- log . Fatal (err )
145
+ logrus . WithError (err ). Fatal ( "Failed to start application." )
143
146
}
144
147
}
145
148
@@ -149,21 +152,33 @@ func DefaultCommand(name string) cli.ActionFunc {
149
152
}
150
153
}
151
154
152
- func run (origin , sshConfig string , apiPort int , allowCORSFromPort bool , autoTunnel bool , authRedirectUrl string , verbose bool , authTimeout time.Duration , localAppTimeout time.Duration ) error {
153
- if verbose {
155
+ type runOptions struct {
156
+ origin string
157
+ sshConfigPath string
158
+ apiPort int
159
+ allowCORSFromPort bool
160
+ autoTunnel bool
161
+ authRedirectURL string
162
+ verbose bool
163
+ authTimeout time.Duration
164
+ localAppTimeout time.Duration
165
+ }
166
+
167
+ func run (opts runOptions ) error {
168
+ if opts .verbose {
154
169
logrus .SetLevel (logrus .DebugLevel )
155
170
}
156
- logrus .WithField ("ssh_config" , sshConfig ).Info ("writing workspace ssh_config file" )
171
+ logrus .WithField ("ssh_config" , opts . sshConfigPath ).Info ("writing workspace ssh_config file" )
157
172
158
173
// Trailing slash(es) result in connection issues, so remove them preemptively
159
- origin = strings .TrimRight (origin , "/" )
174
+ origin : = strings .TrimRight (opts . origin , "/" )
160
175
originURL , err := url .Parse (origin )
161
176
if err != nil {
162
177
return err
163
178
}
164
179
wsHostRegex := "(\\ .[^.]+)\\ ." + strings .ReplaceAll (originURL .Host , "." , "\\ ." )
165
180
wsHostRegex = "([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}|[0-9a-z]{2,16}-[0-9a-z]{2,16}-[0-9a-z]{8,11})" + wsHostRegex
166
- if allowCORSFromPort {
181
+ if opts . allowCORSFromPort {
167
182
wsHostRegex = "([0-9]+)-" + wsHostRegex
168
183
}
169
184
hostRegex , err := regexp .Compile ("^" + wsHostRegex )
@@ -173,7 +188,7 @@ func run(origin, sshConfig string, apiPort int, allowCORSFromPort bool, autoTunn
173
188
174
189
var b * bastion.Bastion
175
190
176
- client , err := connectToServer (auth.LoginOpts {GitpodURL : origin , RedirectURL : authRedirectUrl , AuthTimeout : authTimeout }, func () {
191
+ client , err := connectToServer (auth.LoginOpts {GitpodURL : origin , RedirectURL : opts . authRedirectURL , AuthTimeout : opts . authTimeout }, func () {
177
192
if b != nil {
178
193
b .FullUpdate ()
179
194
}
@@ -188,21 +203,22 @@ func run(origin, sshConfig string, apiPort int, allowCORSFromPort bool, autoTunn
188
203
cb := bastion.CompositeCallbacks {
189
204
& logCallbacks {},
190
205
}
191
- s := & bastion.SSHConfigWritingCallback {Path : sshConfig }
192
- if sshConfig != "" {
206
+
207
+ s := & bastion.SSHConfigWritingCallback {Path : opts .sshConfigPath }
208
+ if opts .sshConfigPath != "" {
193
209
cb = append (cb , s )
194
210
}
195
211
196
- b = bastion .New (client , localAppTimeout , cb )
197
- b .EnableAutoTunnel = autoTunnel
212
+ b = bastion .New (client , opts . localAppTimeout , cb )
213
+ b .EnableAutoTunnel = opts . autoTunnel
198
214
grpcServer := grpc .NewServer ()
199
215
appapi .RegisterLocalAppServer (grpcServer , bastion .NewLocalAppService (b , s ))
200
216
allowOrigin := func (origin string ) bool {
201
217
// Is the origin a subdomain of the installations hostname?
202
218
return hostRegex .Match ([]byte (origin ))
203
219
}
204
220
go func () {
205
- err := http .ListenAndServe ("localhost:" + strconv .Itoa (apiPort ), grpcweb .WrapServer (grpcServer ,
221
+ err := http .ListenAndServe ("localhost:" + strconv .Itoa (opts . apiPort ), grpcweb .WrapServer (grpcServer ,
206
222
grpcweb .WithCorsForRegisteredEndpointsOnly (false ),
207
223
grpcweb .WithOriginFunc (allowOrigin ),
208
224
grpcweb .WithWebsockets (true ),
@@ -312,5 +328,5 @@ func login(loginOpts auth.LoginOpts) (string, error) {
312
328
type logCallbacks struct {}
313
329
314
330
func (* logCallbacks ) InstanceUpdate (w * bastion.Workspace ) {
315
- logrus .WithField ("workspace" , w ).Info ("instance update" )
331
+ logrus .WithField ("workspace" , w ).Info ("Instance update" )
316
332
}
0 commit comments