Skip to content

Commit

Permalink
chore: small improvements gacli
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Nov 29, 2023
1 parent 458cf15 commit b1c135f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 28 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
57 changes: 32 additions & 25 deletions ignite/cmd/ignite/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ import (
"path/filepath"
"strings"

"github.com/Pallinder/go-randomdata"
"github.com/manifoldco/promptui"

"github.com/ignite/cli/ignite/pkg/gacli"
"github.com/ignite/cli/ignite/version"
)

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
Expand Down Expand Up @@ -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,
}

Expand All @@ -74,48 +73,56 @@ 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
}
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)
}
2 changes: 2 additions & 0 deletions ignite/pkg/gacli/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package gacli is a client for Google Analytics to send data points for hint-type=event.
package gacli

0 comments on commit b1c135f

Please sign in to comment.