Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-cli/cmd/preview.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
"context"
9
"os"
10
"os/exec"
11
"regexp"
12
"strconv"
13
"strings"
14
"time"
15
16
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor"
17
"github.com/google/shlex"
18
"github.com/spf13/cobra"
19
"golang.org/x/xerrors"
20
)
21
22
var regexLocalhost = regexp.MustCompile(`((^(localhost|127\.0\.0\.1))|(https?://(localhost|127\.0\.0\.1)))(:[0-9]+)?`)
23
24
var previewCmdOpts struct {
25
External bool
26
}
27
28
// previewCmd represents the preview command
29
var previewCmd = &cobra.Command{
30
Use: "preview <url>",
31
Short: "Opens a URL in the IDE's preview",
32
Args: cobra.ExactArgs(1),
33
RunE: func(cmd *cobra.Command, args []string) error {
34
// TODO(ak) use NotificationService.NotifyActive supervisor API instead
35
36
ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Second)
37
defer cancel()
38
client, err := supervisor.New(ctx)
39
if err != nil {
40
return err
41
}
42
defer client.Close()
43
44
client.WaitForIDEReady(ctx)
45
46
gpBrowserEnvVar := "GP_PREVIEW_BROWSER"
47
48
url := replaceLocalhostInURL(args[0])
49
if previewCmdOpts.External {
50
if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") {
51
url = "https://" + url
52
}
53
gpBrowserEnvVar = "GP_EXTERNAL_BROWSER"
54
}
55
56
return openPreview(gpBrowserEnvVar, url)
57
},
58
}
59
60
func openPreview(gpBrowserEnvVar string, url string) error {
61
pcmd := os.Getenv(gpBrowserEnvVar)
62
if pcmd == "" {
63
return xerrors.Errorf("%s is not set", gpBrowserEnvVar)
64
}
65
pargs, err := shlex.Split(pcmd)
66
if err != nil {
67
return xerrors.Errorf("cannot parse %s: %w", gpBrowserEnvVar, err)
68
}
69
if len(pargs) > 1 {
70
pcmd = pargs[0]
71
}
72
pcmd, err = exec.LookPath(pcmd)
73
if err != nil {
74
return err
75
}
76
77
var args []string
78
for _, parg := range pargs[1:] {
79
if parg == "" {
80
continue
81
}
82
args = append(args, parg)
83
}
84
args = append(args, url)
85
86
previewCmd := exec.Command(pcmd, args...)
87
previewCmd.Stderr = os.Stderr
88
err = previewCmd.Run()
89
if err != nil {
90
return err
91
}
92
93
return nil
94
}
95
96
func replaceLocalhostInURL(url string) string {
97
return regexLocalhost.ReplaceAllStringFunc(url, func(input string) string {
98
hasScheme := strings.HasPrefix(input, "http://") || strings.HasPrefix(input, "https://")
99
input = strings.TrimPrefix(strings.TrimPrefix(input, "http://"), "https://")
100
101
port := 80
102
segs := strings.Split(input, ":")
103
if len(segs) == 2 {
104
port, _ = strconv.Atoi(strings.TrimPrefix(segs[1], ":"))
105
}
106
107
result := GetWorkspaceURL(port)
108
if !hasScheme {
109
result = strings.TrimPrefix(strings.TrimPrefix(result, "http://"), "https://")
110
}
111
return result
112
})
113
}
114
115
func init() {
116
rootCmd.AddCommand(previewCmd)
117
previewCmd.Flags().BoolVar(&previewCmdOpts.External, "external", false, "open the URL in a new browser tab")
118
}
119
120