Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-cli/cmd/open.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
"os"
9
"os/exec"
10
"time"
11
12
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor"
13
14
"context"
15
16
"github.com/google/shlex"
17
"github.com/spf13/cobra"
18
"golang.org/x/xerrors"
19
)
20
21
// initCmd represents the init command
22
var openCmd = &cobra.Command{
23
Use: "open <filename>",
24
Short: "Opens a file in Gitpod",
25
Args: cobra.MinimumNArgs(1),
26
RunE: func(cmd *cobra.Command, args []string) error {
27
// TODO(ak) use NotificationService.NotifyActive supervisor API instead
28
29
ctx, cancel := context.WithTimeout(cmd.Context(), 10*time.Second)
30
defer cancel()
31
32
client, err := supervisor.New(ctx)
33
if err != nil {
34
return err
35
}
36
defer client.Close()
37
38
client.WaitForIDEReady(ctx)
39
40
wait, _ := cmd.Flags().GetBool("wait")
41
42
pcmd := os.Getenv("GP_OPEN_EDITOR")
43
if pcmd == "" {
44
return xerrors.Errorf("GP_OPEN_EDITOR is not set")
45
}
46
pargs, err := shlex.Split(pcmd)
47
if err != nil {
48
return xerrors.Errorf("cannot parse GP_OPEN_EDITOR: %w", err)
49
}
50
if len(pargs) > 1 {
51
pcmd = pargs[0]
52
}
53
pcmd, err = exec.LookPath(pcmd)
54
if err != nil {
55
return err
56
}
57
58
if wait {
59
pargs = append(pargs, "--wait")
60
ctx = cmd.Context()
61
}
62
c := exec.CommandContext(ctx, pcmd, append(pargs[1:], args...)...)
63
c.Stdin = os.Stdin
64
c.Stdout = os.Stdout
65
c.Stderr = os.Stderr
66
err = c.Run()
67
if err != nil {
68
if ctx.Err() != nil {
69
return xerrors.Errorf("editor failed to open in time: %w", ctx.Err())
70
}
71
72
return xerrors.Errorf("editor failed to open: %w", err)
73
}
74
75
return nil
76
},
77
}
78
79
func init() {
80
rootCmd.AddCommand(openCmd)
81
openCmd.Flags().BoolP("wait", "w", false, "wait until all opened files are closed again")
82
}
83
84