Skip to content

Commit 15d49e1

Browse files
iQQBotAndrea Falzetti
andcommitted
gitpod-cli: add analytics
Co-authored-by: Andrea Falzetti <andrea@gitpod.io>
1 parent bbc577b commit 15d49e1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+847
-450
lines changed

components/gitpod-cli/cmd/completion.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package cmd
77
import (
88
"os"
99

10+
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/utils"
1011
"github.com/spf13/cobra"
1112
)
1213

@@ -24,8 +25,12 @@ To configure your bash shell to load completions for each session add to your ba
2425
# ~/.bashrc or ~/.profile
2526
. <(gp completion)
2627
`,
27-
Run: func(cmd *cobra.Command, args []string) {
28-
rootCmd.GenBashCompletion(os.Stdout)
28+
RunE: func(cmd *cobra.Command, args []string) error {
29+
// ignore trace
30+
utils.TrackCommandUsageEvent.Command = nil
31+
32+
_ = rootCmd.GenBashCompletion(os.Stdout)
33+
return nil
2934
},
3035
}
3136

components/gitpod-cli/cmd/credential-helper.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ import (
1818
"github.com/prometheus/procfs"
1919
log "github.com/sirupsen/logrus"
2020
"github.com/spf13/cobra"
21+
"golang.org/x/xerrors"
2122
"google.golang.org/grpc"
2223
"google.golang.org/grpc/credentials/insecure"
2324

2425
"github.com/gitpod-io/gitpod/common-go/util"
26+
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/utils"
2527
supervisor "github.com/gitpod-io/gitpod/supervisor/api"
2628
)
2729

@@ -31,7 +33,11 @@ var credentialHelper = &cobra.Command{
3133
Long: "Supports reading of credentials per host.",
3234
Args: cobra.MinimumNArgs(1),
3335
Hidden: true,
34-
Run: func(cmd *cobra.Command, args []string) {
36+
RunE: func(cmd *cobra.Command, args []string) error {
37+
// ignore trace
38+
utils.TrackCommandUsageEvent.Command = nil
39+
40+
exitCode := 0
3541
action := args[0]
3642
log.SetOutput(io.Discard)
3743
f, err := os.OpenFile(os.TempDir()+"/gitpod-git-credential-helper.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
@@ -40,14 +46,14 @@ var credentialHelper = &cobra.Command{
4046
log.SetOutput(f)
4147
}
4248
if action != "get" {
43-
return
49+
return nil
4450
}
4551

4652
result, err := parseFromStdin()
4753
host := result["host"]
4854
if err != nil || host == "" {
4955
log.WithError(err).Print("error parsing 'host' from stdin")
50-
return
56+
return GpError{OutCome: utils.Outcome_UserErr, Silence: true, ExitCode: &exitCode}
5157
}
5258

5359
var user, token string
@@ -66,13 +72,13 @@ var credentialHelper = &cobra.Command{
6672
}
6773
}()
6874

69-
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
75+
ctx, cancel := context.WithTimeout(cmd.Context(), 1*time.Minute)
7076
defer cancel()
7177

7278
supervisorConn, err := grpc.Dial(util.GetSupervisorAddress(), grpc.WithTransportCredentials(insecure.NewCredentials()))
7379
if err != nil {
7480
log.WithError(err).Print("error connecting to supervisor")
75-
return
81+
return GpError{Err: xerrors.Errorf("error connecting to supervisor: %w", err), Silence: true, ExitCode: &exitCode}
7682
}
7783

7884
resp, err := supervisor.NewTokenServiceClient(supervisorConn).GetToken(ctx, &supervisor.GetTokenRequest{
@@ -81,7 +87,7 @@ var credentialHelper = &cobra.Command{
8187
})
8288
if err != nil {
8389
log.WithError(err).Print("error getting token from supervisior")
84-
return
90+
return GpError{Err: xerrors.Errorf("error getting token from supervisior: %w", err), Silence: true, ExitCode: &exitCode}
8591
}
8692

8793
user = resp.User
@@ -102,12 +108,11 @@ var credentialHelper = &cobra.Command{
102108
return gitCmdInfo.Ok()
103109
})
104110
if err != nil {
105-
log.WithError(err).Print("error walking process tree")
106-
return
111+
return GpError{Err: xerrors.Errorf("error walking process tree: %w", err), Silence: true, ExitCode: &exitCode}
107112
}
108113
if !gitCmdInfo.Ok() {
109114
log.Warn(`Could not detect "RepoUrl" and or "GitCommand", token validation will not be performed`)
110-
return
115+
return nil
111116
}
112117

113118
// Starts another process which tracks the executed git event
@@ -134,14 +139,14 @@ var credentialHelper = &cobra.Command{
134139
)
135140
err = validator.Start()
136141
if err != nil {
137-
log.WithError(err).Print("error spawning validator")
138-
return
142+
return GpError{Err: xerrors.Errorf("error spawning validator: %w", err), Silence: true, ExitCode: &exitCode}
139143
}
140144
err = validator.Process.Release()
141145
if err != nil {
142146
log.WithError(err).Print("error releasing validator")
143-
return
147+
return GpError{Err: xerrors.Errorf("error releasing validator: %w", err), Silence: true, ExitCode: &exitCode}
144148
}
149+
return nil
145150
},
146151
}
147152

components/gitpod-cli/cmd/docs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ const DocsUrl = "https://www.gitpod.io/docs"
1414
var docsCmd = &cobra.Command{
1515
Use: "docs",
1616
Short: "Open Gitpod Documentation in default browser",
17-
Run: func(cmd *cobra.Command, args []string) {
18-
openPreview("GP_EXTERNAL_BROWSER", DocsUrl)
17+
RunE: func(cmd *cobra.Command, args []string) error {
18+
return openPreview("GP_EXTERNAL_BROWSER", DocsUrl)
1919
},
2020
}
2121

components/gitpod-cli/cmd/env.go

Lines changed: 37 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
log "github.com/sirupsen/logrus"
1717
"github.com/spf13/cobra"
18+
"golang.org/x/sync/errgroup"
1819
"golang.org/x/xerrors"
1920
"google.golang.org/grpc"
2021
"google.golang.org/grpc/credentials/insecure"
@@ -51,24 +52,27 @@ Note that you can delete/unset variables if their repository pattern matches the
5152
delete environment variables with a repository pattern of */foo, foo/* or */*.
5253
`,
5354
Args: cobra.ArbitraryArgs,
54-
Run: func(cmd *cobra.Command, args []string) {
55+
RunE: func(cmd *cobra.Command, args []string) error {
5556
log.SetOutput(io.Discard)
5657
f, err := os.OpenFile(os.TempDir()+"/gp-env.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
5758
if err == nil {
5859
defer f.Close()
5960
log.SetOutput(f)
6061
}
6162

63+
ctx, cancel := context.WithTimeout(cmd.Context(), 1*time.Minute)
64+
defer cancel()
65+
6266
if len(args) > 0 {
6367
if unsetEnvs {
64-
deleteEnvs(args)
65-
return
68+
err = deleteEnvs(ctx, args)
69+
} else {
70+
err = setEnvs(ctx, args)
6671
}
67-
68-
setEnvs(args)
6972
} else {
70-
getEnvs()
73+
err = getEnvs(ctx)
7174
}
75+
return err
7276
},
7377
}
7478

@@ -120,84 +124,69 @@ func connectToServer(ctx context.Context) (*connectToServerResult, error) {
120124
return &connectToServerResult{repositoryPattern, client}, nil
121125
}
122126

123-
func getEnvs() {
124-
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
125-
defer cancel()
127+
func getEnvs(ctx context.Context) error {
126128
result, err := connectToServer(ctx)
127129
if err != nil {
128-
fail(err.Error())
130+
return err
129131
}
132+
defer result.client.Close()
130133

131134
vars, err := result.client.GetEnvVars(ctx)
132135
if err != nil {
133-
fail("failed to fetch env vars from server: " + err.Error())
136+
return xerrors.Errorf("failed to fetch env vars from server: %w", err)
134137
}
135138

136139
for _, v := range vars {
137140
printVar(v, exportEnvs)
138141
}
142+
143+
return nil
139144
}
140145

141-
func setEnvs(args []string) {
142-
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
143-
defer cancel()
146+
func setEnvs(ctx context.Context, args []string) error {
144147
result, err := connectToServer(ctx)
145148
if err != nil {
146-
fail(err.Error())
149+
return err
147150
}
151+
defer result.client.Close()
148152

149153
vars, err := parseArgs(args, result.repositoryPattern)
150154
if err != nil {
151-
fail(err.Error())
155+
return err
152156
}
153157

154-
var exitCode int
155-
var wg sync.WaitGroup
156-
wg.Add(len(vars))
158+
g, ctx := errgroup.WithContext(ctx)
157159
for _, v := range vars {
158-
go func(v *serverapi.UserEnvVarValue) {
160+
v := v
161+
g.Go(func() error {
159162
err = result.client.SetEnvVar(ctx, v)
160163
if err != nil {
161-
fmt.Fprintf(os.Stderr, "cannot set %s: %v\n", v.Name, err)
162-
exitCode = -1
163-
} else {
164-
printVar(v, exportEnvs)
164+
return err
165165
}
166-
wg.Done()
167-
}(v)
166+
printVar(v, exportEnvs)
167+
return nil
168+
})
168169
}
169-
wg.Wait()
170-
os.Exit(exitCode)
170+
return g.Wait()
171171
}
172172

173-
func deleteEnvs(args []string) {
174-
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
175-
defer cancel()
173+
func deleteEnvs(ctx context.Context, args []string) error {
176174
result, err := connectToServer(ctx)
177175
if err != nil {
178-
fail(err.Error())
176+
return err
179177
}
178+
defer result.client.Close()
180179

181-
var exitCode int
180+
g, ctx := errgroup.WithContext(ctx)
182181
var wg sync.WaitGroup
183182
wg.Add(len(args))
184183
for _, name := range args {
185-
go func(name string) {
186-
err = result.client.DeleteEnvVar(ctx, &serverapi.UserEnvVarValue{Name: name, RepositoryPattern: result.repositoryPattern})
187-
if err != nil {
188-
fmt.Fprintf(os.Stderr, "cannot unset %s: %v\n", name, err)
189-
exitCode = -1
190-
}
191-
wg.Done()
192-
}(name)
184+
name := name
185+
g.Go(func() error {
186+
return result.client.DeleteEnvVar(ctx, &serverapi.UserEnvVarValue{Name: name, RepositoryPattern: result.repositoryPattern})
187+
})
193188
}
194-
wg.Wait()
195-
os.Exit(exitCode)
196-
}
197-
198-
func fail(msg string) {
199-
fmt.Fprintln(os.Stderr, msg)
200-
os.Exit(-1)
189+
return g.Wait()
201190
}
202191

203192
func printVar(v *serverapi.UserEnvVarValue, export bool) {

components/gitpod-cli/cmd/git-token-validator.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"google.golang.org/grpc/credentials/insecure"
2020

2121
"github.com/gitpod-io/gitpod/common-go/util"
22+
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/utils"
2223
serverapi "github.com/gitpod-io/gitpod/gitpod-protocol"
2324
supervisor "github.com/gitpod-io/gitpod/supervisor/api"
2425
)
@@ -38,7 +39,10 @@ var gitTokenValidator = &cobra.Command{
3839
Long: "Tries to guess the scopes needed for a git operation and requests an appropriate token.",
3940
Args: cobra.ExactArgs(0),
4041
Hidden: true,
41-
Run: func(cmd *cobra.Command, args []string) {
42+
RunE: func(cmd *cobra.Command, args []string) error {
43+
// ignore trace
44+
utils.TrackCommandUsageEvent.Command = nil
45+
4246
log.SetOutput(io.Discard)
4347
f, err := os.OpenFile(os.TempDir()+"/gitpod-git-credential-helper.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
4448
if err == nil {
@@ -48,7 +52,7 @@ var gitTokenValidator = &cobra.Command{
4852

4953
log.Infof("gp git-token-validator")
5054

51-
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
55+
ctx, cancel := context.WithTimeout(cmd.Context(), 1*time.Minute)
5256
defer cancel()
5357

5458
supervisorConn, err := grpc.Dial(util.GetSupervisorAddress(), grpc.WithTransportCredentials(insecure.NewCredentials()))
@@ -77,6 +81,8 @@ var gitTokenValidator = &cobra.Command{
7781
if err != nil {
7882
log.WithError(err).Fatal("error connecting to server")
7983
}
84+
defer client.Close()
85+
8086
params := &serverapi.GuessGitTokenScopesParams{
8187
Host: gitTokenValidatorOpts.Host,
8288
RepoURL: gitTokenValidatorOpts.RepoURL,
@@ -116,7 +122,7 @@ var gitTokenValidator = &cobra.Command{
116122
log.WithError(err).Fatalf("error opening access-control: '%s'", message)
117123
}
118124
}
119-
return
125+
return nil
120126
}
121127
if len(guessedTokenScopes.Scopes) > 0 {
122128
_, err = supervisor.NewTokenServiceClient(supervisorConn).GetToken(ctx,
@@ -128,9 +134,10 @@ var gitTokenValidator = &cobra.Command{
128134
})
129135
if err != nil {
130136
log.WithError(err).Fatal("error getting new token from token service")
131-
return
137+
return nil
132138
}
133139
}
140+
return nil
134141
},
135142
}
136143

components/gitpod-cli/cmd/git-track-command.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import (
1313
log "github.com/sirupsen/logrus"
1414
"github.com/spf13/cobra"
1515

16-
gitpod "github.com/gitpod-io/gitpod/gitpod-cli/pkg/gitpod"
16+
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/gitpod"
17+
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/utils"
1718
serverapi "github.com/gitpod-io/gitpod/gitpod-protocol"
1819
)
1920

@@ -27,7 +28,10 @@ var gitTrackCommand = &cobra.Command{
2728
Long: "Sending anonymous statistics about the executed git commands inside a workspace",
2829
Args: cobra.ExactArgs(0),
2930
Hidden: true,
30-
Run: func(cmd *cobra.Command, args []string) {
31+
RunE: func(cmd *cobra.Command, args []string) error {
32+
// ignore trace
33+
utils.TrackCommandUsageEvent.Command = nil
34+
3135
log.SetOutput(io.Discard)
3236
f, err := os.OpenFile(os.TempDir()+"/gitpod-git-credential-helper.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
3337
if err == nil {
@@ -37,11 +41,11 @@ var gitTrackCommand = &cobra.Command{
3741

3842
log.Infof("gp git-track-command")
3943

40-
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
44+
ctx, cancel := context.WithTimeout(cmd.Context(), 1*time.Minute)
4145
defer cancel()
4246
wsInfo, err := gitpod.GetWSInfo(ctx)
4347
if err != nil {
44-
fail(err.Error())
48+
return err
4549
}
4650

4751
client, err := gitpod.ConnectToServer(ctx, wsInfo, []string{"function:trackEvent"})
@@ -76,6 +80,7 @@ var gitTrackCommand = &cobra.Command{
7680
if err != nil {
7781
log.WithError(err).Fatal("error tracking git event")
7882
}
83+
return nil
7984
},
8085
}
8186

0 commit comments

Comments
 (0)