MCPcopy
hub / github.com/cli/cli / NewCodespaceConnection

Function NewCodespaceConnection

internal/codespaces/connection/connection.go:38–82  ·  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

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

Callers 3

GetCodespaceConnectionFunction · 0.92
TestNewPortForwarderFunction · 0.92

Calls 3

getTunnelManagerFunction · 0.85
getTunnelClientFunction · 0.85
ErrorfMethod · 0.65

Tested by 2

TestNewPortForwarderFunction · 0.74