MCPcopy Index your code
hub / github.com/cli/cli / NewCodespaceConnection

Function NewCodespaceConnection

internal/codespaces/connection/connection.go:42–86  ·  view source on GitHub ↗

NewCodespaceConnection initializes a connection to a codespace. This connections allows for port forwarding which enables the use of most features of the codespace command.

(ctx context.Context, codespace *api.Codespace, httpClient *http.Client)

Source from the content-addressed store, hash-verified

40// This connections allows for port forwarding which enables the
41// use of most features of the codespace command.
42func NewCodespaceConnection(ctx context.Context, codespace *api.Codespace, httpClient *http.Client) (connection *CodespaceConnection, err error) {
43 // Get the tunnel properties
44 tunnelProperties := codespace.Connection.TunnelProperties
45
46 // Create the tunnel manager
47 tunnelManager, err := getTunnelManager(tunnelProperties, httpClient)
48 if err != nil {
49 return nil, fmt.Errorf("error getting tunnel management client: %w", err)
50 }
51
52 // Calculate allowed port privacy settings
53 allowedPortPrivacySettings := codespace.RuntimeConstraints.AllowedPortPrivacySettings
54
55 // Get the access tokens
56 connectToken := tunnelProperties.ConnectAccessToken
57 managementToken := tunnelProperties.ManagePortsAccessToken
58
59 // Create the tunnel definition
60 tunnel := &tunnels.Tunnel{
61 AccessTokens: map[tunnels.TunnelAccessScope]string{tunnels.TunnelAccessScopeConnect: connectToken, tunnels.TunnelAccessScopeManagePorts: managementToken},
62 TunnelID: tunnelProperties.TunnelId,
63 ClusterID: tunnelProperties.ClusterId,
64 Domain: tunnelProperties.Domain,
65 }
66
67 // Create options
68 options := &tunnels.TunnelRequestOptions{
69 IncludePorts: true,
70 }
71
72 // Create the tunnel client (not connected yet)
73 tunnelClient, err := getTunnelClient(ctx, tunnelManager, tunnel, options)
74 if err != nil {
75 return nil, fmt.Errorf("error getting tunnel client: %w", err)
76 }
77
78 return &CodespaceConnection{
79 tunnelProperties: tunnelProperties,
80 TunnelManager: tunnelManager,
81 TunnelClient: tunnelClient,
82 Options: options,
83 Tunnel: tunnel,
84 AllowedPortPrivacySettings: allowedPortPrivacySettings,
85 }, nil
86}
87
88// Connect connects the client to the tunnel.
89func (c *CodespaceConnection) Connect(ctx context.Context) error {

Callers 4

GetCodespaceConnectionFunction · 0.92
TestNewPortForwarderFunction · 0.92

Calls 3

getTunnelManagerFunction · 0.85
getTunnelClientFunction · 0.85
ErrorfMethod · 0.65

Tested by 3

TestNewPortForwarderFunction · 0.74