@@ -20,15 +20,12 @@ import (
20
20
"fmt"
21
21
"net"
22
22
"os"
23
- "path"
24
- "runtime"
23
+ osruntime "runtime"
25
24
"strings"
26
25
"sync"
27
26
28
27
"github.com/docker/docker/api/types"
29
28
"github.com/docker/docker/api/types/container"
30
- "github.com/docker/docker/api/types/mount"
31
- "github.com/docker/docker/api/types/strslice"
32
29
"github.com/getkin/kin-openapi/openapi3"
33
30
"github.com/imdario/mergo"
34
31
"github.com/moby/moby/pkg/stdcopy"
@@ -39,7 +36,7 @@ import (
39
36
"github.com/nitrictech/newcli/pkg/containerengine"
40
37
"github.com/nitrictech/newcli/pkg/cron"
41
38
"github.com/nitrictech/newcli/pkg/output"
42
- "github.com/nitrictech/newcli/pkg/run "
39
+ "github.com/nitrictech/newcli/pkg/runtime "
43
40
"github.com/nitrictech/newcli/pkg/stack"
44
41
"github.com/nitrictech/newcli/pkg/utils"
45
42
v1 "github.com/nitrictech/nitric/pkg/api/nitric/v1"
@@ -138,10 +135,14 @@ func (c *codeConfig) apiSpec(api string) (*openapi3.T, error) {
138
135
139
136
// Collect all workers
140
137
for handler , f := range c .functions {
138
+ rt , err := runtime .NewRunTimeFromHandler (handler )
139
+ if err != nil {
140
+ return nil , err
141
+ }
141
142
if f .apis [api ] != nil {
142
143
for _ , w := range f .apis [api ].workers {
143
144
workers = append (workers , & apiHandler {
144
- target : containerNameFromHandler ( handler ),
145
+ target : rt . ContainerName ( ),
145
146
worker : w ,
146
147
})
147
148
}
@@ -203,7 +204,7 @@ func (c *codeConfig) apiSpec(api string) (*openapi3.T, error) {
203
204
Extensions : map [string ]interface {}{
204
205
"x-nitric-target" : map [string ]string {
205
206
"type" : "function" ,
206
- "name" : containerNameFromHandler ( w .target ) ,
207
+ "name" : w .target ,
207
208
},
208
209
},
209
210
},
@@ -214,35 +215,6 @@ func (c *codeConfig) apiSpec(api string) (*openapi3.T, error) {
214
215
return doc , nil
215
216
}
216
217
217
- func launchOptsForFunctionCollect (runCtx , handler string ) (run.LaunchOpts , error ) {
218
- rt , err := utils .NewRunTimeFromFilename (handler )
219
- if err != nil {
220
- return run.LaunchOpts {}, err
221
- }
222
-
223
- switch rt {
224
- case utils .RuntimeJavascript , utils .RuntimeTypescript :
225
- return run.LaunchOpts {
226
- Image : rt .DevImageName (),
227
- Entrypoint : strslice.StrSlice {"ts-node" },
228
- Cmd : strslice.StrSlice {"-T " + "/app/" + handler },
229
- }, nil
230
- case utils .RuntimeGolang :
231
- module , err := utils .GoModule (runCtx )
232
- if err != nil {
233
- return run.LaunchOpts {}, err
234
- }
235
- return run.LaunchOpts {
236
- Image : rt .DevImageName (),
237
- TargetWD : path .Join ("/go/src" , module ),
238
- Entrypoint : strslice.StrSlice {"go" , "run" },
239
- Cmd : strslice.StrSlice {"./" + handler },
240
- }, nil
241
- default :
242
- return run.LaunchOpts {}, errors .New ("could not get launchOpts from " + handler + ", runtime not supported" )
243
- }
244
- }
245
-
246
218
// collectOne - Collects information about a function for a nitric stack
247
219
// handler - the specific handler for the application
248
220
func (c * codeConfig ) collectOne (handler string ) error {
@@ -271,26 +243,24 @@ func (c *codeConfig) collectOne(handler string) error {
271
243
// Specify the service bind as the port with the docker gateway IP (running in bridge mode)
272
244
ce , err := containerengine .Discover ()
273
245
if err != nil {
274
- return errors .WithMessage (err , "error running the handler " )
246
+ return errors .WithMessage (err , "error discovering container engine " )
275
247
}
276
248
277
- opts , err := launchOptsForFunctionCollect (c .initialStack .Dir , handler )
249
+ rt , err := runtime .NewRunTimeFromHandler (handler )
250
+ if err != nil {
251
+ return errors .WithMessage (err , "error getting the runtime from handler " + handler )
252
+ }
253
+
254
+ opts , err := rt .LaunchOptsForFunctionCollect (c .initialStack .Dir )
278
255
if err != nil {
279
256
return err
280
257
}
281
- fmt .Println (opts .String ())
282
258
283
259
hostConfig := & container.HostConfig {
284
260
AutoRemove : true ,
285
- Mounts : []mount.Mount {
286
- {
287
- Type : "bind" ,
288
- Source : c .initialStack .Dir ,
289
- Target : opts .TargetWD ,
290
- },
291
- },
261
+ Mounts : opts .Mounts ,
292
262
}
293
- if runtime .GOOS == "linux" {
263
+ if osruntime .GOOS == "linux" {
294
264
// setup host.docker.internal to route to host gateway
295
265
// to access rpc server hosted by local CLI run
296
266
hostConfig .ExtraHosts = []string {"host.docker.internal:172.17.0.1" }
@@ -308,7 +278,7 @@ func (c *codeConfig) collectOne(handler string) error {
308
278
Cmd : opts .Cmd ,
309
279
Entrypoint : opts .Entrypoint ,
310
280
WorkingDir : opts .TargetWD ,
311
- }, hostConfig , nil , containerNameFromHandler ( handler ))
281
+ }, hostConfig , nil , rt . ContainerName ( ))
312
282
if err != nil {
313
283
return err
314
284
}
@@ -356,11 +326,6 @@ func (c *codeConfig) collectOne(handler string) error {
356
326
return errs .Aggregate ()
357
327
}
358
328
359
- func containerNameFromHandler (handler string ) string {
360
- rt , _ := utils .NewRunTimeFromFilename (handler )
361
- return rt .ContainerName (handler )
362
- }
363
-
364
329
func (c * codeConfig ) addFunction (fun * FunctionDependencies , handler string ) {
365
330
c .lock .Lock ()
366
331
defer c .lock .Unlock ()
@@ -378,7 +343,11 @@ func (c *codeConfig) ToStack() (*stack.Stack, error) {
378
343
379
344
errs := utils .NewErrorList ()
380
345
for handler , f := range c .functions {
381
- name := containerNameFromHandler (handler )
346
+ rt , err := runtime .NewRunTimeFromHandler (handler )
347
+ if err != nil {
348
+ return nil , err
349
+ }
350
+
382
351
topicTriggers := make ([]string , 0 , len (f .subscriptions )+ len (f .schedules ))
383
352
384
353
for k := range f .apis {
@@ -459,14 +428,14 @@ func (c *codeConfig) ToStack() (*stack.Stack, error) {
459
428
}
460
429
}
461
430
462
- f , ok := s .Functions [name ]
431
+ f , ok := s .Functions [rt . ContainerName () ]
463
432
if ! ok {
464
433
f = stack .FunctionFromHandler (handler , s .Dir )
465
434
}
466
435
f .ComputeUnit .Triggers = stack.Triggers {
467
436
Topics : topicTriggers ,
468
437
}
469
- s .Functions [name ] = f
438
+ s .Functions [rt . ContainerName () ] = f
470
439
}
471
440
472
441
return s , errs .Aggregate ()
0 commit comments