-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathlib.rs
More file actions
140 lines (128 loc) · 4.21 KB
/
lib.rs
File metadata and controls
140 lines (128 loc) · 4.21 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
//! # Module
//!
//! This is where the main CLI session starts. The module is responsible
//! to parse commands and arguments, redirect the execution of the commands and
//! execute the traversal of directory and files, based on the command that was passed.
use cli_options::CliOptions;
use commands::CommandRunner;
use commands::check::CheckCommandPayload;
use pglt_console::{ColorMode, Console};
use pglt_fs::OsFileSystem;
use pglt_workspace::{App, DynRef, Workspace, WorkspaceRef};
use std::env;
mod changed;
mod cli_options;
mod commands;
mod diagnostics;
mod execute;
mod logging;
mod metrics;
mod panic;
mod reporter;
mod service;
use crate::cli_options::ColorsArg;
pub use crate::commands::{PgltCommand, pglt_command};
pub use crate::logging::{LoggingLevel, setup_cli_subscriber};
pub use diagnostics::CliDiagnostic;
pub use execute::{Execution, TraversalMode, VcsTargeted, execute_mode};
pub use panic::setup_panic_handler;
pub use reporter::{DiagnosticsPayload, Reporter, ReporterVisitor, TraversalSummary};
pub use service::{SocketTransport, open_transport};
pub(crate) const VERSION: &str = match option_env!("PGLT_VERSION") {
Some(version) => version,
None => env!("CARGO_PKG_VERSION"),
};
/// Global context for an execution of the CLI
pub struct CliSession<'app> {
/// Instance of [App] used by this run of the CLI
pub app: App<'app>,
}
impl<'app> CliSession<'app> {
pub fn new(
workspace: &'app dyn Workspace,
console: &'app mut dyn Console,
) -> Result<Self, CliDiagnostic> {
Ok(Self {
app: App::new(
DynRef::Owned(Box::<OsFileSystem>::default()),
console,
WorkspaceRef::Borrowed(workspace),
),
})
}
/// Main function to run the CLI
pub fn run(self, command: PgltCommand) -> Result<(), CliDiagnostic> {
let has_metrics = command.has_metrics();
if has_metrics {
crate::metrics::init_metrics();
}
let result = match command {
PgltCommand::Version(_) => commands::version::full_version(self),
PgltCommand::Check {
cli_options,
configuration,
paths,
stdin_file_path,
staged,
changed,
since,
} => run_command(
self,
&cli_options,
CheckCommandPayload {
configuration,
paths,
stdin_file_path,
staged,
changed,
since,
},
),
PgltCommand::Clean => commands::clean::clean(self),
PgltCommand::Start {
config_path,
log_path,
log_prefix_name,
} => commands::daemon::start(self, config_path, Some(log_path), Some(log_prefix_name)),
PgltCommand::Stop => commands::daemon::stop(self),
PgltCommand::Init => commands::init::init(self),
PgltCommand::LspProxy {
config_path,
log_path,
log_prefix_name,
..
} => commands::daemon::lsp_proxy(config_path, Some(log_path), Some(log_prefix_name)),
PgltCommand::RunServer {
stop_on_disconnect,
config_path,
log_path,
log_prefix_name,
} => commands::daemon::run_server(
stop_on_disconnect,
config_path,
Some(log_path),
Some(log_prefix_name),
),
PgltCommand::PrintSocket => commands::daemon::print_socket(),
};
if has_metrics {
metrics::print_metrics();
}
result
}
}
pub fn to_color_mode(color: Option<&ColorsArg>) -> ColorMode {
match color {
Some(ColorsArg::Off) => ColorMode::Disabled,
Some(ColorsArg::Force) => ColorMode::Enabled,
None => ColorMode::Auto,
}
}
pub(crate) fn run_command(
session: CliSession,
cli_options: &CliOptions,
mut command: impl CommandRunner,
) -> Result<(), CliDiagnostic> {
let command = &mut command;
command.run(session, cli_options)
}