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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
use color_eyre::{
Report,
eyre::{Context, OptionExt, eyre},
};
use futures_util::{FutureExt, StreamExt, TryStreamExt, stream};
use git2::{
FetchOptions, ProxyOptions, RemoteCallbacks, Repository,
build::{CheckoutBuilder, RepoBuilder},
};
use tokio::{fs::File, sync::mpsc};
use tokio_util::sync::CancellationToken;
use tracing::instrument;
use walkdir::WalkDir;
use super::{IntelliShellService, import::parse_import_items};
use crate::{
errors::{Result, UserFacingError},
model::{ImportStats, SOURCE_TLDR, TldrConnectionMode},
};
/// Progress events for the `tldr fetch` operation
#[derive(Debug)]
pub enum TldrFetchProgress {
/// Indicates the status of the tldr git repository
Repository(RepoStatus),
/// Indicates that the tldr command files are being located
LocatingFiles,
/// Indicates that the tldr command files have been located
FilesLocated(u64),
/// Indicates the start of the file processing stage
ProcessingStart(u64),
/// Indicates that a single file is being processed
ProcessingFile(String),
/// Indicates that a single file has been processed
FileProcessed(String),
}
/// The status of the tldr git repository
#[derive(Debug)]
pub enum RepoStatus {
/// Cloning the repository for the first time
Cloning,
/// The repository has been successfully cloned
DoneCloning,
/// Fetching latest changes
Fetching,
/// The repository is already up-to-date
UpToDate,
/// Updating the local repository
Updating,
/// The repository has been successfully updated
DoneUpdating,
}
impl IntelliShellService {
/// Removes tldr commands matching the given criteria.
///
/// Returns the number of commands removed
#[instrument(skip_all)]
pub async fn clear_tldr_commands(&self, category: Option<String>) -> Result<u64> {
self.storage.delete_tldr_commands(category).await
}
/// Fetches and imports tldr commands matching the given criteria
#[instrument(skip_all)]
pub async fn fetch_tldr_commands(
&self,
category: Option<String>,
connection_mode: TldrConnectionMode,
commands: Vec<String>,
progress: mpsc::Sender<TldrFetchProgress>,
cancellation_token: CancellationToken,
) -> Result<ImportStats> {
// Check for cancellation at the beginning
if cancellation_token.is_cancelled() {
tracing::info!("TLDR fetch cancelled before starting");
return Err(UserFacingError::Cancelled.into());
}
// Setup repository
self.setup_tldr_repo(connection_mode, progress.clone(), cancellation_token.clone())
.await?;
// Determine which categories to import
let categories = if let Some(cat) = category {
vec![cat]
} else {
vec![
"common".to_owned(),
#[cfg(target_os = "windows")]
"windows".to_owned(),
#[cfg(target_os = "android")]
"android".to_owned(),
#[cfg(target_os = "macos")]
"osx".to_owned(),
#[cfg(target_os = "freebsd")]
"freebsd".to_owned(),
#[cfg(target_os = "openbsd")]
"openbsd".to_owned(),
#[cfg(target_os = "netbsd")]
"netbsd".to_owned(),
#[cfg(any(
target_os = "linux",
target_os = "freebsd",
target_os = "openbsd",
target_os = "netbsd",
target_os = "dragonfly",
))]
"linux".to_owned(),
]
};
// Construct the path to the tldr pages directory
let pages_path = self.tldr_repo_path.join("pages");
tracing::info!("Locating files for categories: {}", categories.join(", "));
progress.send(TldrFetchProgress::LocatingFiles).await.ok();
// Iterate over directory entries
let mut command_files = Vec::new();
let mut iter = WalkDir::new(&pages_path).max_depth(2).into_iter();
while let Some(result) = iter.next() {
// Check for cancellation within the file discovery loop
if cancellation_token.is_cancelled() {
tracing::info!("TLDR fetch cancelled during file discovery");
return Err(UserFacingError::Cancelled.into());
}
let entry = result.wrap_err("Couldn't read tldr repository files")?;
let path = entry.path();
// Skip base path
if path == pages_path {
continue;
}
// Skip non-included categories
let file_name = entry.file_name().to_str().ok_or_eyre("Non valid file name")?;
if entry.file_type().is_dir() {
if !categories.iter().any(|c| c == file_name) {
tracing::trace!("Skipped directory: {file_name}");
iter.skip_current_dir();
continue;
} else {
// The directory entry itself must be skipped as well, we only care about files
continue;
}
}
// We only care about markdown files
let Some(file_name_no_ext) = file_name.strip_suffix(".md") else {
tracing::warn!("Unexpected file found: {}", path.display());
continue;
};
// Skip non-included commands
if !commands.is_empty() {
if !commands.iter().any(|c| c == file_name_no_ext) {
continue;
} else {
tracing::trace!("Included command: {file_name_no_ext}");
}
}
// Retrieve the category
let category = path
.parent()
.and_then(|p| p.file_name())
.and_then(|p| p.to_str())
.ok_or_eyre("Couldn't read tldr category")?
.to_owned();
// Include the command file
command_files.push((path.to_path_buf(), category, file_name_no_ext.to_owned()));
}
progress
.send(TldrFetchProgress::FilesLocated(command_files.len() as u64))
.await
.ok();
tracing::info!("Found {} files to be processed", command_files.len());
progress
.send(TldrFetchProgress::ProcessingStart(command_files.len() as u64))
.await
.ok();
// Create a stream that reads and parses each command file concurrently
let items_stream = stream::iter(command_files)
.map(move |(path, category, command)| {
let progress = progress.clone();
async move {
progress
.send(TldrFetchProgress::ProcessingFile(command.clone()))
.await
.ok();
// Open and parse the file
let file = File::open(&path)
.await
.wrap_err_with(|| format!("Failed to open tldr file: {}", path.display()))?;
let stream = parse_import_items(file, vec![], category, SOURCE_TLDR);
progress.send(TldrFetchProgress::FileProcessed(command)).await.ok();
Ok::<_, Report>(stream)
}
})
.buffered(5)
.try_flatten();
// Import items while the token is not cancelled
let stats = self
.storage
.import_items(
items_stream.take_until(cancellation_token.clone().cancelled_owned().fuse()),
true,
false,
)
.await?;
// After processing, check if cancellation was the reason the stream ended
if cancellation_token.is_cancelled() {
tracing::info!("TLDR fetch cancelled during command processing");
return Err(UserFacingError::Cancelled.into());
}
Ok(stats)
}
#[instrument(skip_all)]
async fn setup_tldr_repo(
&self,
connection_mode: TldrConnectionMode,
progress: mpsc::Sender<TldrFetchProgress>,
cancellation_token: CancellationToken,
) -> Result<bool> {
const BRANCH: &str = "main";
const HTTPS_URL: &str = "https://github.com/tldr-pages/tldr.git";
const SSH_URL: &str = "git@github.com:tldr-pages/tldr.git";
let tldr_repo_path = self.tldr_repo_path.clone();
tokio::task::spawn_blocking(move || {
// Helper to send progress to the channel
let send_progress = |status| {
// Use blocking_send as we are in a sync context
progress.blocking_send(TldrFetchProgress::Repository(status)).ok();
};
// Open the repository upfront when it already exists, so the connection mode can be resolved from it
let repo = if tldr_repo_path.exists() {
Some(Repository::open(&tldr_repo_path).wrap_err("Failed to open existing tldr repository")?)
} else {
None
};
// Resolve the effective repository URL from the requested connection mode:
// - `Https`/`Ssh` force the matching transport
// - `Auto` reuses the transport of an existing clone (this also honors git `insteadOf` rewrites), falling
// back to HTTPS for a fresh clone
let repo_url = match connection_mode {
TldrConnectionMode::Https => HTTPS_URL.to_owned(),
TldrConnectionMode::Ssh => SSH_URL.to_owned(),
TldrConnectionMode::Auto => repo
.as_ref()
.and_then(|repo| repo.find_remote("origin").ok())
.and_then(|remote| remote.url().map(ToOwned::to_owned))
.unwrap_or_else(|| HTTPS_URL.to_owned()),
};
// Setup git callbacks to enable cancellation
let mut callbacks = RemoteCallbacks::new();
callbacks.transfer_progress(move |_| !cancellation_token.is_cancelled());
// Always register the SSH credentials callback: libgit2 only invokes it when the resolved transport
// is SSH, which can happen via an explicit `ssh` mode, a reused SSH remote, or a git `insteadOf`
// rewrite of an HTTPS URL (so sniffing the configured scheme isn't reliable). SSH auth is negotiated
// in steps: libgit2 first asks for the username (when it isn't part of the URL), then for the
// credentials, so the returned credential type must match the requested `allowed_types`, otherwise
// git2 passes through and libgit2 reports it as a missing callback. The agent is only offered once to
// avoid looping forever when authentication fails.
let mut agent_offered = false;
callbacks.credentials(move |_url, username_from_url, allowed_types| {
let username = username_from_url.unwrap_or("git");
if allowed_types.contains(git2::CredentialType::USERNAME) {
git2::Cred::username(username)
} else if allowed_types.contains(git2::CredentialType::SSH_KEY) && !agent_offered {
agent_offered = true;
git2::Cred::ssh_key_from_agent(username)
} else {
Err(git2::Error::from_str(
"SSH authentication failed: no usable identity found in the SSH agent",
))
}
});
// Setup git fetch options for a swallow copy with auto proxy config
let mut proxy_opts = ProxyOptions::new();
proxy_opts.auto();
let mut fetch_options = FetchOptions::new();
fetch_options.proxy_options(proxy_opts);
fetch_options.remote_callbacks(callbacks);
fetch_options.depth(1);
// Fetch latest repo changes or clone it if it doesn't exist yet
if let Some(repo) = repo {
tracing::info!("Fetching latest tldr changes ...");
send_progress(RepoStatus::Fetching);
// Update 'origin' URL if it doesn't match `repo_url` (no-op in `Auto` mode, which already
// derives the URL from the existing remote)
let current_url = repo.find_remote("origin")?.url().map(|s| s.to_owned());
if current_url.as_deref() != Some(repo_url.as_str()) {
repo.remote_set_url("origin", &repo_url)
.wrap_err("Failed to update remote URL")?;
}
// Get the 'origin' remote
let mut remote = repo.find_remote("origin")?;
// Fetch the latest changes from the remote 'main' branch
let refspec = format!("refs/heads/{BRANCH}:refs/remotes/origin/{BRANCH}");
if let Err(err) = remote.fetch(&[refspec], Some(&mut fetch_options), None) {
// Check if the error was a user-initiated cancellation
if err.code() == git2::ErrorCode::User && err.class() == git2::ErrorClass::Callback {
return Err(UserFacingError::Cancelled.into());
}
return Err(Report::from(err).wrap_err("Failed to fetch from tldr remote").into());
}
// Get the commit OID from the fetched data (FETCH_HEAD)
let fetch_head = repo.find_reference("FETCH_HEAD")?;
let fetch_commit_oid = fetch_head
.target()
.ok_or_else(|| eyre!("FETCH_HEAD is not a direct reference"))?;
// Get the OID of the current commit on the local branch
let local_ref_name = format!("refs/heads/{BRANCH}");
let local_commit_oid = repo.find_reference(&local_ref_name)?.target();
// If the commit OIDs are the same, the repo is already up-to-date
if Some(fetch_commit_oid) == local_commit_oid {
tracing::info!("Repository is already up-to-date");
send_progress(RepoStatus::UpToDate);
return Ok(false);
}
tracing::info!("Updating to the latest version ...");
send_progress(RepoStatus::Updating);
// Find the local branch reference
let mut local_ref = repo.find_reference(&local_ref_name)?;
// Update the local branch to point directly to the newly fetched commit
let msg = format!("Resetting to latest commit {fetch_commit_oid}");
local_ref.set_target(fetch_commit_oid, &msg)?;
// Point HEAD to the updated local branch
repo.set_head(&local_ref_name)?;
// Checkout the new HEAD to update the files in the working directory
let mut checkout_builder = CheckoutBuilder::new();
checkout_builder.force();
repo.checkout_head(Some(&mut checkout_builder))?;
tracing::info!("Repository successfully updated");
send_progress(RepoStatus::DoneUpdating);
Ok(true)
} else {
tracing::info!("Performing a shallow clone of '{repo_url}' ...");
send_progress(RepoStatus::Cloning);
// Clone the repository
if let Err(err) = RepoBuilder::new()
.branch(BRANCH)
.fetch_options(fetch_options)
.clone(&repo_url, &tldr_repo_path)
{
if err.code() == git2::ErrorCode::User && err.class() == git2::ErrorClass::Callback {
return Err(UserFacingError::Cancelled.into());
}
return Err(Report::from(err).wrap_err("Failed to clone tldr repository").into());
}
tracing::info!("Repository successfully cloned");
send_progress(RepoStatus::DoneCloning);
Ok(true)
}
})
.await
.wrap_err("tldr repository task failed")?
}
}