Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: small improvements gacli #3789

Merged
merged 3 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should not add external libraries for everything. Here it was used only for random names.
Which I find overkilled to have a library for that. We can as well just use the hostname.

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
59 changes: 33 additions & 26 deletions ignite/cmd/ignite/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@ 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 = "G-<ID>"
gaSecret = "<API_SECRET>"
envDoNotTrack = "DO_NOT_TRACK"
igniteDir = ".ignite"
igniteAnonIdentity = "anon"
gaID = "G-<ID>"
gaSecret = "<API_SECRET>"
envDoNotTrack = "DO_NOT_TRACK"
igniteDir = ".ignite"
igniteIdentity = "identity.json"
)

var gaclient gacli.Client
Expand Down Expand Up @@ -51,14 +50,14 @@ func addCmdMetric(m metric) {
return
}

ident, err := prepareMetrics()
if err != nil {
dntInfo, err := checkDNT()
if err != nil || dntInfo.DoNotTrack {
return
}

met := gacli.Metric{
FullCmd: m.command,
User: ident.Name,
User: dntInfo.Name,
Version: version.Version,
}

Expand All @@ -75,48 +74,56 @@ func addCmdMetric(m metric) {
if len(cmds) > 0 {
met.Cmd = cmds[1]
}
go func() {
gaclient.SendMetric(met)
}()

go gaclient.SendMetric(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
1 change: 0 additions & 1 deletion ignite/pkg/gacli/gacli.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Package gacli is a client for Google Analytics to send data points for hint-type=event.
package gacli

import (
Expand Down