-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathsession.rs
164 lines (139 loc) · 4.94 KB
/
session.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use ci_info::types::Vendor as CiVendor;
use reqwest::blocking::Client;
use reqwest::Url;
use rover_client::shared::GitContext;
use saucer::Utf8PathBuf;
use semver::Version;
use serde::Serialize;
use sha2::{Digest, Sha256};
use uuid::Uuid;
use wsl::is_wsl;
use std::collections::HashMap;
use std::convert::TryFrom;
use std::env::{self, consts::OS};
use std::fmt::Debug;
use std::time::Duration;
use crate::{Report, SputnikError};
/// The Session represents a usage of the CLI analogous to a web session
/// It contains the "url" (command path + flags) but doesn't contain any
/// values entered by the user. It also contains some identity information
/// for the user
#[derive(Debug, Serialize)]
pub struct Session {
/// the command usage where information about the command is collected
command: Command,
/// Apollo generated machine ID. Stored globally at ~/.apollo/config.toml
machine_id: Uuid,
/// A unique session id
session_id: Uuid,
/// SHA-256 hash of the current working directory
cwd_hash: String,
/// SHA-256 hash of the git remote URL
remote_url_hash: Option<String>,
/// Information about the current architecture/platform
platform: Platform,
/// The current version of the CLI
cli_version: Version,
/// Where the telemetry data is being reported to
#[serde(skip_serializing)]
reporting_info: ReportingInfo,
/// The reqwest Client sputnik uses to send telemetry data
#[serde(skip_serializing)]
client: Client,
}
/// Platform represents the platform the CLI is being run from
#[derive(Debug, Serialize)]
pub struct Platform {
/// the platform from which the command was run (i.e. linux, macOS, windows or even wsl)
os: String,
/// if we think this command is being run in CI
continuous_integration: Option<CiVendor>,
}
/// Command contains information about the command that was run
#[derive(PartialEq, Serialize, Clone, Debug)]
pub struct Command {
/// the name of the command that was run.
pub name: String,
/// the arguments that were run with the command.
pub arguments: HashMap<String, serde_json::Value>,
}
/// ReportingInfo represents information about where the telemetry data
/// should be reported to
#[derive(Debug)]
struct ReportingInfo {
is_telemetry_enabled: bool,
endpoint: Url,
user_agent: String,
}
impl Session {
/// creates a new Session containing info about the current command
/// being executed.
pub fn new<T: Report>(app: &T) -> Result<Session, SputnikError> {
let machine_id = app.machine_id()?;
let command = app.serialize_command()?;
let client = app.client();
let reporting_info = ReportingInfo {
is_telemetry_enabled: app.is_telemetry_enabled()?,
endpoint: app.endpoint()?,
user_agent: app.user_agent(),
};
let current_dir = Utf8PathBuf::try_from(env::current_dir()?)?;
let session_id = Uuid::new_v4();
let cwd_hash = get_cwd_hash(¤t_dir);
let remote_url_hash = get_repo_hash();
let continuous_integration = if ci_info::is_ci() {
ci_info::get().vendor
} else {
None
};
let os = if is_wsl() {
"wsl".to_string()
} else {
OS.to_string()
};
let platform = Platform {
os,
continuous_integration,
};
let cli_version = Version::parse(app.version().as_str())?;
Ok(Session {
command,
machine_id,
session_id,
cwd_hash,
remote_url_hash,
platform,
cli_version,
reporting_info,
client,
})
}
/// sends anonymous usage data to the endpoint defined in ReportingInfo.
pub fn report(&self) -> Result<(), SputnikError> {
if self.reporting_info.is_telemetry_enabled && !cfg!(debug_assertions) {
// set timeout to 400 ms to prevent blocking for too long on reporting
let timeout = Duration::from_millis(4000);
let body = serde_json::to_string(&self)?;
tracing::debug!("POSTing to {}", &self.reporting_info.endpoint);
tracing::debug!("{}", body);
self.client
.post(self.reporting_info.endpoint.clone())
.body(body)
.header("User-Agent", &self.reporting_info.user_agent)
.header("Content-Type", "application/json")
.timeout(timeout)
.send()?;
}
Ok(())
}
}
/// returns sha256 digest of the directory the tool was executed from.
fn get_cwd_hash(current_dir: &Utf8PathBuf) -> String {
format!("{:x}", Sha256::digest(current_dir.as_str().as_bytes()))
}
/// returns sha256 digest of the repository the tool was executed from.
fn get_repo_hash() -> Option<String> {
GitContext::default()
.remote_url
.map(|remote_url| format!("{:x}", Sha256::digest(remote_url.as_bytes())))
}