diff --git a/cmd/world/main.go b/cmd/world/main.go index adb6e26..809ac28 100644 --- a/cmd/world/main.go +++ b/cmd/world/main.go @@ -1,19 +1,44 @@ package main import ( - "github.com/getsentry/sentry-go" + "github.com/denisbrodbeck/machineid" + ph "github.com/posthog/posthog-go" "log" + "os" + "pkg.world.dev/world-cli/common/logger" + "pkg.world.dev/world-cli/posthog" "time" + "github.com/getsentry/sentry-go" "pkg.world.dev/world-cli/cmd/world/root" ) +// This variable will be overridden by ldflags during build +// Example : go build -ldflags "-X main.AppVersion=1.0.0 -X main.PosthogApiKey= -X main.SentryDsn=" +var ( + AppVersion string + PosthogApiKey string + SentryDsn string +) + +const ( + postInstallationEvent = "World CLI Installation" + runningEvent = "World CLI Running" +) + +func init() { + // Set default app version in case not provided by ldflags + if AppVersion == "" { + AppVersion = "dev" + } + root.AppVersion = AppVersion +} + func main() { // Sentry initialization - DSN := "" // Input DSN here, you can get it from https://argus-labs.sentry.io/settings/projects/world-cli/keys/ - if DSN != "" { + if SentryDsn != "" { err := sentry.Init(sentry.ClientOptions{ - Dsn: DSN, + Dsn: SentryDsn, EnableTracing: true, TracesSampleRate: 1.0, ProfilesSampleRate: 1.0, @@ -35,5 +60,36 @@ func main() { }() } + // Posthog Initialization + posthog.Init(PosthogApiKey) + defer posthog.Close() + + // Obtain the machine ID + machineID, err := machineid.ProtectedID("world-cli") + if err != nil { + logger.Error(err) + } + + // Create capture event for posthog + event := ph.Capture{ + DistinctId: machineID, + Timestamp: time.Now(), + Properties: map[string]interface{}{ + "version": AppVersion, + "command": os.Args, + }, + } + + // Capture event post installation + if len(os.Args) > 1 && os.Args[1] == "post-installation" { + event.Event = postInstallationEvent + posthog.CaptureEvent(event) + return + } + + // Capture event running + event.Event = runningEvent + posthog.CaptureEvent(event) + root.Execute() } diff --git a/cmd/world/root/version.go b/cmd/world/root/version.go index 169528a..394c563 100644 --- a/cmd/world/root/version.go +++ b/cmd/world/root/version.go @@ -2,11 +2,11 @@ package root import ( "fmt" - "runtime/debug" - "github.com/spf13/cobra" ) +var AppVersion string + // versionCmd print the version number of World CLI // Usage: `world version` var versionCmd = &cobra.Command{ @@ -14,11 +14,6 @@ var versionCmd = &cobra.Command{ Short: "Print the version number of World CLI", Long: `Print the version number of World CLI`, Run: func(cmd *cobra.Command, args []string) { - bi, ok := debug.ReadBuildInfo() - if ok { - fmt.Printf("World CLI %s\n", bi.Main.Version) - } else { - fmt.Printf("World CLI \n") - } + fmt.Printf("World CLI %s\n", AppVersion) }, } diff --git a/go.mod b/go.mod index 03a2a72..1b57ce9 100644 --- a/go.mod +++ b/go.mod @@ -5,10 +5,12 @@ go 1.21.1 require ( github.com/charmbracelet/bubbles v0.16.1 github.com/charmbracelet/bubbletea v0.24.2 + github.com/denisbrodbeck/machineid v1.0.1 github.com/getsentry/sentry-go v0.26.0 github.com/guumaster/logsymbols v0.3.1 github.com/magefile/mage v1.15.0 github.com/pelletier/go-toml v1.9.5 + github.com/posthog/posthog-go v0.0.0-20240202122501-d793288ce2c9 github.com/rs/zerolog v1.31.0 github.com/spf13/cobra v1.7.0 github.com/zulkhair/fresh v0.0.0-20240206120148-20e4bcfbc141 @@ -18,6 +20,7 @@ require ( require ( github.com/atotto/clipboard v0.1.4 // indirect github.com/google/go-cmp v0.5.9 // indirect + github.com/google/uuid v1.3.0 // indirect github.com/howeyc/fsnotify v0.9.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/pilu/config v0.0.0-20131214182432-3eb99e6c0b9a // indirect diff --git a/go.sum b/go.sum index 7883ac5..4f8f3e3 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,4 @@ +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4= github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI= github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= @@ -11,10 +12,13 @@ github.com/charmbracelet/lipgloss v0.7.1/go.mod h1:yG0k3giv8Qj8edTCbbg6AlQ5e8KNW github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY= github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= +github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/denisbrodbeck/machineid v1.0.1 h1:geKr9qtkB876mXguW2X6TU4ZynleN6ezuMSRhl4D7AQ= +github.com/denisbrodbeck/machineid v1.0.1/go.mod h1:dJUwb7PTidGDeYyUBmXZ2GphQBbjJCrnectwCyxcUSI= github.com/getsentry/sentry-go v0.26.0 h1:IX3++sF6/4B5JcevhdZfdKIHfyvMmAq/UnqcyT2H6mA= github.com/getsentry/sentry-go v0.26.0/go.mod h1:lc76E2QywIyW8WuBnwl8Lc4bkmQH4+w1gwTf25trprY= github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA= @@ -22,6 +26,8 @@ github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3Bop github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/guumaster/logsymbols v0.3.1 h1:bnCE484dAQFvMWt2EfZAzF1oCgu8yo/Vp1QGQ0EmaAA= github.com/guumaster/logsymbols v0.3.1/go.mod h1:1M5/1js2Z7Yo8DRB3QrPURwqsXeOfgsJv1Utjookknw= github.com/howeyc/fsnotify v0.9.0 h1:0gtV5JmOKH4A8SsFxG2BczSeXWWPvcMT0euZt5gDAxY= @@ -63,13 +69,17 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/posthog/posthog-go v0.0.0-20240202122501-d793288ce2c9 h1:13EBt0uuhoWDuJ+jz58vTIRjUPd5BdSgRqN2RpEddvc= +github.com/posthog/posthog-go v0.0.0-20240202122501-d793288ce2c9/go.mod h1:migYMxlAqcnQy+3eN8mcL0b2tpKy6R+8Zc0lxwk4dKM= github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A= github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= +github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I= github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= @@ -78,6 +88,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/urfave/cli v1.22.5/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= github.com/zulkhair/fresh v0.0.0-20240206120148-20e4bcfbc141 h1:XymlZUaPL97jSQRZ9dzbgAWIJJ/1Tjneoz/LlFo8Q9Q= github.com/zulkhair/fresh v0.0.0-20240206120148-20e4bcfbc141/go.mod h1:21tH5jBa0HwMBoVsf4qBfImYjF2Qtfddsa2KY4y9zr4= golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= diff --git a/posthog/posthog.go b/posthog/posthog.go new file mode 100644 index 0000000..c12ff6b --- /dev/null +++ b/posthog/posthog.go @@ -0,0 +1,38 @@ +package posthog + +import ( + ph "github.com/posthog/posthog-go" + + "pkg.world.dev/world-cli/common/logger" +) + +var ( + client ph.Client + initialized bool +) + +// Init Posthog initialization +func Init(posthogApiKey string) { + if posthogApiKey != "" { + client = ph.New(posthogApiKey) + initialized = true + } +} + +func CaptureEvent(capture ph.Capture) { + if initialized { + err := client.Enqueue(capture) + if err != nil { + logger.Error(err) + } + } +} + +func Close() { + if initialized { + err := client.Close() + if err != nil { + logger.Error(err) + } + } +}