forked from EpicGames/lore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.rs
More file actions
400 lines (332 loc) · 13.5 KB
/
Copy pathcli.rs
File metadata and controls
400 lines (332 loc) · 13.5 KB
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
390
391
392
393
394
395
396
397
398
399
400
// SPDX-FileCopyrightText: 2026 Epic Games, Inc.
// SPDX-License-Identifier: MIT
use clap::Parser;
use clap::Subcommand;
use lore::LORE_LIBRARY_VERSION;
use lore::interface::LoreEvent;
use lore::interface::LoreEventCallback;
use lore::interface::LoreGlobalArgs;
use serde::Deserialize;
use serde::Serialize;
use thiserror::Error;
use crate::commands::*;
use crate::config::config;
use crate::eprintln;
use crate::println;
use crate::styling::cli_styles;
use crate::util::get_repository_path;
#[derive(Parser)]
#[command(name = "lore", styles = cli_styles())]
#[clap(about, long_about = None)]
#[clap(version = LORE_LIBRARY_VERSION.as_str())]
pub struct LoreCli {
#[command(subcommand)]
pub command: Option<LoreCommands>,
/// Use given path as repository path
#[clap(global = true, long, value_name = "path")]
pub repository: Option<String>,
/// Set the logging level
#[clap(global = true, long = "log-level", value_name = "level", conflicts_with_all = ["debug", "silent"])]
pub level: Option<String>,
/// Enable debug output
#[clap(global = true, long, short, action, conflicts_with_all = ["level", "silent"])]
pub debug: bool,
/// Suppress all output
#[clap(global = true, hide = true, long, short, action, conflicts_with_all = ["level", "debug"])]
pub silent: bool,
/// Time execution of command
#[clap(global = true, hide = true, long, short, action)]
pub time: bool,
/// Force the operation if possible
#[clap(global = true, long, short, action)]
pub force: bool,
/// Dry run mode, only report what would have been changed and perform no changes to local file system
#[clap(global = true, long, action)]
pub dry_run: bool,
/// Enable machine-readable json output
#[clap(global = true, hide = true, long, short, action)]
pub json: bool,
/// Disable pagination
#[clap(global = true, long, short = 'P', action)]
pub no_pager: bool,
/// Force offline mode
#[clap(global = true, long, action)]
pub offline: bool,
/// Use remote data
#[clap(global = true, long, action)]
pub remote: bool,
/// Use local data
#[clap(global = true, long, action, conflicts_with = "remote")]
pub local: bool,
/// Use given identity
#[clap(global = true, long, action)]
pub identity: Option<String>,
/// Avoid using compression
#[clap(global = true, hide = true, long, action)]
pub nocompress: bool,
/// Set maximum number of parallel connections
#[clap(global = true, long)]
pub max_connections: Option<u32>,
/// Set maximum number of parallel files opened
#[clap(global = true, long, value_name = "count")]
pub file_count_limit: Option<usize>,
/// Set maximum total size in bytes of parallel files opened
#[clap(global = true, long, value_name = "size")]
pub file_size_limit: Option<usize>,
/// Set maximum number of parallel compress operations
#[clap(global = true, long, value_name = "count")]
pub compress_limit: Option<usize>,
/// Set maximum total number of threads Lore sizes its pools for
#[clap(global = true, hide = true, long, value_name = "count")]
pub max_threads: Option<usize>,
#[arg(long, hide = true)]
pub markdown_help: bool,
/// Set maximum number of revisions to search when matching or finding revisions
#[clap(global = true, long)]
search_limit: Option<usize>,
/// Set to search for nearest match when matching revisions
#[clap(global = true, long, action)]
search_nearest: bool,
/// Set to run automatic garbage collection on local store in background
#[clap(global = true, long, action)]
gc: bool,
/// Force sync data to storage media during flush
#[clap(global = true, long, action)]
sync_data: bool,
/// Cache fragment payloads fetched from remote in the local store
#[clap(global = true, long, action)]
pub cache: bool,
/// Disable interactive prompts (e.g., per-link commit messages)
#[clap(global = true, long, action)]
pub non_interactive: bool,
}
pub type EventCallbackFn = Box<dyn Fn(&LoreEvent) + Send + Sync>;
pub trait EventCallbackExt {
/// Wraps the callback with default handling for `LoreEvent::Log`.
/// `LoreEvent::Error` is swallowed — `Dispatcher::send_error`
/// already routes errors through `LoreEvent::Log(level=Error)`.
fn with_defaults(self) -> Self;
}
impl EventCallbackExt for EventCallbackFn {
fn with_defaults(self) -> Self {
Box::new(move |event: &LoreEvent| match event {
LoreEvent::Error(_) => {}
LoreEvent::Log(data) => {
crate::logging::handle_log_event(data);
}
other => self(other),
})
}
}
pub fn output_formatter() -> Option<LoreEventCallback> {
if config().json {
Some(Some(Box::new(move |event: &LoreEvent| {
// Filter log events since we receive all log events for log file,
// but output should adhere to the configured log level limit
if let LoreEvent::Log(data) = event
&& data.level < config().log_level
{
return;
}
// Ignore the end of events event
if let LoreEvent::End(_) = event {
return;
}
#[derive(Serialize, Deserialize)]
struct JsonError {
error: String,
}
match serde_json::to_string(event) {
Ok(string) => println!("{string}"),
Err(err) => {
eprintln!(
"{}",
serde_json::to_string(&JsonError {
error: err.to_string()
})
.unwrap()
);
}
}
}) as Box<_>))
} else {
None
}
}
#[derive(Subcommand)]
pub enum LoreCommands {
/// Repository commands
Repository(repository::RepositoryArgs),
/// Branch commands
Branch(branch::BranchArgs),
/// Revision commands
Revision(revision::RevisionArgs),
/// File commands
File(file::FileArgs),
/// Authentication commands
Auth(auth::AuthArgs),
/// Layer commands
Layer(layer::LayerArgs),
/// Logfile commands
Logfile(logfile::LogfileArgs),
/// Authenticate the CLI
Login(auth::AuthLoginArgs),
/// Link commands
Link(link::LinkArgs),
// Config commands
/// Show current repository status.
///
/// Reports the staged revision (if any) plus the files and
/// directories currently marked dirty. By default no filesystem
/// walk is performed — only the tracked dirty flags are read, so
/// changes made without prior `lore dirty` or `--scan` will not
/// appear.
///
/// Pass `--scan` to walk the filesystem under the given paths,
/// reconcile every file against the current revision, and refresh
/// dirty flags (setting them on detected modifications/adds/deletes
/// and clearing stale ones). The refreshed flags are persisted so
/// subsequent `lore stage` / `lore status` calls see an accurate
/// picture without rescanning.
Status(repository::RepositoryStatusArgs),
/// Clone a remote repository into the given path
Clone(repository::RepositoryCloneArgs),
/// Stage changes for commit.
///
/// Directory path (including `.`): stages only files already marked
/// dirty under that directory. No filesystem walk is performed;
/// clean or unmarked files are skipped. Mark files first with
/// `lore dirty` (or `lore status --scan` to reconcile in bulk), or
/// pass `--scan` here to discover and stage in one pass.
///
/// Specific file path: checked against the filesystem and staged
/// if its on-disk content differs from the current revision,
/// regardless of its dirty flag.
///
/// `--scan`: forces a filesystem walk under the given paths, marks
/// modified, added, and deleted files dirty, and stages them in
/// one step. Use this when changes were made externally without
/// going through `lore dirty`, or to recover after losing track of
/// dirty state.
Stage(file::FileStageArgs),
/// Mark files as dirty so they show up in `lore status` and get
/// picked up by `lore stage` (no content is read or staged).
///
/// Use this when your editor or build tool has modified files and
/// you want to inform Lore of the change without performing a full
/// `--scan`. For bulk reconciliation across a tree, prefer
/// `lore status --scan` or `lore stage --scan`.
Dirty(file::FileDirtyArgs),
/// Unstage changes to a file or directory
Unstage(file::FileUnstageArgs),
/// Reset changes to a file or directory
Reset(file::FileResetArgs),
/// Show differences between two revisions of a file
Diff(file::FileDiffArgs),
/// List revisions of a repository
History(revision::RevisionHistoryArgs),
/// Commit the staged revision
Commit(revision::RevisionCommitArgs),
/// Synchronize to a repository state
#[clap(visible_alias("synchronize"))]
Sync(revision::RevisionSyncArgs),
/// Push commits to remote
Push(branch::BranchPushArgs),
/// Lock file
Lock(lock::LockFileArgs),
/// Manage the repository in a service process
Service(service::ServiceArgs),
/// Notifications
Notification(notification::NotificationArgs),
/// Generate terminal autocompletions
Completions(completions::CompletionsArgs),
/// Manage the shared store
SharedStore(shared_store::SharedStoreArgs),
}
pub fn handle_lore_commands(cmd: &LoreCommands, globals: LoreGlobalArgs) -> u8 {
match cmd {
LoreCommands::Repository(sub_cmd) => {
repository::handle_repository_commands(&sub_cmd.command, globals)
}
LoreCommands::Status(args) => repository::handle_repository_status(globals, args),
LoreCommands::Clone(args) => repository::handle_repository_clone(globals, args),
LoreCommands::Branch(sub_cmd) => branch::handle_branch_commands(&sub_cmd.command, globals),
LoreCommands::Revision(sub_cmd) => {
revision::handle_revision_commands(&sub_cmd.command, globals)
}
LoreCommands::File(sub_cmd) => file::handle_file_commands(&sub_cmd.command, globals),
LoreCommands::Stage(args) => file::handle_file_stage(globals, args),
LoreCommands::Dirty(args) => file::handle_file_dirty(globals, args),
LoreCommands::Unstage(args) => file::handle_file_unstage(globals, args),
LoreCommands::Reset(args) => file::handle_file_reset(globals, args),
LoreCommands::Diff(args) => file::handle_file_diff(globals, args),
LoreCommands::History(args) => revision::handle_revision_history(globals, args),
LoreCommands::Commit(args) => revision::handle_revision_commit(globals, args),
LoreCommands::Sync(args) => revision::handle_revision_sync(globals, args),
LoreCommands::Push(args) => branch::handle_branch_push(globals, args),
LoreCommands::Auth(sub_cmd) => auth::handle_auth_commands(&sub_cmd.command, globals),
LoreCommands::Layer(sub_cmd) => layer::handle_layer_commands(&sub_cmd.command, globals),
LoreCommands::Login(args) => auth::handle_login_command(globals, args),
LoreCommands::Logfile(sub_cmd) => logfile::handle_logfile_commands(&sub_cmd.command),
LoreCommands::Lock(sub_cmd) => lock::handle_lock_file_commands(globals, &sub_cmd.command),
LoreCommands::Link(sub_cmd) => link::handle_link_commands(&sub_cmd.command, globals),
LoreCommands::Service(sub_cmd) => {
service::handle_service_commands(&sub_cmd.command, globals)
}
LoreCommands::Notification(sub_cmd) => {
notification::handle_notification_commands(&sub_cmd.command, globals)
}
LoreCommands::Completions(sub_cmd) => completions::handle_completions_commands(sub_cmd),
LoreCommands::SharedStore(sub_cmd) => {
shared_store::handle_store_commands(&sub_cmd.command, globals)
}
}
}
#[derive(Error, Debug, PartialEq, Eq)]
pub enum LoreCliError {
#[error("Log level '{0}' is not valid. Choose one of [trace, debug, info, warn, error].")]
ParseLogLevel(String),
}
pub fn lore_globals_from_args(cli: &LoreCli) -> LoreGlobalArgs {
let mut args = LoreGlobalArgs {
repository_path: get_repository_path(cli.repository.clone()),
force: cli.force.into(),
dry_run: cli.dry_run.into(),
offline: cli.offline.into(),
remote: cli.remote.into(),
local: cli.local.into(),
gc: cli.gc.into(),
sync_data: cli.sync_data.into(),
cache: cli.cache.into(),
identity: cli.identity.clone().into(),
search_limit: cli.search_limit.unwrap_or_default() as u32,
search_nearest: cli.search_nearest.into(),
..Default::default()
};
if let Some(limit) = cli.file_count_limit
&& limit > 0
{
args.file_count_limit = limit as u64;
}
if let Some(limit) = cli.file_size_limit
&& limit > 0
{
args.file_size_limit = limit as u64;
}
if let Some(limit) = cli.compress_limit
&& limit > 0
{
args.compress_task_limit = limit as u64;
}
args.max_connections = if let Some(max_connections) = cli.max_connections {
max_connections
} else if let Ok(max_connections) = std::env::var("LORE_MAX_CONNECTIONS")
&& let Ok(max_connections) = str::parse(max_connections.as_str())
{
max_connections
} else {
0
};
args
}
// TODO(vri): Add command shortcuts