MCPcopy
hub / github.com/cli/cli / ListPorts

Method ListPorts

pkg/cmd/codespace/ports.go:53–125  ·  view source on GitHub ↗

ListPorts lists known ports in a codespace.

(ctx context.Context, selector *CodespaceSelector, exporter cmdutil.Exporter)

Source from the content-addressed store, hash-verified

51
52// ListPorts lists known ports in a codespace.
53func (a *App) ListPorts(ctx context.Context, selector *CodespaceSelector, exporter cmdutil.Exporter) (err error) {
54 codespace, err := selector.Select(ctx)
55 if err != nil {
56 return err
57 }
58
59 devContainerCh := getDevContainer(ctx, a.apiClient, codespace)
60
61 codespaceConnection, err := codespaces.GetCodespaceConnection(ctx, a, a.apiClient, codespace)
62 if err != nil {
63 return fmt.Errorf("error connecting to codespace: %w", err)
64 }
65
66 fwd, err := portforwarder.NewPortForwarder(ctx, codespaceConnection)
67 if err != nil {
68 return fmt.Errorf("failed to create port forwarder: %w", err)
69 }
70 defer safeClose(fwd, &err)
71
72 var ports []*tunnels.TunnelPort
73 err = a.RunWithProgress("Fetching ports", func() (err error) {
74 ports, err = fwd.ListPorts(ctx)
75 return
76 })
77 if err != nil {
78 return fmt.Errorf("error getting ports of shared servers: %w", err)
79 }
80
81 devContainerResult := <-devContainerCh
82 if devContainerResult.err != nil {
83 // Warn about failure to read the devcontainer file. Not a codespace command error.
84 a.errLogger.Printf("Failed to get port names: %v", devContainerResult.err.Error())
85 }
86
87 var portInfos []*portInfo
88
89 for _, p := range ports {
90 // filter out internal ports from list
91 if portforwarder.IsInternalPort(p) {
92 continue
93 }
94
95 portInfos = append(portInfos, &portInfo{
96 Port: p,
97 codespace: codespace,
98 devContainer: devContainerResult.devContainer,
99 })
100 }
101
102 if err := a.io.StartPager(); err != nil {
103 a.errLogger.Printf("error starting pager: %v", err)
104 }
105 defer a.io.StopPager()
106
107 if exporter != nil {
108 return exporter.Write(a.io, portInfos)
109 }
110

Callers 1

TestListPortsFunction · 0.95

Calls 15

RunWithProgressMethod · 0.95
ListPortsMethod · 0.95
GetCodespaceConnectionFunction · 0.92
NewPortForwarderFunction · 0.92
IsInternalPortFunction · 0.92
NewFunction · 0.92
WithHeaderFunction · 0.92
getDevContainerFunction · 0.85
StartPagerMethod · 0.80
StopPagerMethod · 0.80
ColorSchemeMethod · 0.80

Tested by 1

TestListPortsFunction · 0.76