From b1c135ffbbc561c7ff5c401baba8e13d48f803fe Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Wed, 29 Nov 2023 17:11:42 +0100 Subject: [PATCH 1/2] chore: small improvements gacli --- go.mod | 1 - go.sum | 2 -- ignite/cmd/ignite/analytics.go | 57 +++++++++++++++++++--------------- ignite/pkg/gacli/doc.go | 2 ++ 4 files changed, 34 insertions(+), 28 deletions(-) create mode 100644 ignite/pkg/gacli/doc.go diff --git a/go.mod b/go.mod index 7111527d3e..d218510d02 100644 --- a/go.mod +++ b/go.mod @@ -22,7 +22,6 @@ require ( github.com/99designs/keyring v1.2.2 github.com/AlecAivazis/survey/v2 v2.3.7 github.com/DATA-DOG/go-sqlmock v1.5.0 - github.com/Pallinder/go-randomdata v1.2.0 github.com/blang/semver/v4 v4.0.0 github.com/briandowns/spinner v1.23.0 github.com/bufbuild/buf v1.15.1 diff --git a/go.sum b/go.sum index 8d8fb14a0a..44254abd4c 100644 --- a/go.sum +++ b/go.sum @@ -129,8 +129,6 @@ github.com/OneOfOne/xxhash v1.2.8 h1:31czK/TI9sNkxIKfaUfGlU47BAxQ0ztGgd9vPyqimf8 github.com/OneOfOne/xxhash v1.2.8/go.mod h1:eZbhyaAYD41SGSSsnmcpxVoRiQ/MPUTjUdIIOT9Um7Q= github.com/OpenPeeDeeP/depguard/v2 v2.1.0 h1:aQl70G173h/GZYhWf36aE5H0KaujXfVMnn/f1kSDVYY= github.com/OpenPeeDeeP/depguard/v2 v2.1.0/go.mod h1:PUBgk35fX4i7JDmwzlJwJ+GMe6NfO1723wmJMgPThNQ= -github.com/Pallinder/go-randomdata v1.2.0 h1:DZ41wBchNRb/0GfsePLiSwb0PHZmT67XY00lCDlaYPg= -github.com/Pallinder/go-randomdata v1.2.0/go.mod h1:yHmJgulpD2Nfrm0cR9tI/+oAgRqCQQixsA8HyRZfV9Y= github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 h1:kkhsdkhsCvIsutKu5zLMgWtgh9YxGCNAw8Ad8hjwfYg= github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0= github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo= diff --git a/ignite/cmd/ignite/analytics.go b/ignite/cmd/ignite/analytics.go index a07a0dbeb7..62fbc241ee 100644 --- a/ignite/cmd/ignite/analytics.go +++ b/ignite/cmd/ignite/analytics.go @@ -6,7 +6,6 @@ import ( "path/filepath" "strings" - "github.com/Pallinder/go-randomdata" "github.com/manifoldco/promptui" "github.com/ignite/cli/ignite/pkg/gacli" @@ -14,10 +13,10 @@ import ( ) const ( - gaid = "UA-290147351-1" - envDoNotTrack = "DO_NOT_TRACK" - igniteDir = ".ignite" - igniteAnonIdentity = "anon" + gaid = "UA-290147351-1" + envDoNotTrack = "DO_NOT_TRACK" + igniteDir = ".ignite" + igniteIdentity = "identity.json" ) var gaclient gacli.Client @@ -50,14 +49,14 @@ func addCmdMetric(m metric) { return } - ident, err := prepareMetrics() - if err != nil { + dntInfo, err := checkDNT() + if err != nil || dntInfo.DoNotTrack { return } met := gacli.Metric{ Action: m.command, - User: ident.Name, + User: dntInfo.Name, Version: version.Version, } @@ -74,12 +73,11 @@ func addCmdMetric(m metric) { if len(cmds) > 0 { met.Label = cmds[1] } - go func() { - gaclient.Send(met) - }() + + go gaclient.Send(met) } -func prepareMetrics() (identity, error) { +func checkDNT() (identity, error) { home, err := os.UserHomeDir() if err != nil { return identity{}, err @@ -87,35 +85,44 @@ func prepareMetrics() (identity, error) { if err := os.Mkdir(filepath.Join(home, igniteDir), 0o700); err != nil && !os.IsExist(err) { return identity{}, err } - anonPath := filepath.Join(home, igniteDir, igniteAnonIdentity) - data, err := os.ReadFile(anonPath) + identityPath := filepath.Join(home, igniteDir, igniteIdentity) + data, err := os.ReadFile(identityPath) if err != nil && !os.IsNotExist(err) { return identity{}, err } - i := identity{ - Name: randomdata.SillyName(), - DoNotTrack: false, + var i identity + if err := json.Unmarshal(data, &i); err == nil { + return i, nil } - if len(data) > 0 { - return i, json.Unmarshal(data, &i) + + hostname, err := os.Hostname() + if err != nil { + hostname = "unknown" } + i.Name = hostname + i.DoNotTrack = false - prompt := promptui.Prompt{ - Label: "Ignite would like to collect metrics about command usage. " + - "All data will be anonymous and helps to improve Ignite. " + + prompt := promptui.Select{ + Label: "Ignite collects metrics about command usage. " + + "All data is anonymous and helps to improve Ignite. " + "Ignite respect the DNT rules (consoledonottrack.com). " + "Would you agree to share these metrics with us?", - IsConfirm: true, + Items: []string{"Yes", "No"}, } - if _, err := prompt.Run(); err != nil { + resultID, _, err := prompt.Run() + if err != nil { return identity{}, err } + if resultID != 0 { + i.DoNotTrack = true + } + data, err = json.Marshal(&i) if err != nil { return i, err } - return i, os.WriteFile(anonPath, data, 0o700) + return i, os.WriteFile(identityPath, data, 0o700) } diff --git a/ignite/pkg/gacli/doc.go b/ignite/pkg/gacli/doc.go new file mode 100644 index 0000000000..9b905a5d3a --- /dev/null +++ b/ignite/pkg/gacli/doc.go @@ -0,0 +1,2 @@ +// Package gacli is a client for Google Analytics to send data points for hint-type=event. +package gacli From 257939bfa34b6329aa2fb99726d1063599029b4b Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Wed, 29 Nov 2023 17:15:12 +0100 Subject: [PATCH 2/2] duplicate --- ignite/pkg/gacli/gacli.go | 1 - 1 file changed, 1 deletion(-) diff --git a/ignite/pkg/gacli/gacli.go b/ignite/pkg/gacli/gacli.go index 39fb52a65e..1561ee975f 100644 --- a/ignite/pkg/gacli/gacli.go +++ b/ignite/pkg/gacli/gacli.go @@ -1,4 +1,3 @@ -// Package gacli is a client for Google Analytics to send data points for hint-type=event. package gacli import (