gitpod/components/supervisor/cmd/call-server.go
Christian Weichel 7935b7f015 [gitpod-protocol] Move Go server API client to gitpod-protocol
This way it's accessible from packages other than supervisor,
e.g. future integration tests.
2021-01-08 13:40:43 +01:00

64 lines
1.5 KiB
Go

// Copyright (c) 2020 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.
package cmd
import (
"context"
"encoding/json"
"fmt"
"os"
"github.com/gitpod-io/gitpod/common-go/log"
gitpod "github.com/gitpod-io/gitpod/gitpod-protocol"
"github.com/spf13/cobra"
)
var callServerCmd = &cobra.Command{
Use: "call-server <host> <token>",
Hidden: true,
Args: cobra.MinimumNArgs(2),
Run: func(cmd *cobra.Command, args []string) {
var (
ctx, cancel = context.WithCancel(context.Background())
host = args[0]
token = args[1]
)
defer cancel()
api, err := gitpod.ConnectToServer(fmt.Sprintf("ws://%s/api/v1", host), gitpod.ConnectToServerOpts{
Token: token,
})
if err != nil {
log.WithError(err).Fatal("ConnectToServer")
}
defer api.Close()
usr, err := api.GetLoggedInUser(ctx)
if err != nil {
log.WithError(err).Fatal("GetLoggedInUser")
}
json.NewEncoder(os.Stdout).Encode(usr)
enc := json.NewEncoder(os.Stdout)
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
instanceID, _ := cmd.Flags().GetString("instance-id")
updates, err := api.InstanceUpdates(ctx, instanceID)
if err != nil {
log.WithError(err).Fatal("InstanceUpdates")
}
for u := range updates {
enc.Encode(u)
}
},
}
func init() {
rootCmd.AddCommand(callServerCmd)
callServerCmd.Flags().String("instance-id", os.Getenv("GITPOD_INSTANCE_ID"), "instance ID to listen for")
}