66//! file beside the credentials, written through the same
77//! [`crate::auth::write_private`] so it gets the same `0600` treatment.
88//!
9- //! One setting today — `update-check` — with room for more: `get`/`set`/
10- //! `unset` take a `key`, restricted by clap to [`KEYS`], so adding a second
9+ //! Two settings — `update-check` and `telemetry` — with room for more:
10+ //! `get`/`set`/` unset` take a `key`, restricted by clap to [`KEYS`], so a new
1111//! setting is a new key and a new match arm rather than a new subcommand.
1212//! `list` needs no key at all: it walks [`KEYS`] and reports every setting's
1313//! current value in one call, which `get` cannot — the whole reason it
@@ -30,7 +30,8 @@ pub const COMMAND: &str = "config";
3030const CONFIG_FILE : & str = "config.json" ;
3131
3232const UPDATE_CHECK_KEY : & str = "update-check" ;
33- const KEYS : & [ & str ] = & [ UPDATE_CHECK_KEY ] ;
33+ const TELEMETRY_KEY : & str = "telemetry" ;
34+ const KEYS : & [ & str ] = & [ UPDATE_CHECK_KEY , TELEMETRY_KEY ] ;
3435
3536const ON : & str = "on" ;
3637const OFF : & str = "off" ;
@@ -43,6 +44,8 @@ const OFF: &str = "off";
4344struct Config {
4445 #[ serde( default , skip_serializing_if = "Option::is_none" ) ]
4546 update_check : Option < bool > ,
47+ #[ serde( default , skip_serializing_if = "Option::is_none" ) ]
48+ telemetry : Option < bool > ,
4649}
4750
4851fn config_path ( ) -> Option < PathBuf > {
@@ -83,6 +86,12 @@ pub fn update_check_enabled() -> bool {
8386 update_check_setting ( & read_config ( ) )
8487}
8588
89+ /// Whether the run's telemetry event may be recorded, per the persisted
90+ /// setting. [`crate::telemetry_event`] checks it alongside `MAPBOX_CLI_NO_TELEMETRY`.
91+ pub fn telemetry_enabled ( ) -> bool {
92+ read_config ( ) . telemetry . unwrap_or ( true )
93+ }
94+
8695fn on_off ( enabled : bool ) -> & ' static str {
8796 if enabled {
8897 ON
@@ -97,6 +106,7 @@ fn on_off(enabled: bool) -> &'static str {
97106fn resolve ( config : & Config , key : & str ) -> bool {
98107 match key {
99108 UPDATE_CHECK_KEY => update_check_setting ( config) ,
109+ TELEMETRY_KEY => config. telemetry . unwrap_or ( true ) ,
100110 _ => unreachable ! ( "clap's value_parser restricts `key` to {KEYS:?}" ) ,
101111 }
102112}
@@ -109,6 +119,7 @@ fn resolve(config: &Config, key: &str) -> bool {
109119fn clear ( config : & mut Config , key : & str ) {
110120 match key {
111121 UPDATE_CHECK_KEY => config. update_check = None ,
122+ TELEMETRY_KEY => config. telemetry = None ,
112123 _ => unreachable ! ( "clap's value_parser restricts `key` to {KEYS:?}" ) ,
113124 }
114125}
@@ -173,6 +184,7 @@ pub fn set(matches: &ArgMatches, mode: Mode) -> Result<()> {
173184 let mut config = read_config ( ) ;
174185 match key. as_str ( ) {
175186 UPDATE_CHECK_KEY => config. update_check = Some ( enabled) ,
187+ TELEMETRY_KEY => config. telemetry = Some ( enabled) ,
176188 _ => unreachable ! ( "clap's value_parser restricts `key` to {KEYS:?}" ) ,
177189 }
178190 write_config ( & config) ?;
@@ -228,6 +240,7 @@ mod tests {
228240 fn the_config_round_trips_and_tolerates_an_empty_one ( ) {
229241 let off = Config {
230242 update_check : Some ( false ) ,
243+ ..Config :: default ( )
231244 } ;
232245 let text = serde_json:: to_string ( & off) . expect ( "serialize" ) ;
233246 assert_eq ! ( text, r#"{"update_check":false}"# ) ;
@@ -249,7 +262,10 @@ mod tests {
249262 #[ test]
250263 fn resolve_matches_update_check_setting_at_every_state ( ) {
251264 for update_check in [ None , Some ( true ) , Some ( false ) ] {
252- let config = Config { update_check } ;
265+ let config = Config {
266+ update_check,
267+ ..Config :: default ( )
268+ } ;
253269 assert_eq ! (
254270 resolve( & config, UPDATE_CHECK_KEY ) ,
255271 update_check_setting( & config)
@@ -265,13 +281,15 @@ mod tests {
265281 fn clear_removes_the_key_rather_than_writing_the_default ( ) {
266282 let mut explicit_default = Config {
267283 update_check : Some ( true ) ,
284+ ..Config :: default ( )
268285 } ;
269286 clear ( & mut explicit_default, UPDATE_CHECK_KEY ) ;
270287 assert_eq ! ( explicit_default, Config :: default ( ) ) ;
271288 assert_eq ! ( explicit_default. update_check, None ) ;
272289
273290 let mut explicit_off = Config {
274291 update_check : Some ( false ) ,
292+ ..Config :: default ( )
275293 } ;
276294 clear ( & mut explicit_off, UPDATE_CHECK_KEY ) ;
277295 assert_eq ! ( explicit_off. update_check, None ) ;
0 commit comments