Skip to content

Commit d965c8f

Browse files
committed
Add a persisted telemetry setting to mapbox config
mapbox config set telemetry off stops the run's telemetry event in every shell, beside MAPBOX_CLI_NO_TELEMETRY for one. The setting is read again at exit, so the run that turns it off does not report itself.
1 parent df533ea commit d965c8f

7 files changed

Lines changed: 45 additions & 11 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,10 @@ that may never merge. They are not releases and are not listed here.
255255
code; it does find a `~/.mapbox/history` directory it didn't before, and
256256
`mapbox config list` now reports a second key, `history`.
257257

258+
- `mapbox config set telemetry off` turns the telemetry event off for good,
259+
in every shell, the way `MAPBOX_CLI_NO_TELEMETRY=1` does for one. The run
260+
that turns it off records nothing either. `mapbox config list` now also
261+
reports `telemetry`.
258262
- Each run sends one `cli.command` telemetry event to Mapbox, from a
259263
background process the command doesn't wait for, with your `mapbox auth
260264
login` token or, when you're logged out, one built into the CLI. A build

‎README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,8 @@ it, and dropped if it can't be delivered. It is sent with your
295295
`mapbox auth login` token when you're logged in, and with a token built into
296296
the CLI otherwise. A build from source has no built-in token, so when you're
297297
logged out it keeps the event in `~/.mapbox/.telemetry/<date>.jsonl` (kept
298-
for 7 days) instead. `MAPBOX_CLI_NO_TELEMETRY=1` turns it off.
298+
for 7 days) instead. `MAPBOX_CLI_NO_TELEMETRY=1` or `mapbox config set telemetry off`
299+
turns it off.
299300

300301
### Diagnostics and settings
301302

‎docs/commands.md‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4126,6 +4126,7 @@ was set in, and stays in every future shell instead.
41264126
| `update-check` | `on` | The update notice; mirrors `MAPBOX_NO_UPDATE_CHECK` (see [Update notices](../README.md#update-notices)) |
41274127
| `history` | `on` | [Command history](../README.md#command-history), read by `mapbox history`; `MAPBOX_HISTORY=0` or `=1` overrides it for a session |
41284128
| `log` | `off` | [Diagnostic logs](../README.md#diagnostic-logs), shown by `mapbox history show`; `MAPBOX_LOG=1` or `=0` overrides it for a session. Needs `history` on: `config set log on` with history off fails with `history_required` |
4129+
| `telemetry` | `on` | The run's telemetry event; mirrors `MAPBOX_CLI_NO_TELEMETRY` (see [Privacy](../README.md#privacy)) |
41294130

41304131
### `mapbox config get`
41314132

@@ -4137,7 +4138,7 @@ than failing, the same forgiving read the update-check cache itself uses.
41374138

41384139
| Parameter | Effect |
41394140
| --- | --- |
4140-
| `<key>` | Which setting to read: `update-check`, `history` or `log`. |
4141+
| `<key>` | Which setting to read: `update-check`, `history`, `log` or `telemetry`. |
41414142

41424143
#### Examples
41434144

@@ -4176,7 +4177,7 @@ without an environment variable.
41764177

41774178
| Parameter | Effect |
41784179
| --- | --- |
4179-
| `<key>` | Which setting to change: `update-check`, `history` or `log`. |
4180+
| `<key>` | Which setting to change: `update-check`, `history`, `log` or `telemetry`. |
41804181
| `<value>` | `on` or `off`. |
41814182

41824183
#### Examples
@@ -4235,6 +4236,7 @@ mapbox config list
42354236
update-check on
42364237
history on
42374238
log off
4239+
telemetry on
42384240
```
42394241

