Path: blob/main/components/gitpod-cli/pkg/supervisor/client.go
2500 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package supervisor56import (7"context"8"sync"9"time"1011"golang.org/x/xerrors"12"google.golang.org/grpc"13"google.golang.org/grpc/credentials/insecure"1415"github.com/gitpod-io/gitpod/common-go/util"16"github.com/gitpod-io/gitpod/supervisor/api"17)1819type SupervisorClient struct {20conn *grpc.ClientConn21closeOnce sync.Once2223Status api.StatusServiceClient24Terminal api.TerminalServiceClient25Info api.InfoServiceClient26Notification api.NotificationServiceClient27Control api.ControlServiceClient28Token api.TokenServiceClient29}3031type SupervisorClientOption struct {32Address string33}3435func New(ctx context.Context, options ...*SupervisorClientOption) (*SupervisorClient, error) {36address := util.GetSupervisorAddress()37for _, option := range options {38if option.Address != "" {39address = option.Address40}41}42conn, err := grpc.DialContext(ctx, address, grpc.WithTransportCredentials(insecure.NewCredentials()))43if err != nil {44return nil, xerrors.Errorf("failed connecting to supervisor: %w", err)45}4647return &SupervisorClient{48conn: conn,49Status: api.NewStatusServiceClient(conn),50Terminal: api.NewTerminalServiceClient(conn),51Info: api.NewInfoServiceClient(conn),52Notification: api.NewNotificationServiceClient(conn),53Control: api.NewControlServiceClient(conn),54Token: api.NewTokenServiceClient(conn),55}, nil56}5758func (client *SupervisorClient) Close() {59client.closeOnce.Do(func() {60client.conn.Close()61})62}6364func (client *SupervisorClient) WaitForIDEReady(ctx context.Context) {65var ideReady bool66for !ideReady {67resp, _ := client.Status.IDEStatus(ctx, &api.IDEStatusRequest{Wait: true})68if resp != nil {69ideReady = resp.Ok70}71if ctx.Err() != nil {72return73}74if !ideReady {75time.Sleep(1 * time.Second)76}77}78}798081