diff --git a/.github/workflows/release_build.yml b/.github/workflows/release_build.yml index f728d52bd9..9840e2d3e4 100644 --- a/.github/workflows/release_build.yml +++ b/.github/workflows/release_build.yml @@ -46,6 +46,7 @@ jobs: args: release --clean env: GITHUB_TOKEN: ${{ secrets.GO_RELEASER_GITHUB_TOKEN }} + POSTHOG_API_KEY_FOR_CLI: ${{ secrets.POSTHOG_API_KEY_FOR_CLI }} FURY_TOKEN: ${{ secrets.FURYPUSHTOKEN }} AUR_KEY: ${{ secrets.AUR_KEY }} GORELEASER_KEY: ${{ secrets.GORELEASER_KEY }} diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 99291bde7e..12fb34dbab 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -15,6 +15,9 @@ monorepo: tag_prefix: infisical-cli/ dir: cli +env: + - POSTHOG_API_KEY_FOR_CLI={{ .Env.POSTHOG_API_KEY_FOR_CLI }} + builds: - id: darwin-build binary: infisical diff --git a/cli/go.mod b/cli/go.mod index 3e9d2aac64..640e6dc0ca 100644 --- a/cli/go.mod +++ b/cli/go.mod @@ -28,6 +28,7 @@ require ( github.com/chzyer/readline v1.5.1 // indirect github.com/danieljoos/wincred v1.1.2 // indirect github.com/davecgh/go-spew v1.1.1 // indirect + github.com/denisbrodbeck/machineid v1.0.1 // indirect github.com/dvsekhvalnov/jose2go v1.5.0 // indirect github.com/fsnotify/fsnotify v1.4.9 // indirect github.com/go-openapi/errors v0.20.2 // indirect @@ -47,11 +48,13 @@ require ( github.com/oklog/ulid v1.3.1 // indirect github.com/pelletier/go-toml v1.9.3 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/posthog/posthog-go v0.0.0-20221221115252-24dfed35d71a // indirect github.com/rivo/uniseg v0.2.0 // indirect github.com/spf13/afero v1.6.0 // indirect github.com/spf13/cast v1.3.1 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/subosito/gotenv v1.2.0 // indirect + github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c // indirect go.mongodb.org/mongo-driver v1.10.0 // indirect golang.org/x/net v0.7.0 // indirect golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect diff --git a/cli/go.sum b/cli/go.sum index 24b6aecd36..574452d459 100644 --- a/cli/go.sum +++ b/cli/go.sum @@ -69,6 +69,7 @@ github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnht github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= github.com/coreos/go-systemd/v22 v22.3.2/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/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/danieljoos/wincred v1.1.2 h1:QLdCxFs1/Yl4zduvBdcHB8goaYk9RARS2SgLLRuAyr0= @@ -76,6 +77,8 @@ github.com/danieljoos/wincred v1.1.2/go.mod h1:GijpziifJoIBfYh+S7BbkdUTU4LfM+QnG 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/dvsekhvalnov/jose2go v1.5.0 h1:3j8ya4Z4kMCwT5nXIKFSV84YS+HdqSSO0VsTQxaLAeM= github.com/dvsekhvalnov/jose2go v1.5.0/go.mod h1:QsHjhyTlD/lAVqn/NSbVZmSCGeDehTB/mPZadG+mhXU= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= @@ -286,6 +289,8 @@ github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZ 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/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= +github.com/posthog/posthog-go v0.0.0-20221221115252-24dfed35d71a h1:Ey0XWvrg6u6hyIn1Kd/jCCmL+bMv9El81tvuGBbxZGg= +github.com/posthog/posthog-go v0.0.0-20221221115252-24dfed35d71a/go.mod h1:oa2sAs9tGai3VldabTV0eWejt/O4/OOD7azP8GaikqU= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= @@ -295,9 +300,11 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/zerolog v1.26.1 h1:/ihwxqH+4z8UxyI70wM1z9yCvkWcfz/a3mj48k/Zngc= github.com/rs/zerolog v1.26.1/go.mod h1:/wSSJWX7lVrsOwlbyTRSOJvqRlc+WjWlfes+CiJ+tmc= +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/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= +github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= @@ -330,9 +337,12 @@ github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4= github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= +github.com/urfave/cli v1.22.5/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= github.com/xdg-go/scram v1.1.1/go.mod h1:RaEWvsqvNKKvBPvcKeFjrG2cJqOkHTiyTpzz23ni57g= github.com/xdg-go/stringprep v1.0.3/go.mod h1:W3f5j4i+9rC0kuIEJL0ky1VpHXQU3ocBgklLGvcBnW8= +github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c h1:3lbZUMbMiGUW/LMkfsEABsc5zNT9+b1CvsJx47JzJ8g= +github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c/go.mod h1:UrdRz5enIKZ63MEE3IF9l2/ebyx59GyGgPi+tICQdmM= github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= diff --git a/cli/packages/cmd/export.go b/cli/packages/cmd/export.go index 230e728375..453d1b75c7 100644 --- a/cli/packages/cmd/export.go +++ b/cli/packages/cmd/export.go @@ -11,6 +11,7 @@ import ( "github.com/Infisical/infisical-merge/packages/models" "github.com/Infisical/infisical-merge/packages/util" + "github.com/posthog/posthog-go" "github.com/rs/zerolog/log" "github.com/spf13/cobra" ) @@ -95,6 +96,8 @@ var exportCmd = &cobra.Command{ } fmt.Print(output) + + Telemetry.CaptureEvent("cli-command:export", posthog.NewProperties().Set("secretsCount", len(secrets)).Set("version", util.CLI_VERSION)) }, } diff --git a/cli/packages/cmd/init.go b/cli/packages/cmd/init.go index f730fd7f86..64387a8370 100644 --- a/cli/packages/cmd/init.go +++ b/cli/packages/cmd/init.go @@ -12,6 +12,7 @@ import ( "github.com/Infisical/infisical-merge/packages/util" "github.com/go-resty/resty/v2" "github.com/manifoldco/promptui" + "github.com/posthog/posthog-go" "github.com/rs/zerolog/log" "github.com/spf13/cobra" ) @@ -78,6 +79,9 @@ var initCmd = &cobra.Command{ if err != nil { util.HandleError(err) } + + Telemetry.CaptureEvent("cli-command:init", posthog.NewProperties().Set("version", util.CLI_VERSION)) + }, } diff --git a/cli/packages/cmd/login.go b/cli/packages/cmd/login.go index 55b3fdc4ff..f20a178539 100644 --- a/cli/packages/cmd/login.go +++ b/cli/packages/cmd/login.go @@ -22,6 +22,7 @@ import ( "github.com/fatih/color" "github.com/go-resty/resty/v2" "github.com/manifoldco/promptui" + "github.com/posthog/posthog-go" "github.com/rs/zerolog/log" "github.com/spf13/cobra" "golang.org/x/crypto/argon2" @@ -275,6 +276,7 @@ var loginCmd = &cobra.Command{ plainBold.Println("\nQuick links") fmt.Println("- Learn to inject secrets into your application at https://infisical.com/docs/cli/usage") fmt.Println("- Stuck? Join our slack for quick support https://infisical.com/slack") + Telemetry.CaptureEvent("cli-command:login", posthog.NewProperties().Set("infisical-backend", config.INFISICAL_URL).Set("version", util.CLI_VERSION)) }, } diff --git a/cli/packages/cmd/reset.go b/cli/packages/cmd/reset.go index 9b1eb0f748..0007f2687a 100644 --- a/cli/packages/cmd/reset.go +++ b/cli/packages/cmd/reset.go @@ -7,6 +7,7 @@ import ( "os" "github.com/Infisical/infisical-merge/packages/util" + "github.com/posthog/posthog-go" "github.com/spf13/cobra" ) @@ -37,6 +38,7 @@ var resetCmd = &cobra.Command{ util.DeleteBackupSecrets() util.PrintSuccessMessage("Reset successful") + Telemetry.CaptureEvent("cli-command:reset", posthog.NewProperties().Set("version", util.CLI_VERSION)) }, } diff --git a/cli/packages/cmd/root.go b/cli/packages/cmd/root.go index 871ee62ad6..9c7814eccd 100644 --- a/cli/packages/cmd/root.go +++ b/cli/packages/cmd/root.go @@ -12,9 +12,12 @@ import ( "github.com/spf13/cobra" "github.com/Infisical/infisical-merge/packages/config" + "github.com/Infisical/infisical-merge/packages/telemetry" "github.com/Infisical/infisical-merge/packages/util" ) +var Telemetry *telemetry.Telemetry + var rootCmd = &cobra.Command{ Use: "infisical", Short: "Infisical CLI is used to inject environment variables into any process", @@ -35,6 +38,7 @@ func Execute() { func init() { cobra.OnInitialize(initLog) rootCmd.PersistentFlags().StringP("log-level", "l", "info", "log level (trace, debug, info, warn, error, fatal)") + rootCmd.PersistentFlags().Bool("telemetry", true, "Infisical collects non-sensitive telemetry data to enhance features and improve user experience. Participation is voluntary") rootCmd.PersistentFlags().StringVar(&config.INFISICAL_URL, "domain", util.INFISICAL_DEFAULT_API_URL, "Point the CLI to your own backend [can also set via environment variable name: INFISICAL_API_URL]") rootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) { if !util.IsRunningInDocker() { @@ -49,6 +53,9 @@ func init() { config.INFISICAL_URL = envInfisicalBackendUrl } } + + isTelemetryOn, _ := rootCmd.PersistentFlags().GetBool("telemetry") + Telemetry = telemetry.NewTelemetry(isTelemetryOn) } func initLog() { diff --git a/cli/packages/cmd/run.go b/cli/packages/cmd/run.go index 982c2e3298..e61af67a38 100644 --- a/cli/packages/cmd/run.go +++ b/cli/packages/cmd/run.go @@ -15,6 +15,7 @@ import ( "github.com/Infisical/infisical-merge/packages/models" "github.com/Infisical/infisical-merge/packages/util" "github.com/fatih/color" + "github.com/posthog/posthog-go" "github.com/rs/zerolog/log" "github.com/spf13/cobra" ) @@ -125,6 +126,8 @@ var runCmd = &cobra.Command{ log.Debug().Msgf("injecting the following environment variables into shell: %v", env) + Telemetry.CaptureEvent("cli-command:run", posthog.NewProperties().Set("secretsCount", len(secrets)).Set("environment", environmentName).Set("isUsingServiceToken", infisicalToken != "").Set("single-command", strings.Join(args, " ")).Set("multi-command", cmd.Flag("command").Value.String())) + if cmd.Flags().Changed("command") { command := cmd.Flag("command").Value.String() diff --git a/cli/packages/cmd/scan.go b/cli/packages/cmd/scan.go index fdc99f2535..45796d29d5 100644 --- a/cli/packages/cmd/scan.go +++ b/cli/packages/cmd/scan.go @@ -30,7 +30,9 @@ import ( "github.com/Infisical/infisical-merge/config" "github.com/Infisical/infisical-merge/detect" + "github.com/Infisical/infisical-merge/packages/util" "github.com/Infisical/infisical-merge/report" + "github.com/posthog/posthog-go" "github.com/rs/zerolog/log" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -270,6 +272,8 @@ var scanCmd = &cobra.Command{ } } + Telemetry.CaptureEvent("cli-command:scan", posthog.NewProperties().Set("risks", len(findings)).Set("version", util.CLI_VERSION)) + // write report if desired reportPath, _ := cmd.Flags().GetString("report-path") ext, _ := cmd.Flags().GetString("report-format") @@ -375,6 +379,8 @@ var scanGitChangesCmd = &cobra.Command{ log.Info().Msg("no leaks found") } + Telemetry.CaptureEvent("cli-command:scan git-changes", posthog.NewProperties().Set("risks", len(findings)).Set("version", util.CLI_VERSION)) + reportPath, _ := cmd.Flags().GetString("report-path") ext, _ := cmd.Flags().GetString("report-format") if reportPath != "" { diff --git a/cli/packages/cmd/secrets.go b/cli/packages/cmd/secrets.go index 8256f36270..26835eff1d 100644 --- a/cli/packages/cmd/secrets.go +++ b/cli/packages/cmd/secrets.go @@ -19,6 +19,7 @@ import ( "github.com/Infisical/infisical-merge/packages/util" "github.com/Infisical/infisical-merge/packages/visualize" "github.com/go-resty/resty/v2" + "github.com/posthog/posthog-go" "github.com/rs/zerolog/log" "github.com/spf13/cobra" ) @@ -63,6 +64,7 @@ var secretsCmd = &cobra.Command{ } visualize.PrintAllSecretDetails(secrets) + Telemetry.CaptureEvent("cli-command:secrets", posthog.NewProperties().Set("secretCount", len(secrets)).Set("version", util.CLI_VERSION)) }, } @@ -266,6 +268,8 @@ var secretsSetCmd = &cobra.Command{ } visualize.Table(headers, rows) + + Telemetry.CaptureEvent("cli-command:secrets set", posthog.NewProperties().Set("version", util.CLI_VERSION)) }, } @@ -333,6 +337,7 @@ var secretsDeleteCmd = &cobra.Command{ fmt.Printf("secret name(s) [%v] have been deleted from your project \n", strings.Join(args, ", ")) + Telemetry.CaptureEvent("cli-command:secrets delete", posthog.NewProperties().Set("secretCount", len(secrets)).Set("version", util.CLI_VERSION)) }, } @@ -377,6 +382,7 @@ func getSecretsByNames(cmd *cobra.Command, args []string) { } visualize.PrintAllSecretDetails(requestedSecrets) + Telemetry.CaptureEvent("cli-command:secrets get", posthog.NewProperties().Set("secretCount", len(secrets)).Set("version", util.CLI_VERSION)) } func generateExampleEnv(cmd *cobra.Command, args []string) { @@ -565,6 +571,8 @@ func generateExampleEnv(cmd *cobra.Command, args []string) { fmt.Println(strings.Join(dashedList, "")) } fmt.Println(strings.Join(fullyGeneratedDocuments, "")) + + Telemetry.CaptureEvent("cli-command:generate-example-env", posthog.NewProperties().Set("secretCount", len(secrets)).Set("version", util.CLI_VERSION)) } func CenterString(s string, numStars int) string { diff --git a/cli/packages/cmd/user.go b/cli/packages/cmd/user.go index 6fef0ac8a0..0d8f1b214d 100644 --- a/cli/packages/cmd/user.go +++ b/cli/packages/cmd/user.go @@ -8,6 +8,7 @@ import ( "github.com/Infisical/infisical-merge/packages/models" "github.com/Infisical/infisical-merge/packages/util" "github.com/manifoldco/promptui" + "github.com/posthog/posthog-go" "github.com/spf13/cobra" ) @@ -78,6 +79,8 @@ var switchCmd = &cobra.Command{ if err != nil { util.HandleError(err, "") } + + Telemetry.CaptureEvent("cli-command:user switch", posthog.NewProperties().Set("numberOfLoggedInProfiles", len(loggedInProfiles)).Set("version", util.CLI_VERSION)) }, } @@ -174,7 +177,7 @@ var domainCmd = &cobra.Command{ if err != nil { util.HandleError(err, "") } - + Telemetry.CaptureEvent("cli-command:user domain", posthog.NewProperties().Set("version", util.CLI_VERSION)) }, } diff --git a/cli/packages/cmd/vault.go b/cli/packages/cmd/vault.go index 66c4f595b8..e6662c5d62 100644 --- a/cli/packages/cmd/vault.go +++ b/cli/packages/cmd/vault.go @@ -8,6 +8,7 @@ import ( "github.com/99designs/keyring" "github.com/Infisical/infisical-merge/packages/util" + "github.com/posthog/posthog-go" "github.com/rs/zerolog/log" "github.com/spf13/cobra" ) @@ -48,6 +49,8 @@ var vaultSetCmd = &cobra.Command{ } fmt.Printf("\nSuccessfully, switched vault backend from [%s] to [%s]. Please login in again to store your login details in the new vault with [infisical login]", currentVaultBackend, wantedVaultTypeName) + + Telemetry.CaptureEvent("cli-command:vault set", posthog.NewProperties().Set("currentVault", currentVaultBackend).Set("wantedVault", wantedVaultTypeName).Set("version", util.CLI_VERSION)) } else { log.Error().Msgf("The requested vault type [%s] is not available on this system. Only the following vault backends are available for you system: %s", wantedVaultTypeName, keyring.AvailableBackends()) } @@ -76,6 +79,8 @@ func printAvailableVaultBackends() { log.Error().Msgf("printAvailableVaultBackends: unable to print the available vault backend because of error [err=%s]", err) } + Telemetry.CaptureEvent("cli-command:vault", posthog.NewProperties().Set("currentVault", currentVaultBackend).Set("version", util.CLI_VERSION)) + fmt.Printf("\n\nYou are currently using [%s] vault to store your login credentials", string(currentVaultBackend)) } diff --git a/cli/packages/telemetry/telemetry.go b/cli/packages/telemetry/telemetry.go new file mode 100644 index 0000000000..166b002e17 --- /dev/null +++ b/cli/packages/telemetry/telemetry.go @@ -0,0 +1,70 @@ +package telemetry + +import ( + "os" + + "github.com/Infisical/infisical-merge/packages/util" + "github.com/denisbrodbeck/machineid" + "github.com/posthog/posthog-go" +) + +type Telemetry struct { + isEnabled bool + posthogClient posthog.Client +} + +func NewTelemetry(telemetryIsEnabled bool) *Telemetry { + posthogAPIKey := os.Getenv("POSTHOG_API_KEY_FOR_CLI") + if posthogAPIKey != "" { + client, _ := posthog.NewWithConfig( + posthogAPIKey, + posthog.Config{}, + ) + + return &Telemetry{isEnabled: telemetryIsEnabled, posthogClient: client} + } else { + return &Telemetry{isEnabled: false} + } +} + +func (t *Telemetry) CaptureEvent(eventName string, properties posthog.Properties) { + userIdentity, err := t.GetDistinctId() + if err != nil { + return + } + + if t.isEnabled { + t.posthogClient.Enqueue(posthog.Capture{ + DistinctId: userIdentity, + Event: eventName, + Properties: properties, + }) + + defer t.posthogClient.Close() + } +} + +func (t *Telemetry) GetDistinctId() (string, error) { + var distinctId string + var outputErr error + + machineId, err := machineid.ID() + if err != nil { + outputErr = err + } + + userDetails, err := util.GetCurrentLoggedInUserDetails() + if err != nil { + outputErr = err + } + + if userDetails.IsUserLoggedIn && userDetails.UserCredentials.Email != "" { + distinctId = userDetails.UserCredentials.Email + } else if machineId != "" { + distinctId = "anonymous_cli_" + machineId + } else { + distinctId = "" + } + + return distinctId, outputErr +}