Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-cli/cmd/url.go
2498 views
1
// Copyright (c) 2020 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
"fmt"
9
"os"
10
"strconv"
11
"strings"
12
13
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/utils"
14
"github.com/spf13/cobra"
15
"golang.org/x/xerrors"
16
)
17
18
// urlCmd represents the url command
19
var urlCmd = &cobra.Command{
20
Use: "url [port]",
21
Short: "Prints the URL of this workspace",
22
Long: `Prints the URL of this workspace. This command can print the URL of
23
the current workspace itself, or of a service running in this workspace on a
24
particular port. For example:
25
gp url 8080
26
will print the URL of a service/server exposed on port 8080.`,
27
Args: cobra.MaximumNArgs(1),
28
RunE: func(cmd *cobra.Command, args []string) error {
29
if len(args) == 0 {
30
fmt.Println(os.Getenv("GITPOD_WORKSPACE_URL"))
31
return nil
32
}
33
34
port, err := strconv.ParseUint(args[0], 10, 16)
35
if err != nil {
36
return GpError{Err: xerrors.Errorf("port \"%s\" is not a valid number", args[0]), OutCome: utils.Outcome_UserErr, ErrorCode: utils.UserErrorCode_InvalidArguments}
37
}
38
39
fmt.Println(GetWorkspaceURL(int(port)))
40
return nil
41
},
42
}
43
44
func init() {
45
rootCmd.AddCommand(urlCmd)
46
}
47
48
func GetWorkspaceURL(port int) (url string) {
49
wsurl := os.Getenv("GITPOD_WORKSPACE_URL")
50
if port == 0 {
51
return wsurl
52
}
53
54
serviceurl := wsurl
55
serviceurl = strings.Replace(serviceurl, "https://", fmt.Sprintf("https://%d-", port), -1)
56
serviceurl = strings.Replace(serviceurl, "http://", fmt.Sprintf("http://%d-", port), -1)
57
return serviceurl
58
}
59
60