This repository has been archived by the owner on Feb 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
57 lines (51 loc) · 1.72 KB
/
root.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
package cmds
import (
"flag"
"log"
"os"
"strings"
v "github.com/appscode/go/version"
"github.com/appscode/kutil/tools/analytics"
"github.com/jpillora/go-ogle-analytics"
"github.com/kubepack/packserver/client/clientset/versioned/scheme"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
genericapiserver "k8s.io/apiserver/pkg/server"
clientsetscheme "k8s.io/client-go/kubernetes/scheme"
)
const (
gaTrackingCode = "UA-62096468-20"
)
func NewRootCmd() *cobra.Command {
var (
AnalyticsClientID string
EnableAnalytics = true
)
var rootCmd = &cobra.Command{
Use: "packserver",
Short: `Packserver by AppsCode - Kubepack api server`,
DisableAutoGenTag: true,
PersistentPreRun: func(c *cobra.Command, args []string) {
c.Flags().VisitAll(func(flag *pflag.Flag) {
log.Printf("FLAG: --%s=%q", flag.Name, flag.Value)
})
if EnableAnalytics && gaTrackingCode != "" {
if client, err := ga.NewClient(gaTrackingCode); err == nil {
AnalyticsClientID = analytics.ClientID()
client.ClientID(AnalyticsClientID)
parts := strings.Split(c.CommandPath(), " ")
client.Send(ga.NewEvent(parts[0], strings.Join(parts[1:], "/")).Label(v.Version.Version))
}
}
scheme.AddToScheme(clientsetscheme.Scheme)
},
}
rootCmd.PersistentFlags().AddGoFlagSet(flag.CommandLine)
// ref: https://github.com/kubernetes/kubernetes/issues/17162#issuecomment-225596212
flag.CommandLine.Parse([]string{})
rootCmd.PersistentFlags().BoolVar(&EnableAnalytics, "analytics", EnableAnalytics, "Send analytical events to Google Analytics")
rootCmd.AddCommand(v.NewCmdVersion())
stopCh := genericapiserver.SetupSignalHandler()
rootCmd.AddCommand(NewCmdRun(os.Stdout, os.Stderr, stopCh))
return rootCmd
}