42404242
</td><td>
@@ -4252,6 +4254,10 @@ log off
42524254
{
42534255
"key": "log",
42544256
"value": false
4257+
},
4258+
{
4259+
"key": "telemetry",
4260+
"value": true
42554261
}
42564262
]
42574263
```
@@ -4270,7 +4276,7 @@ default, a key explicitly set to the old default value does not.
42704276

42714277
| Parameter | Effect |
42724278
| --- | --- |
4273-
| `<key>` | Which setting to clear: `update-check`, `history` or `log`. |
4279+
| `<key>` | Which setting to clear: `update-check`, `history`, `log` or `telemetry`. |
42744280

42754281
#### Examples
42764282

‎src/config.rs‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ const CONFIG_FILE: &str = "config.json";
3333
const UPDATE_CHECK_KEY: &str = "update-check";
3434
const HISTORY_KEY: &str = "history";
3535
const LOG_KEY: &str = "log";
36-
const KEYS: &[&str] = &[UPDATE_CHECK_KEY, HISTORY_KEY, LOG_KEY];
36+
const TELEMETRY_KEY: &str = "telemetry";
37+
const KEYS: &[&str] = &[UPDATE_CHECK_KEY, HISTORY_KEY, LOG_KEY, TELEMETRY_KEY];
3738

3839
const ON: &str = "on";
3940
const OFF: &str = "off";
@@ -50,6 +51,8 @@ struct Config {
5051
history: Option<bool>,
5152
#[serde(default, skip_serializing_if = "Option::is_none")]
5253
log: Option<bool>,
54+
#[serde(default, skip_serializing_if = "Option::is_none")]
55+
telemetry: Option<bool>,
5356
}
5457

5558
fn config_path() -> Option<PathBuf> {
@@ -102,6 +105,12 @@ pub fn log_enabled() -> bool {
102105
read_config().log.unwrap_or(false)
103106
}
104107

108+
/// Whether the run's telemetry event may be recorded, per the persisted
109+
/// setting. [`crate::telemetry_event`] checks it alongside `MAPBOX_CLI_NO_TELEMETRY`.
110+
pub fn telemetry_enabled() -> bool {
111+
read_config().telemetry.unwrap_or(true)
112+
}
113+
105114
fn on_off(enabled: bool) -> &'static str {
106115
if enabled {
107116
ON
@@ -118,6 +127,7 @@ fn resolve(config: &Config, key: &str) -> bool {
118127
UPDATE_CHECK_KEY => update_check_setting(config),
119128
HISTORY_KEY => config.history.unwrap_or(true),
120129
LOG_KEY => config.log.unwrap_or(false),
130+
TELEMETRY_KEY => config.telemetry.unwrap_or(true),
121131
_ => unreachable!("clap's value_parser restricts `key` to {KEYS:?}"),
122132
}
123133
}
@@ -132,6 +142,7 @@ fn clear(config: &mut Config, key: &str) {
132142
UPDATE_CHECK_KEY => config.update_check = None,
133143
HISTORY_KEY => config.history = None,
134144
LOG_KEY => config.log = None,
145+
TELEMETRY_KEY => config.telemetry = None,
135146
_ => unreachable!("clap's value_parser restricts `key` to {KEYS:?}"),
136147
}
137148
}
@@ -214,6 +225,7 @@ pub fn set(matches: &ArgMatches, mode: Mode) -> Result<()> {
214225
UPDATE_CHECK_KEY => config.update_check = Some(enabled),
215226
HISTORY_KEY => config.history = Some(enabled),
216227
LOG_KEY => config.log = Some(enabled),
228+
TELEMETRY_KEY => config.telemetry = Some(enabled),
217229
_ => unreachable!("clap's value_parser restricts `key` to {KEYS:?}"),
218230
}
219231
write_config(&config)?;

‎src/telemetry_event.rs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
//!
1919
//! Best-effort throughout: nothing here can change a command's output, its
2020
//! exit code, or how long it takes to return. With telemetry off
21-
//! (`MAPBOX_CLI_NO_TELEMETRY`), nothing is built or written.
21+
//! (`MAPBOX_CLI_NO_TELEMETRY`, or `mapbox config set telemetry off`),
22+
//! nothing is built or written.
2223
2324
use std::io::IsTerminal;
2425
use std::path::Path;
@@ -303,7 +304,9 @@ fn with_workflow(f: impl FnOnce(&mut Option<Workflow>)) {
303304

304305
/// Builds the run's event and hands it to the sink, unless telemetry is off.
305306
pub(crate) fn deliver(record: &Record) {
306-
if !telemetry::telemetry_allowed() {
307+
// Read at the end of the run: the run that turns telemetry off is one
308+
// that should not report itself.
309+
if !telemetry::telemetry_allowed() || !crate::config::telemetry_enabled() {
307310
return;
308311
}
309312
let event = build(record);

‎tests/config.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ fn list_reports_every_setting_including_an_unset_one() {
135135
assert!(empty.status.success());
136136
assert_eq!(
137137
stdout(&empty),
138-
r#"[{"key":"update-check","value":true},{"key":"history","value":true},{"key":"log","value":false}]"#
138+
r#"[{"key":"update-check","value":true},{"key":"history","value":true},{"key":"log","value":false},{"key":"telemetry","value":true}]"#
139139
);
140140

141141
let set = command(&home)
@@ -151,7 +151,7 @@ fn list_reports_every_setting_including_an_unset_one() {
151151
assert!(after.status.success());
152152
assert_eq!(
153153
stdout(&after),
154-
r#"[{"key":"update-check","value":false},{"key":"history","value":true},{"key":"log","value":false}]"#
154+
r#"[{"key":"update-check","value":false},{"key":"history","value":true},{"key":"log","value":false},{"key":"telemetry","value":true}]"#
155155
);
156156

157157
let text = command(&home)
@@ -161,7 +161,7 @@ fn list_reports_every_setting_including_an_unset_one() {
161161
assert!(text.status.success());
162162
assert_eq!(
163163
stdout(&text),
164-
"update-check off\nhistory on\nlog off"
164+
"update-check off\nhistory on\nlog off\ntelemetry on"
165165
);
166166
}
167167

‎tests/telemetry_events.rs‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ fn help_version_and_usage_errors_record_their_invocation() {
209209
}
210210

211211
#[test]
212-
fn the_opt_out_records_nothing() {
212+
fn either_opt_out_records_nothing() {
213213
let home = scratch("opt-out-env");
214214
let out = command(&home)
215215
.env("MAPBOX_CLI_NO_TELEMETRY", "1")
@@ -221,6 +221,14 @@ fn the_opt_out_records_nothing() {
221221
!config_dir(&home).join(".telemetry").exists(),
222222
"MAPBOX_CLI_NO_TELEMETRY=1 still wrote telemetry"
223223
);
224+
225+
let home = scratch("opt-out-config");
226+
assert!(run(&home, &["config", "set", "telemetry", "off"])
227+
.status
228+
.success());
229+
// That run records nothing either: the setting it wrote is read at exit.
230+
let _ = run(&home, &["config", "list"]);
231+
assert_eq!(events(&home), Vec::<Value>::new());
224232
}
225233

226234
#[test]

0 commit comments

Comments
 (0)