Path: blob/main/components/gitpod-cli/cmd/git-track-command.go
2498 views
// Copyright (c) 2021 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package cmd56import (7"context"8"io"9"os"10"time"1112log "github.com/sirupsen/logrus"13"github.com/spf13/cobra"1415"github.com/gitpod-io/gitpod/gitpod-cli/pkg/gitpod"16"github.com/gitpod-io/gitpod/gitpod-cli/pkg/utils"17serverapi "github.com/gitpod-io/gitpod/gitpod-protocol"18)1920var gitTrackCommandOpts struct {21GitCommand string22}2324var gitTrackCommand = &cobra.Command{25Use: "git-track-command",26Short: "Gitpod's Git command tracker",27Long: "Sending anonymous statistics about the executed git commands inside a workspace",28Args: cobra.ExactArgs(0),29Hidden: true,30RunE: func(cmd *cobra.Command, args []string) error {31// ignore trace32utils.TrackCommandUsageEvent.Command = nil3334log.SetOutput(io.Discard)35f, err := os.OpenFile(os.TempDir()+"/gitpod-git-credential-helper.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)36if err == nil {37defer f.Close()38log.SetOutput(f)39}4041log.Infof("gp git-track-command")4243ctx, cancel := context.WithTimeout(cmd.Context(), 1*time.Minute)44defer cancel()45wsInfo, err := gitpod.GetWSInfo(ctx)46if err != nil {47return err48}4950client, err := gitpod.ConnectToServer(ctx, wsInfo, []string{"function:trackEvent"})5152if err != nil {53log.WithError(err).Fatal("error connecting to supervisor")54}5556defer client.Close()5758type GitEventParams struct {59Command string `json:"command,omitempty"`60WorkspaceId string `json:"workspaceId,omitempty"`61// most often used across all events62InstanceId string `json:"instanceId,omitempty"`63// deprecated for backward compatibility64WorkspaceInstanceId string `json:"workspaceInstanceId,omitempty"`65Timestamp int64 `json:"timestamp,omitempty"`66}6768params := &GitEventParams{69Command: gitTrackCommandOpts.GitCommand,70WorkspaceId: wsInfo.WorkspaceId,71InstanceId: wsInfo.InstanceId,72WorkspaceInstanceId: wsInfo.InstanceId,73Timestamp: time.Now().Unix(),74}75event := &serverapi.RemoteTrackMessage{76Event: "git_command",77Properties: *params,78}79log.WithField("command", gitTrackCommandOpts.GitCommand).80Info("tracking the GitCommand event")8182// TODO(ak) use segment directly + supervisor info to get workspace and isntance IDs, don't use server83err = client.TrackEvent(ctx, event)84if err != nil {85log.WithError(err).Fatal("error tracking git event")86}87return nil88},89}9091func init() {92rootCmd.AddCommand(gitTrackCommand)93gitTrackCommand.Flags().StringVarP(&gitTrackCommandOpts.GitCommand, "gitCommand", "c", "", "The Git command to be recorded")94gitTrackCommand.MarkFlagRequired("gitCommand")95}969798