From a5a0ec074f5dee321d125613d558aafa367a7fcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Qu=C3=A9r=C3=A9?= Date: Mon, 10 Feb 2020 19:06:19 +0100 Subject: [PATCH 1/2] refactor: add an option to disable telemetry in BootstrapConfig --- internal/core/bootstrap.go | 16 +++++++++++----- internal/core/testing.go | 26 ++++++++++++++------------ 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/internal/core/bootstrap.go b/internal/core/bootstrap.go index 6ff5c71e4a..c7f683bf83 100644 --- a/internal/core/bootstrap.go +++ b/internal/core/bootstrap.go @@ -32,6 +32,10 @@ type BootstrapConfig struct { // If provided this client will be passed to all commands. // If not a client will be automatically created by the CLI using Config, Env and flags see createClient(). Client *scw.Client + + // DisableTelemetry, if set to true this will disable telemetry report no matter what the config sent_telemetry is set to. + // This is useful when running test to avoid sending meaningless telemetries. + DisableTelemetry bool } // 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 start := time.Now() defer func() { // skip telemetry report when at least one of the following criteria matches: - // - version is not a release - // - telemetry is disabled on the current command // - telemetry is disabled from the config (user must consent) - if (!matomo.ForceTelemetry && !config.BuildInfo.IsRelease()) || - (meta.command == nil || meta.command.DisableTelemetry) || - !matomo.IsTelemetryEnabled() { + // - no valid command was executed + // - telemetry is disabled on the ran command + // - telemetry is explicitly disable in bootstrap config + if !matomo.IsTelemetryEnabled() || + meta.command == nil || + meta.command.DisableTelemetry || + config.DisableTelemetry { logger.Debugf("skipping telemetry report") return } diff --git a/internal/core/testing.go b/internal/core/testing.go index 27a51b7f48..bc0d875d71 100644 --- a/internal/core/testing.go +++ b/internal/core/testing.go @@ -169,12 +169,13 @@ func Test(config *TestConfig) func(t *testing.T) { stderrBuffer := &bytes.Buffer{} logger.Debugf("command: %s", cmdTemplate(cmd)) _, result, err := Bootstrap(&BootstrapConfig{ - Args: strings.Split(cmdTemplate(cmd), " "), - Commands: config.Commands, - BuildInfo: &config.BuildInfo, - Stdout: stdoutBuffer, - Stderr: stderrBuffer, - Client: client, + Args: strings.Split(cmdTemplate(cmd), " "), + Commands: config.Commands, + BuildInfo: &config.BuildInfo, + Stdout: stdoutBuffer, + Stderr: stderrBuffer, + Client: client, + DisableTelemetry: true, }) require.NoError(t, err, "stdout: %s\nstderr: %s", stdoutBuffer.String(), stderrBuffer.String()) @@ -200,12 +201,13 @@ func Test(config *TestConfig) func(t *testing.T) { stderr := &bytes.Buffer{} logger.Debugf("command: %s", cmdTemplate(config.Cmd)) exitCode, result, err = Bootstrap(&BootstrapConfig{ - Args: strings.Split(cmdTemplate(config.Cmd), " "), - Commands: config.Commands, - BuildInfo: &config.BuildInfo, - Stdout: stdout, - Stderr: stderr, - Client: client, + Args: strings.Split(cmdTemplate(config.Cmd), " "), + Commands: config.Commands, + BuildInfo: &config.BuildInfo, + Stdout: stdout, + Stderr: stderr, + Client: client, + DisableTelemetry: true, }) config.Check(t, &CheckFuncCtx{ From b45f0dd54e547c18aeeb78dd938313a9ce374eb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Qu=C3=A9r=C3=A9?= Date: Mon, 10 Feb 2020 19:37:27 +0100 Subject: [PATCH 2/2] address comments --- internal/core/bootstrap.go | 12 ++++++------ internal/matomo/matomo.go | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/internal/core/bootstrap.go b/internal/core/bootstrap.go index c7f683bf83..f7246ea705 100644 --- a/internal/core/bootstrap.go +++ b/internal/core/bootstrap.go @@ -33,7 +33,7 @@ type BootstrapConfig struct { // If not a client will be automatically created by the CLI using Config, Env and flags see createClient(). Client *scw.Client - // DisableTelemetry, if set to true this will disable telemetry report no matter what the config sent_telemetry is set to. + // DisableTelemetry, if set to true this will disable telemetry report no matter what the config send_telemetry is set to. // This is useful when running test to avoid sending meaningless telemetries. DisableTelemetry bool } @@ -66,14 +66,14 @@ func Bootstrap(config *BootstrapConfig) (exitCode int, result interface{}, err e start := time.Now() defer func() { // skip telemetry report when at least one of the following criteria matches: - // - telemetry is disabled from the config (user must consent) - // - no valid command was executed - // - telemetry is disabled on the ran command // - telemetry is explicitly disable in bootstrap config - if !matomo.IsTelemetryEnabled() || + // - no command was executed + // - telemetry is disabled on the ran command + // - telemetry is disabled from the config (user must consent) + if config.DisableTelemetry || meta.command == nil || meta.command.DisableTelemetry || - config.DisableTelemetry { + matomo.IsTelemetryDisabled() { logger.Debugf("skipping telemetry report") return } diff --git a/internal/matomo/matomo.go b/internal/matomo/matomo.go index f61a2d539f..799398b054 100644 --- a/internal/matomo/matomo.go +++ b/internal/matomo/matomo.go @@ -111,11 +111,11 @@ func generateRandNumber() string { return bigRand.String() } -// IsTelemetryEnabled returns true when the Opt-In send_telemetry attribute in the config is set. -func IsTelemetryEnabled() bool { +// IsTelemetryDisabled returns true when the Opt-In send_telemetry attribute in the config is set. +func IsTelemetryDisabled() bool { config, err := scw.LoadConfig() if err != nil { return false } - return config.SendUsage + return !config.SendUsage }