Path: blob/main/components/gitpod-cli/cmd/ports-protocol.go
2498 views
// Copyright (c) 2023 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 cmd56import (7"context"8"fmt"9"strconv"10"strings"11"time"1213"github.com/gitpod-io/gitpod/gitpod-cli/pkg/gitpod"14"github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor"15"github.com/gitpod-io/gitpod/gitpod-cli/pkg/utils"16serverapi "github.com/gitpod-io/gitpod/gitpod-protocol"17"github.com/gitpod-io/gitpod/supervisor/api"18"github.com/spf13/cobra"19"golang.org/x/xerrors"20)2122// portsProtocolCmd change protocol of port23var portsProtocolCmd = &cobra.Command{24Use: "protocol <port:{http|https}>",25Short: "Set port protocol",26Args: cobra.ExactArgs(1),27RunE: func(cmd *cobra.Command, args []string) error {28// TODO: we can add protocol for analysis later.29portProtocol := args[0]30s := strings.Split(portProtocol, ":")31if len(s) != 2 {32return GpError{Err: xerrors.Errorf("cannot parse args, should be something like `3000:http` or `3000:https`"), OutCome: utils.Outcome_UserErr, ErrorCode: utils.UserErrorCode_InvalidArguments}33}34port, err := strconv.Atoi(s[0])35if err != nil {36return GpError{Err: xerrors.Errorf("port should be integer"), OutCome: utils.Outcome_UserErr, ErrorCode: utils.UserErrorCode_InvalidArguments}37}38protocol := s[1]39if protocol != serverapi.PortProtocolHTTP && protocol != serverapi.PortProtocolHTTPS {40return GpError{Err: xerrors.Errorf("protocol should be `%s` or `%s`", serverapi.PortProtocolHTTP, serverapi.PortProtocolHTTPS), OutCome: utils.Outcome_UserErr, ErrorCode: utils.UserErrorCode_InvalidArguments}41}42ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Second)43defer cancel()4445supervisorClient, err := supervisor.New(ctx)46if err != nil {47return err48}49defer supervisorClient.Close()5051ports, err := supervisorClient.GetPortsList(ctx)52if err != nil {53return err54}55var prePortStatus *api.PortsStatus56for _, p := range ports {57if p.LocalPort == uint32(port) {58prePortStatus = p59break60}61}62wsInfo, err := gitpod.GetWSInfo(ctx)63if err != nil {64return xerrors.Errorf("cannot get workspace info, %w", err)65}66client, err := gitpod.ConnectToServer(ctx, wsInfo, []string{67"function:openPort",68"resource:workspace::" + wsInfo.WorkspaceId + "::get/update",69})70if err != nil {71return xerrors.Errorf("cannot connect to server, %w", err)72}73defer client.Close()74params := &serverapi.WorkspaceInstancePort{75Port: float64(port),76Protocol: protocol,77}78if prePortStatus != nil && prePortStatus.Exposed != nil {79params.Visibility = prePortStatus.Exposed.Visibility.String()80}8182if _, err := client.OpenPort(ctx, wsInfo.WorkspaceId, params); err != nil {83return xerrors.Errorf("failed to change port protocol: %w", err)84}85fmt.Printf("port %v is now %s\n", port, protocol)86return nil87},88}8990func init() {91portsCmd.AddCommand(portsProtocolCmd)92}939495