66//! delivery change — a new endpoint, batching — happen without touching them.
77//!
88//! [`deliver`] is the one place that picks the [`Sink`]. [`HttpSink`] sends
9- //! the event to Mapbox Events when this build has somewhere to send it — a
10- //! token compiled in, or [`URL_ENV`]. Otherwise [`FileSink`] keeps it on this
11- //! machine, which is what `cargo build` and `cargo test` get.
9+ //! the event to Mapbox Events when there is a token to send it with — from
10+ //! [`crate::cli_token`] — or a [`URL_ENV`]. Otherwise [`FileSink`] keeps it
11+ //! on this machine, which is what `cargo test` and a logged-out source build
12+ //! get.
1213//!
1314//! It also owns the directory, `~/.mapbox/.telemetry` (or under
1415//! `$MAPBOX_CONFIG_DIR`), written only once the config directory exists,
@@ -25,6 +26,7 @@ use reqwest::header::{CONTENT_TYPE, USER_AGENT};
2526use serde_json:: json;
2627
2728use crate :: auth;
29+ use crate :: cli_token;
2830use crate :: dated_jsonl;
2931use crate :: http;
3032use crate :: telemetry;
@@ -48,19 +50,14 @@ const _: () = assert!(
4850 "telemetry_sink::ENDPOINT is still staging; switch it to PRODUCTION_ENDPOINT before a production release"
4951) ;
5052
51- /// The CLI's own `pk.` token for Mapbox Events, compiled into release
52- /// builds. Never the user's: the event must not be tied to their account.
53- const TOKEN : Option < & str > = option_env ! ( "MAPBOX_CLI_TELEMETRY_TOKEN" ) ;
54-
5553/// Set by [`HttpSink`] on the child it spawns, and read by [`is_send_child`]
5654/// at the top of `main` — a mode of this binary rather than a hidden
5755/// subcommand, for the reason `update_check` gives.
5856const SEND_ENV : & str = "MAPBOX_INTERNAL_TELEMETRY_SEND" ;
5957
60- /// The full URL to send to, token included, overriding [`ENDPOINT`] and
61- /// [`TOKEN`]. The parent hands it to the child; it is also the seam
62- /// `tests/telemetry_events.rs` points at a loopback server, and how a
63- /// development build sends to staging without a compiled-in token.
58+ /// The full URL to send to, token included, overriding [`ENDPOINT`] and the
59+ /// token. The parent hands it to the child; it is also the seam
60+ /// `tests/telemetry_events.rs` points at a loopback server.
6461const URL_ENV : & str = "MAPBOX_INTERNAL_TELEMETRY_URL" ;
6562
6663/// Turns on the delivery log: one line when an event is handed to the child
@@ -151,19 +148,20 @@ impl Sink for HttpSink {
151148}
152149
153150/// Hands `line` to [`HttpSink`] when there is a URL to send to, and to
154- /// [`FileSink`] otherwise.
155- pub ( crate ) fn deliver ( line : & str ) {
156- match send_url ( ) {
151+ /// [`FileSink`] otherwise. `profile` is the run's `--profile`, whose login
152+ /// [`crate::cli_token`] may send with.
153+ pub ( crate ) fn deliver ( line : & str , profile : Option < & str > ) {
154+ match send_url ( profile) {
157155 Some ( url) => HttpSink { url } . deliver ( line) ,
158156 None => FileSink . deliver ( line) ,
159157 }
160158}
161159
162- fn send_url ( ) -> Option < String > {
160+ fn send_url ( profile : Option < & str > ) -> Option < String > {
163161 if let Some ( url) = from_environment ( URL_ENV ) {
164162 return Some ( url) ;
165163 }
166- TOKEN . map ( |token| format ! ( "{ENDPOINT}?access_token={token}" ) )
164+ cli_token :: for_background ( profile ) . map ( |token| format ! ( "{ENDPOINT}?access_token={token}" ) )
167165}
168166
169167/// Whether this process is the sender rather than a command.
0 commit comments