Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-cli/cmd/ports-protocol.go
2498 views
1
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cmd
6
7
import (
8
"context"
9
"fmt"
10
"strconv"
11
"strings"
12
"time"
13
14
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/gitpod"
15
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor"
16
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/utils"
17
serverapi "github.com/gitpod-io/gitpod/gitpod-protocol"
18
"github.com/gitpod-io/gitpod/supervisor/api"
19
"github.com/spf13/cobra"
20
"golang.org/x/xerrors"
21
)
22
23
// portsProtocolCmd change protocol of port
24
var portsProtocolCmd = &cobra.Command{
25
Use: "protocol <port:{http|https}>",
26
Short: "Set port protocol",
27
Args: cobra.ExactArgs(1),
28
RunE: func(cmd *cobra.Command, args []string) error {
29
// TODO: we can add protocol for analysis later.
30
portProtocol := args[0]
31
s := strings.Split(portProtocol, ":")
32
if len(s) != 2 {
33
return GpError{Err: xerrors.Errorf("cannot parse args, should be something like `3000:http` or `3000:https`"), OutCome: utils.Outcome_UserErr, ErrorCode: utils.UserErrorCode_InvalidArguments}
34
}
35
port, err := strconv.Atoi(s[0])
36
if err != nil {
37
return GpError{Err: xerrors.Errorf("port should be integer"), OutCome: utils.Outcome_UserErr, ErrorCode: utils.UserErrorCode_InvalidArguments}
38
}
39
protocol := s[1]
40
if protocol != serverapi.PortProtocolHTTP && protocol != serverapi.PortProtocolHTTPS {
41
return GpError{Err: xerrors.Errorf("protocol should be `%s` or `%s`", serverapi.PortProtocolHTTP, serverapi.PortProtocolHTTPS), OutCome: utils.Outcome_UserErr, ErrorCode: utils.UserErrorCode_InvalidArguments}
42
}
43
ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Second)
44
defer cancel()
45
46
supervisorClient, err := supervisor.New(ctx)
47
if err != nil {
48
return err
49
}
50
defer supervisorClient.Close()
51
52
ports, err := supervisorClient.GetPortsList(ctx)
53
if err != nil {
54
return err
55
}
56
var prePortStatus *api.PortsStatus
57
for _, p := range ports {
58
if p.LocalPort == uint32(port) {
59
prePortStatus = p
60
break
61
}
62
}
63
wsInfo, err := gitpod.GetWSInfo(ctx)
64
if err != nil {
65
return xerrors.Errorf("cannot get workspace info, %w", err)
66
}
67
client, err := gitpod.ConnectToServer(ctx, wsInfo, []string{
68
"function:openPort",
69
"resource:workspace::" + wsInfo.WorkspaceId + "::get/update",
70
})
71
if err != nil {
72
return xerrors.Errorf("cannot connect to server, %w", err)
73
}
74
defer client.Close()
75
params := &serverapi.WorkspaceInstancePort{
76
Port: float64(port),
77
Protocol: protocol,
78
}
79
if prePortStatus != nil && prePortStatus.Exposed != nil {
80
params.Visibility = prePortStatus.Exposed.Visibility.String()
81
}
82
83
if _, err := client.OpenPort(ctx, wsInfo.WorkspaceId, params); err != nil {
84
return xerrors.Errorf("failed to change port protocol: %w", err)
85
}
86
fmt.Printf("port %v is now %s\n", port, protocol)
87
return nil
88
},
89
}
90
91
func init() {
92
portsCmd.AddCommand(portsProtocolCmd)
93
}
94
95