Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/supervisor/cmd/call-server.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
"encoding/json"
10
"fmt"
11
"os"
12
13
"github.com/spf13/cobra"
14
15
"github.com/gitpod-io/gitpod/common-go/log"
16
gitpod "github.com/gitpod-io/gitpod/gitpod-protocol"
17
)
18
19
var callServerCmd = &cobra.Command{
20
Use: "call-server <host> <token>",
21
Hidden: true,
22
Args: cobra.MinimumNArgs(2),
23
Run: func(cmd *cobra.Command, args []string) {
24
var (
25
ctx, cancel = context.WithCancel(context.Background())
26
host = args[0]
27
token = args[1]
28
)
29
defer cancel()
30
31
api, err := gitpod.ConnectToServer(fmt.Sprintf("ws://%s/api/v1", host), gitpod.ConnectToServerOpts{
32
Token: token,
33
Log: log.Log,
34
})
35
if err != nil {
36
log.WithError(err).Fatal("ConnectToServer")
37
}
38
defer api.Close()
39
40
usr, err := api.GetLoggedInUser(ctx)
41
if err != nil {
42
log.WithError(err).Fatal("GetLoggedInUser")
43
}
44
45
err = json.NewEncoder(os.Stdout).Encode(usr)
46
if err != nil {
47
log.WithError(err).Error("encoding user")
48
}
49
50
enc := json.NewEncoder(os.Stdout)
51
enc.SetEscapeHTML(false)
52
enc.SetIndent("", " ")
53
54
workspaceID, _ := cmd.Flags().GetString("workspace-id")
55
updates, err := api.WorkspaceUpdates(ctx, workspaceID)
56
if err != nil {
57
log.WithError(err).Fatal("WorkspaceUpdates")
58
}
59
for u := range updates {
60
err := enc.Encode(u)
61
if err != nil {
62
log.WithError(err).Error("encoding update")
63
}
64
}
65
},
66
}
67
68
func init() {
69
rootCmd.AddCommand(callServerCmd)
70
71
callServerCmd.Flags().String("workspace-id", os.Getenv("GITPOD_WORKSPACE_ID"), "workspace ID to listen for")
72
}
73
74