11package main
22
33import (
4- "bufio"
54 "context"
65 "fmt"
76 "os"
8- "strings"
9- "time"
107
118 "github.com/github/ghcs/api"
129 "github.com/github/ghcs/cmd/ghcs/output"
1310 "github.com/github/ghcs/internal/codespaces"
14- "github.com/github/go-liveshare"
1511 "github.com/spf13/cobra"
1612)
1713
@@ -59,33 +55,23 @@ func SSH(sshProfile, codespaceName string, sshServerPort int) error {
5955 return fmt .Errorf ("error connecting to liveshare: %v" , err )
6056 }
6157
62- terminal , err := liveshare . NewTerminal ( lsclient )
58+ result , remoteSSHServerPort , sshUser , _ , err := codespaces . StartSSHServer ( ctx , lsclient )
6359 if err != nil {
64- return fmt .Errorf ("error creating liveshare terminal : %v" , err )
60+ return fmt .Errorf ("error getting ssh server details : %v" , err )
6561 }
6662
67- log .Println ("Preparing SSH..." )
68- if sshProfile == "" {
69- containerID , err := getContainerID (ctx , log , terminal )
70- if err != nil {
71- return fmt .Errorf ("error getting container id: %v" , err )
72- }
73-
74- if err := setupSSH (ctx , log , terminal , containerID , codespace .RepositoryName ); err != nil {
75- return fmt .Errorf ("error creating ssh server: %v" , err )
76- }
77-
78- log .Print ("\n " )
63+ if ! result {
64+ return fmt .Errorf ("error starting ssh: %v" , err )
7965 }
8066
81- tunnelPort , tunnelClosed , err := codespaces .MakeSSHTunnel (ctx , lsclient , sshServerPort )
67+ tunnelPort , tunnelClosed , err := codespaces .MakeSSHTunnel (ctx , lsclient , sshServerPort , remoteSSHServerPort )
8268 if err != nil {
8369 return fmt .Errorf ("make ssh tunnel: %v" , err )
8470 }
8571
8672 connectDestination := sshProfile
8773 if connectDestination == "" {
88- connectDestination = fmt .Sprintf ("%s@localhost" , getSSHUser ( codespace ) )
74+ connectDestination = fmt .Sprintf ("%s@localhost" , sshUser )
8975 }
9076
9177 usingCustomPort := tunnelPort == sshServerPort
@@ -105,65 +91,3 @@ func SSH(sshProfile, codespaceName string, sshServerPort int) error {
10591
10692 return nil
10793}
108-
109- func getContainerID (ctx context.Context , logger * output.Logger , terminal * liveshare.Terminal ) (string , error ) {
110- logger .Print ("." )
111-
112- cmd := terminal .NewCommand (
113- "/" ,
114- "/usr/bin/docker ps -aq --filter label=Type=codespaces --filter status=running" ,
115- )
116-
117- stream , err := cmd .Run (ctx )
118- if err != nil {
119- return "" , fmt .Errorf ("error running command: %v" , err )
120- }
121-
122- logger .Print ("." )
123- scanner := bufio .NewScanner (stream )
124- scanner .Scan ()
125-
126- logger .Print ("." )
127- containerID := scanner .Text ()
128- if err := scanner .Err (); err != nil {
129- return "" , fmt .Errorf ("error scanning stream: %v" , err )
130- }
131-
132- logger .Print ("." )
133- if err := stream .Close (); err != nil {
134- return "" , fmt .Errorf ("error closing stream: %v" , err )
135- }
136-
137- return containerID , nil
138- }
139-
140- func setupSSH (ctx context.Context , logger * output.Logger , terminal * liveshare.Terminal , containerID , repositoryName string ) error {
141- setupBashProfileCmd := fmt .Sprintf (`echo "cd /workspaces/%v; export $(cat /workspaces/.codespaces/shared/.env | xargs); exec /bin/zsh;" > /home/codespace/.bash_profile` , repositoryName )
142-
143- logger .Print ("." )
144- compositeCommand := []string {setupBashProfileCmd }
145- cmd := terminal .NewCommand (
146- "/" ,
147- fmt .Sprintf ("/usr/bin/docker exec -t %s /bin/bash -c '" + strings .Join (compositeCommand , "; " )+ "'" , containerID ),
148- )
149- stream , err := cmd .Run (ctx )
150- if err != nil {
151- return fmt .Errorf ("error running command: %v" , err )
152- }
153-
154- logger .Print ("." )
155- if err := stream .Close (); err != nil {
156- return fmt .Errorf ("error closing stream: %v" , err )
157- }
158-
159- time .Sleep (1 * time .Second )
160-
161- return nil
162- }
163-
164- func getSSHUser (codespace * api.Codespace ) string {
165- if codespace .RepositoryNWO == "github/github" {
166- return "root"
167- }
168- return "codespace"
169- }
0 commit comments