Skip to content

Commit 9ff532a

Browse files
authored
refactor: add an option to disable telemetry in BootstrapConfig (#681)
1 parent 19c8c5e commit 9ff532a

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

internal/core/bootstrap.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ type BootstrapConfig struct {
3232
// If provided this client will be passed to all commands.
3333
// If not a client will be automatically created by the CLI using Config, Env and flags see createClient().
3434
Client *scw.Client
35+
36+
// DisableTelemetry, if set to true this will disable telemetry report no matter what the config send_telemetry is set to.
37+
// This is useful when running test to avoid sending meaningless telemetries.
38+
DisableTelemetry bool
3539
}
3640

3741
// Bootstrap is the main entry point. It is directly called from main.
@@ -62,12 +66,14 @@ func Bootstrap(config *BootstrapConfig) (exitCode int, result interface{}, err e
6266
start := time.Now()
6367
defer func() {
6468
// skip telemetry report when at least one of the following criteria matches:
65-
// - version is not a release
66-
// - telemetry is disabled on the current command
69+
// - telemetry is explicitly disable in bootstrap config
70+
// - no command was executed
71+
// - telemetry is disabled on the ran command
6772
// - telemetry is disabled from the config (user must consent)
68-
if (!matomo.ForceTelemetry && !config.BuildInfo.IsRelease()) ||
69-
(meta.command == nil || meta.command.DisableTelemetry) ||
70-
!matomo.IsTelemetryEnabled() {
73+
if config.DisableTelemetry ||
74+
meta.command == nil ||
75+
meta.command.DisableTelemetry ||
76+
matomo.IsTelemetryDisabled() {
7177
logger.Debugf("skipping telemetry report")
7278
return
7379
}

internal/core/testing.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,13 @@ func Test(config *TestConfig) func(t *testing.T) {
169169
stderrBuffer := &bytes.Buffer{}
170170
logger.Debugf("command: %s", cmdTemplate(cmd))
171171
_, result, err := Bootstrap(&BootstrapConfig{
172-
Args: strings.Split(cmdTemplate(cmd), " "),
173-
Commands: config.Commands,
174-
BuildInfo: &config.BuildInfo,
175-
Stdout: stdoutBuffer,
176-
Stderr: stderrBuffer,
177-
Client: client,
172+
Args: strings.Split(cmdTemplate(cmd), " "),
173+
Commands: config.Commands,
174+
BuildInfo: &config.BuildInfo,
175+
Stdout: stdoutBuffer,
176+
Stderr: stderrBuffer,
177+
Client: client,
178+
DisableTelemetry: true,
178179
})
179180
require.NoError(t, err, "stdout: %s\nstderr: %s", stdoutBuffer.String(), stderrBuffer.String())
180181

@@ -200,12 +201,13 @@ func Test(config *TestConfig) func(t *testing.T) {
200201
stderr := &bytes.Buffer{}
201202
logger.Debugf("command: %s", cmdTemplate(config.Cmd))
202203
exitCode, result, err = Bootstrap(&BootstrapConfig{
203-
Args: strings.Split(cmdTemplate(config.Cmd), " "),
204-
Commands: config.Commands,
205-
BuildInfo: &config.BuildInfo,
206-
Stdout: stdout,
207-
Stderr: stderr,
208-
Client: client,
204+
Args: strings.Split(cmdTemplate(config.Cmd), " "),
205+
Commands: config.Commands,
206+
BuildInfo: &config.BuildInfo,
207+
Stdout: stdout,
208+
Stderr: stderr,
209+
Client: client,
210+
DisableTelemetry: true,
209211
})
210212

211213
config.Check(t, &CheckFuncCtx{

internal/matomo/matomo.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,11 @@ func generateRandNumber() string {
111111
return bigRand.String()
112112
}
113113

114-
// IsTelemetryEnabled returns true when the Opt-In send_telemetry attribute in the config is set.
115-
func IsTelemetryEnabled() bool {
114+
// IsTelemetryDisabled returns true when the Opt-In send_telemetry attribute in the config is set.
115+
func IsTelemetryDisabled() bool {
116116
config, err := scw.LoadConfig()
117117
if err != nil {
118118
return false
119119
}
120-
return config.SendUsage
120+
return !config.SendUsage
121121
}

0 commit comments

Comments
 (0)