Skip to content

Commit 752e255

Browse files
authored
CLOUDP-124943: Enable/disable telemetry in mongosh when changed in atlas cli (#1306)
1 parent 620a6df commit 752e255

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

internal/cli/config/set.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/mongodb/mongodb-atlas-cli/internal/cli"
2222
"github.com/mongodb/mongodb-atlas-cli/internal/cli/require"
2323
"github.com/mongodb/mongodb-atlas-cli/internal/config"
24+
"github.com/mongodb/mongodb-atlas-cli/internal/mongosh"
2425
"github.com/mongodb/mongodb-atlas-cli/internal/search"
2526
"github.com/mongodb/mongodb-atlas-cli/internal/validate"
2627
"github.com/spf13/cobra"
@@ -53,6 +54,14 @@ func (opts *SetOpts) Run() error {
5354
} else {
5455
opts.store.Set(opts.prop, value)
5556
}
57+
58+
if opts.prop == config.TelemetryEnabledProperty && mongosh.Detect() {
59+
err := mongosh.SetTelemetry(value.(bool))
60+
if err != nil {
61+
return fmt.Errorf("error setting telemetry on mongosh: %w", err)
62+
}
63+
}
64+
5665
if err := opts.store.Save(); err != nil {
5766
return err
5867
}

internal/config/profile.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const (
6666
configPerm = 0600
6767
defaultPermissions = 0700
6868
skipUpdateCheck = "skip_update_check"
69-
telemetryEnabled = "telemetry_enabled"
69+
TelemetryEnabledProperty = "telemetry_enabled"
7070
MongoCLI = "mongocli"
7171
AtlasCLI = "atlascli"
7272
)
@@ -123,7 +123,7 @@ func Properties() []string {
123123
opsManagerSkipVerify,
124124
mongoShellPath,
125125
skipUpdateCheck,
126-
telemetryEnabled,
126+
TelemetryEnabledProperty,
127127
AccessTokenField,
128128
RefreshTokenField,
129129
}
@@ -132,14 +132,14 @@ func Properties() []string {
132132
func BooleanProperties() []string {
133133
return []string{
134134
skipUpdateCheck,
135-
telemetryEnabled,
135+
TelemetryEnabledProperty,
136136
}
137137
}
138138

139139
func GlobalProperties() []string {
140140
return []string{
141141
skipUpdateCheck,
142-
telemetryEnabled,
142+
TelemetryEnabledProperty,
143143
mongoShellPath,
144144
}
145145
}
@@ -419,13 +419,13 @@ func (*Profile) SetSkipUpdateCheck(v bool) {
419419
// IsTelemetryEnabledSet return true if telemetry_enabled has been set.
420420
func IsTelemetryEnabledSet() bool { return Default().IsTelemetryEnabledSet() }
421421
func (*Profile) IsTelemetryEnabledSet() bool {
422-
return viper.IsSet(telemetryEnabled)
422+
return viper.IsSet(TelemetryEnabledProperty)
423423
}
424424

425425
// TelemetryEnabled get the configured telemetry enabled value.
426426
func TelemetryEnabled() bool { return Default().TelemetryEnabled() }
427427
func (p *Profile) TelemetryEnabled() bool {
428-
return isTelemetryFeatureAllowed() && p.GetBoolWithDefault(telemetryEnabled, true)
428+
return isTelemetryFeatureAllowed() && p.GetBoolWithDefault(TelemetryEnabledProperty, true)
429429
}
430430

431431
// SetTelemetryEnabled sets the telemetry enabled value.
@@ -435,7 +435,7 @@ func (*Profile) SetTelemetryEnabled(v bool) {
435435
if !isTelemetryFeatureAllowed() {
436436
return
437437
}
438-
SetGlobal(telemetryEnabled, v)
438+
SetGlobal(TelemetryEnabledProperty, v)
439439
}
440440

441441
func boolEnv(key string) bool {

internal/mongosh/mongosh.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,20 @@ func binPath() string {
3232
return ""
3333
}
3434

35-
func Run(username, password, mongoURI string) error {
36-
args := []string{mongoshBin, "-u", username, "-p", password, mongoURI}
35+
func execCommand(args ...string) error {
36+
a := append([]string{mongoshBin}, args...)
3737
env := os.Environ()
38-
return syscall.Exec(binPath(), args, env) //nolint:gosec // false positive, this path won't be tampered
38+
return syscall.Exec(binPath(), a, env) //nolint:gosec // false positive, this path won't be tampered
39+
}
40+
41+
func SetTelemetry(enable bool) error {
42+
cmd := "disableTelemetry()"
43+
if enable {
44+
cmd = "enableTelemetry()"
45+
}
46+
return execCommand("--nodb", "--eval", cmd)
47+
}
48+
49+
func Run(username, password, mongoURI string) error {
50+
return execCommand("-u", username, "-p", password, mongoURI)
3951
}

internal/telemetry/command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func StartTrackingCommand(cmd *cobra.Command, args []string) {
4444
}
4545

4646
func FinishTrackingCommand(opt TrackOptions) {
47-
if !config.TelemetryEnabled() {
47+
if !config.TelemetryEnabled() || currentTracker == nil {
4848
return
4949
}
5050

0 commit comments

Comments
 (0)