MCPcopy Create free account
hub / github.com/cli/cli / ForwardPorts

Method ForwardPorts

pkg/cmd/codespace/ports.go:325–366  ·  view source on GitHub ↗
(ctx context.Context, selector *CodespaceSelector, ports []string, allInterfaces bool)

Source from the content-addressed store, hash-verified

323}
324
325func (a *App) ForwardPorts(ctx context.Context, selector *CodespaceSelector, ports []string, allInterfaces bool) (err error) {
326 portPairs, err := getPortPairs(ports)
327 if err != nil {
328 return fmt.Errorf("get port pairs: %w", err)
329 }
330
331 codespace, err := selector.Select(ctx)
332 if err != nil {
333 return err
334 }
335
336 codespaceConnection, err := codespaces.GetCodespaceConnection(ctx, a, a.apiClient, codespace)
337 if err != nil {
338 return fmt.Errorf("error connecting to codespace: %w", err)
339 }
340
341 // Run forwarding of all ports concurrently, aborting all of
342 // them at the first failure, including cancellation of the context.
343 group, ctx := errgroup.WithContext(ctx)
344 for _, pair := range portPairs {
345 group.Go(func() error {
346 listen, _, err := codespaces.ListenTCP(pair.local, allInterfaces)
347 if err != nil {
348 return err
349 }
350 defer listen.Close()
351
352 a.errLogger.Printf("Forwarding ports: remote %d <=> local %s", pair.remote, listen.Addr())
353 fwd, err := portforwarder.NewPortForwarder(ctx, codespaceConnection)
354 if err != nil {
355 return fmt.Errorf("failed to create port forwarder: %w", err)
356 }
357 defer safeClose(fwd, &err)
358
359 opts := portforwarder.ForwardPortOpts{
360 Port: pair.remote,
361 }
362 return fwd.ForwardPortToListener(ctx, opts, listen)
363 })
364 }
365 return group.Wait() // first error
366}
367
368type portPair struct {
369 remote, local int

Callers 2

newPortsForwardCmdFunction · 0.80

Calls 11

ForwardPortToListenerMethod · 0.95
GetCodespaceConnectionFunction · 0.92
ListenTCPFunction · 0.92
NewPortForwarderFunction · 0.92
getPortPairsFunction · 0.85
AddrMethod · 0.80
safeCloseFunction · 0.70
ErrorfMethod · 0.65
SelectMethod · 0.65
CloseMethod · 0.65
PrintfMethod · 0.65