-
Notifications
You must be signed in to change notification settings - Fork 288
/
analytics.go
106 lines (82 loc) · 2.87 KB
/
analytics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package cli
import (
"os"
"os/exec"
"runtime"
"strings"
tiltanalytics "github.com/windmilleng/tilt/internal/analytics"
"github.com/spf13/cobra"
giturls "github.com/whilp/git-urls"
"github.com/windmilleng/wmclient/pkg/analytics"
)
const tiltAppName = "tilt"
const analyticsURLEnvVar = "TILT_ANALYTICS_URL"
// Testing analytics locally:
// (after `npm install http-echo-server -g`)
// In one window: `PORT=9988 http-echo-server`
// In another: `TILT_ANALYTICS_URL=http://localhost:9988 tilt up`
// Analytics requests will show up in the http-echo-server window.
type analyticsOpter struct{}
var _ tiltanalytics.AnalyticsOpter = analyticsOpter{}
func (ao analyticsOpter) ReadUserOpt() (analytics.Opt, error) {
return analytics.OptStatus()
}
func (ao analyticsOpter) SetUserOpt(opt analytics.Opt) error {
return analytics.SetOpt(opt)
}
func initAnalytics(rootCmd *cobra.Command) (*tiltanalytics.TiltAnalytics, error) {
var analyticsCmd *cobra.Command
var err error
options := []analytics.Option{}
// enabled: true because TiltAnalytics wraps the RemoteAnalytics and has its own guards for whether analytics
// is enabled. When TiltAnalytics decides to pass a call through to RemoteAnalytics, it should always work.
options = append(options, analytics.WithGlobalTags(globalTags()), analytics.WithEnabled(true))
analyticsURL := os.Getenv(analyticsURLEnvVar)
if analyticsURL != "" {
options = append(options, analytics.WithReportURL(analyticsURL))
}
backingAnalytics, analyticsCmd, err := analytics.Init(tiltAppName, options...)
if err != nil {
return nil, err
}
rootCmd.AddCommand(analyticsCmd)
return tiltanalytics.NewTiltAnalytics(analyticsOpter{}, backingAnalytics, provideTiltInfo().AnalyticsVersion())
}
func globalTags() map[string]string {
ret := map[string]string{
tiltanalytics.TagVersion: provideTiltInfo().AnalyticsVersion(),
tiltanalytics.TagOS: runtime.GOOS,
}
// store a hash of the git remote to help us guess how many users are running it on the same repository
origin := normalizeGitRemote(gitOrigin("."))
if origin != "" {
ret[tiltanalytics.TagGitRepoHash] = tiltanalytics.HashMD5(origin)
}
return ret
}
func gitOrigin(fromDir string) string {
cmd := exec.Command("git", "-C", fromDir, "remote", "get-url", "origin")
b, err := cmd.Output()
if err != nil {
return ""
}
return strings.TrimRight(string(b), "\n")
}
func normalizeGitRemote(s string) string {
u, err := giturls.Parse(string(s))
if err != nil {
return s
}
// treat "http://", "https://", "git://", "ssh://", etc as equiv
u.Scheme = ""
u.User = nil
// github.com/windmilleng/tilt is the same as github.com/windmilleng/tilt/
if strings.HasSuffix(u.Path, "/") {
u.Path = u.Path[:len(u.Path)-1]
}
// github.com/windmilleng/tilt is the same as github.com/windmilleng/tilt.git
if strings.HasSuffix(u.Path, ".git") {
u.Path = u.Path[:len(u.Path)-4]
}
return u.String()
}