Skip to content

Commit 35d8dc6

Browse files
authored
Move analytics collector to root command (#164)
1 parent 6a55694 commit 35d8dc6

File tree

3 files changed

+34
-38
lines changed

3 files changed

+34
-38
lines changed

root.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,46 @@ package cmds
33
import (
44
"flag"
55
"log"
6+
"strings"
67

78
v "github.com/appscode/go/version"
9+
"github.com/jpillora/go-ogle-analytics"
810
"github.com/spf13/cobra"
911
"github.com/spf13/pflag"
1012
)
1113

14+
const (
15+
gaTrackingCode = "UA-62096468-20"
16+
)
17+
1218
func NewCmdStash(version string) *cobra.Command {
19+
var (
20+
enableAnalytics = true
21+
)
1322
var rootCmd = &cobra.Command{
14-
Use: "stash",
15-
Short: `Stash by AppsCode - Backup your Kubernetes Volumes`,
16-
Long: `Stash is a Kubernetes operator for restic. For more information, visit here: https://github.com/appscode/stash/tree/master/docs`,
23+
Use: "stash",
24+
Short: `Stash by AppsCode - Backup your Kubernetes Volumes`,
25+
Long: `Stash is a Kubernetes operator for restic. For more information, visit here: https://github.com/appscode/stash/tree/master/docs`,
26+
DisableAutoGenTag: true,
1727
PersistentPreRun: func(c *cobra.Command, args []string) {
1828
c.Flags().VisitAll(func(flag *pflag.Flag) {
1929
log.Printf("FLAG: --%s=%q", flag.Name, flag.Value)
2030
})
31+
if enableAnalytics && gaTrackingCode != "" {
32+
if client, err := ga.NewClient(gaTrackingCode); err == nil {
33+
parts := strings.Split(c.CommandPath(), " ")
34+
client.Send(ga.NewEvent(parts[0], strings.Join(parts[1:], "/")).Label(version))
35+
}
36+
}
2137
},
2238
}
2339
rootCmd.PersistentFlags().AddGoFlagSet(flag.CommandLine)
40+
// ref: https://github.com/kubernetes/kubernetes/issues/17162#issuecomment-225596212
41+
flag.CommandLine.Parse([]string{})
42+
rootCmd.PersistentFlags().BoolVar(&enableAnalytics, "analytics", enableAnalytics, "Send analytical events to Google Analytics")
2443

2544
rootCmd.AddCommand(v.NewCmdVersion())
2645
rootCmd.AddCommand(NewCmdRun(version))
27-
rootCmd.AddCommand(NewCmdSchedule(version))
46+
rootCmd.AddCommand(NewCmdSchedule())
2847
return rootCmd
2948
}

run.go

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/appscode/pat"
1010
sapi "github.com/appscode/stash/api"
1111
scs "github.com/appscode/stash/client/clientset"
12-
"github.com/appscode/stash/pkg/analytics"
1312
"github.com/appscode/stash/pkg/controller"
1413
"github.com/appscode/stash/pkg/docker"
1514
"github.com/appscode/stash/pkg/migrator"
@@ -29,25 +28,16 @@ var (
2928

3029
func NewCmdRun(version string) *cobra.Command {
3130
var (
32-
masterURL string
33-
kubeconfigPath string
34-
tag string = stringz.Val(version, "canary")
35-
address string = ":56790"
36-
enableAnalytics bool = true
31+
masterURL string
32+
kubeconfigPath string
33+
tag string = stringz.Val(version, "canary")
34+
address string = ":56790"
3735
)
3836

3937
cmd := &cobra.Command{
40-
Use: "run",
41-
Short: "Run Stash operator",
42-
PreRun: func(cmd *cobra.Command, args []string) {
43-
if enableAnalytics {
44-
analytics.Enable()
45-
}
46-
analytics.SendEvent("operator", "started", version)
47-
},
48-
PostRun: func(cmd *cobra.Command, args []string) {
49-
analytics.SendEvent("operator", "stopped", version)
50-
},
38+
Use: "run",
39+
Short: "Run Stash operator",
40+
DisableAutoGenTag: true,
5141
Run: func(cmd *cobra.Command, args []string) {
5242
if err := docker.CheckDockerImageVersion(docker.ImageOperator, tag); err != nil {
5343
log.Fatalf(`Image %v:%v not found.`, docker.ImageOperator, tag)
@@ -90,7 +80,6 @@ func NewCmdRun(version string) *cobra.Command {
9080
cmd.Flags().StringVar(&kubeconfigPath, "kubeconfig", kubeconfigPath, "Path to kubeconfig file with authorization information (the master location is set by the master flag).")
9181
cmd.Flags().StringVar(&address, "address", address, "Address to listen on for web interface and telemetry.")
9282
cmd.Flags().StringVar(&scratchDir, "scratch-dir", scratchDir, "Directory used to store temporary files. Use an `emptyDir` in Kubernetes.")
93-
cmd.Flags().BoolVar(&enableAnalytics, "analytics", enableAnalytics, "Send analytical events to Google Analytics")
9483

9584
return cmd
9685
}

schedule.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
"github.com/appscode/log"
99
rcs "github.com/appscode/stash/client/clientset"
10-
"github.com/appscode/stash/pkg/analytics"
1110
"github.com/appscode/stash/pkg/scheduler"
1211
"github.com/appscode/stash/pkg/util"
1312
"github.com/spf13/cobra"
@@ -17,7 +16,7 @@ import (
1716
"k8s.io/client-go/tools/clientcmd"
1817
)
1918

20-
func NewCmdSchedule(version string) *cobra.Command {
19+
func NewCmdSchedule() *cobra.Command {
2120
var (
2221
masterURL string
2322
kubeconfigPath string
@@ -29,21 +28,12 @@ func NewCmdSchedule(version string) *cobra.Command {
2928
PushgatewayURL: "http://stash-operator.kube-system.svc:56789",
3029
PodLabelsPath: "/etc/stash/labels",
3130
}
32-
enableAnalytics bool = true
3331
)
3432

3533
cmd := &cobra.Command{
36-
Use: "schedule",
37-
Short: "Run Stash cron daemon",
38-
PreRun: func(cmd *cobra.Command, args []string) {
39-
if enableAnalytics {
40-
analytics.Enable()
41-
}
42-
analytics.SendEvent("scheduler", "started", version)
43-
},
44-
PostRun: func(cmd *cobra.Command, args []string) {
45-
analytics.SendEvent("scheduler", "stopped", version)
46-
},
34+
Use: "schedule",
35+
Short: "Run Stash cron daemon",
36+
DisableAutoGenTag: true,
4737
Run: func(cmd *cobra.Command, args []string) {
4838
config, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfigPath)
4939
if err != nil {
@@ -141,8 +131,6 @@ func NewCmdSchedule(version string) *cobra.Command {
141131
cmd.Flags().StringVar(&opt.ScratchDir, "scratch-dir", opt.ScratchDir, "Directory used to store temporary files. Use an `emptyDir` in Kubernetes.")
142132
cmd.Flags().StringVar(&opt.PushgatewayURL, "pushgateway-url", opt.PushgatewayURL, "URL of Prometheus pushgateway used to cache backup metrics")
143133

144-
// Analytics flags
145-
cmd.Flags().BoolVar(&enableAnalytics, "analytics", enableAnalytics, "Send analytical events to Google Analytics")
146134
return cmd
147135
}
148136

0 commit comments

Comments
 (0)