cli/internal/codespaces/codespaces.go
Jose Garcia 3a28643630 Keep codespace struct in API for now
- Use a private codespace structure in the cmd pkg to encapsulate common
  behavior
2021-10-06 11:44:26 -04:00

76 lines
2.1 KiB
Go

package codespaces
import (
"context"
"errors"
"fmt"
"time"
"github.com/cli/cli/v2/internal/codespaces/api"
"github.com/cli/cli/v2/pkg/liveshare"
)
type logger interface {
Print(v ...interface{}) (int, error)
Println(v ...interface{}) (int, error)
}
func connectionReady(cs *api.Codespace) bool {
return cs.Environment.Connection.SessionID != "" &&
cs.Environment.Connection.SessionToken != "" &&
cs.Environment.Connection.RelayEndpoint != "" &&
cs.Environment.Connection.RelaySAS != "" &&
cs.Environment.State == api.CodespaceEnvironmentStateAvailable
}
type apiClient interface {
GetCodespace(ctx context.Context, name string, includeConnection bool) (*api.Codespace, error)
StartCodespace(ctx context.Context, name string) error
}
// ConnectToLiveshare waits for a Codespace to become running,
// and connects to it using a Live Share session.
func ConnectToLiveshare(ctx context.Context, log logger, apiClient apiClient, cs *api.Codespace) (*liveshare.Session, error) {
var startedCodespace bool
if cs.Environment.State != api.CodespaceEnvironmentStateAvailable {
startedCodespace = true
log.Print("Starting your codespace...")
if err := apiClient.StartCodespace(ctx, cs.Name); err != nil {
return nil, fmt.Errorf("error starting codespace: %w", err)
}
}
for retries := 0; !connectionReady(cs); retries++ {
if retries > 1 {
if retries%2 == 0 {
log.Print(".")
}
time.Sleep(1 * time.Second)
}
if retries == 30 {
return nil, errors.New("timed out while waiting for the codespace to start")
}
var err error
cs, err = apiClient.GetCodespace(ctx, cs.Name, true)
if err != nil {
return nil, fmt.Errorf("error getting codespace: %w", err)
}
}
if startedCodespace {
fmt.Print("\n")
}
log.Println("Connecting to your codespace...")
return liveshare.Connect(ctx, liveshare.Options{
SessionID: cs.Environment.Connection.SessionID,
SessionToken: cs.Environment.Connection.SessionToken,
RelaySAS: cs.Environment.Connection.RelaySAS,
RelayEndpoint: cs.Environment.Connection.RelayEndpoint,
HostPublicKeys: cs.Environment.Connection.HostPublicKeys,
})
}