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
// Copyright 2022 The Tari Project
// SPDX-License-Identifier: BSD-3-Clause
use std::{io, time::Duration};
use crossterm::{
cursor,
event::{Event, EventStream, KeyCode, KeyEvent, KeyModifiers},
terminal,
};
use futures::{FutureExt, StreamExt};
use rustyline::{CompletionType, Config, EditMode, Editor, config::OutputStreamType, error::ReadlineError};
use tari_shutdown::ShutdownSignal;
use tokio::{signal, time};
use crate::{
LOG_TARGET,
commands::{
cli,
command::{Args, CommandContext, WatchCommand},
parser::Parser,
reader::CommandReader,
},
};
pub struct CliLoop {
context: CommandContext,
reader: CommandReader,
commands: Vec<String>,
watch_task: Option<WatchCommand>,
non_interactive: bool,
first_signal: bool,
done: bool,
shutdown_signal: ShutdownSignal,
}
impl CliLoop {
pub fn new(context: CommandContext, watch_command: Option<String>, non_interactive: bool) -> Self {
let parser = Parser::new();
let commands = parser.get_commands();
let cli_config = Config::builder()
.history_ignore_space(true)
.completion_type(CompletionType::List)
.edit_mode(EditMode::Emacs)
.output_stream(OutputStreamType::Stdout)
.auto_add_history(true)
.build();
let mut rustyline = Editor::with_config(cli_config);
rustyline.set_helper(Some(parser));
// Saves the user from having to type this in again to return to "watch status"
rustyline.history_mut().add("watch status");
let reader = CommandReader::new(rustyline);
let watch_task = {
if let Some(line) = watch_command {
WatchCommand::new(line)
} else if non_interactive {
WatchCommand::new("status --output log")
} else {
WatchCommand::new("status")
}
};
let shutdown_signal = context.shutdown.to_signal();
Self {
context,
reader,
commands,
watch_task: Some(watch_task),
non_interactive,
first_signal: false,
done: false,
shutdown_signal,
}
}
/// Runs the Base Node CLI loop
/// ## Parameters
/// `parser` - The parser to process input commands
/// `shutdown` - The trigger for shutting down
///
/// ## Returns
/// Doesn't return anything
pub async fn cli_loop(mut self, disable_splash_screen: bool) {
if !disable_splash_screen {
cli::print_banner(self.commands.clone(), 3);
}
if self.non_interactive {
self.watch_loop_non_interactive().await;
} else {
while !self.done {
self.watch_loop().await;
if !self.done {
self.execute_command().await;
}
}
}
}
fn is_interrupted(&self, event: Option<Result<Event, io::Error>>) -> bool {
if let Some(Ok(Event::Key(key))) = event {
match key {
KeyEvent {
code: KeyCode::Char('c'),
modifiers: KeyModifiers::CONTROL,
kind: _,
state: _,
} => {
return true;
},
_ => {
if self.non_interactive {
println!("Press Ctrl-C to interrupt the node.");
} else {
println!("Press Ctrl-C to enter the interactive shell.");
}
},
}
}
false
}
async fn watch_loop(&mut self) {
if let Some(command) = self.watch_task.take() {
let mut interrupt = signal::ctrl_c().fuse().boxed();
let mut software_update_notif = self.context.software_updater.update_notifier().clone();
let config = self.context.config.clone();
let line = command.line();
let interval = command
.interval
.map(Duration::from_secs)
.unwrap_or(config.base_node.status_line_interval);
if let Err(err) = self.context.handle_command_str(line).await {
println!("Wrong command to watch `{line}`. Failed with: {err}");
} else {
let mut events = EventStream::new();
while !self.done {
let interval = time::sleep(interval);
tokio::select! {
_ = interval => {
if let Err(err) = self.context.handle_command_str(line).await {
println!("Watched command `{line}` failed: {err}");
} else {
let args: Result<Args, _> = line.parse();
if let Ok(command) = args
&& command.is_quit()
{
self.done = true;
}
}
continue;
},
_ = &mut interrupt => {
break;
}
event = events.next() => {
if self.is_interrupted(event) {
break;
}
}
Ok(_) = software_update_notif.changed() => {
if let Some(ref update) = *software_update_notif.borrow() {
println!(
"Version {} of the {} is available: {} (sha: {})",
update.version(),
update.app(),
update.download_url(),
update.to_hash_hex()
);
}
}
}
if !self.done {
crossterm::execute!(io::stdout(), cursor::MoveToNextLine(1)).ok();
}
}
terminal::disable_raw_mode().ok();
}
}
}
async fn watch_loop_non_interactive(&mut self) {
if let Some(command) = self.watch_task.take() {
let mut interrupt = signal::ctrl_c().fuse().boxed();
let config = &self.context.config;
let line = command.line();
let interval = command
.interval
.map(Duration::from_secs)
.unwrap_or(config.base_node.status_line_interval);
if let Err(err) = self.context.handle_command_str(line).await {
println!("Wrong command to watch `{line}`. Failed with: {err}");
} else {
while !self.done {
let interval = time::sleep(interval);
tokio::select! {
_ = interval => {
if let Err(err) = self.context.handle_command_str(line).await {
println!("Watched command `{line}` failed: {err}");
} else {
let args: Result<Args, _> = line.parse();
if let Ok(command) = args
&& command.is_quit()
{
self.done = true;
}
}
continue;
},
_ = &mut interrupt => {
break;
},
_ = self.shutdown_signal.wait() => {
self.done = true;
}
}
}
}
}
}
async fn handle_line(&mut self, line: String) {
// Reset the interruption flag if the command entered.
self.first_signal = false;
if !line.is_empty() {
// Handle the built-in "help" command before passing to the command parser.
if line.trim() == "help" {
println!("Available commands:");
for cmd in &self.commands {
println!(" {cmd}");
}
return;
}
match self.context.handle_command_str(&line).await {
Err(err) => {
// Check if the entered command is unrecognized (does not match any known command).
let cmd_name = line.split_whitespace().next().unwrap_or("");
if !cmd_name.is_empty() && !self.commands.iter().any(|c| c.as_str() == cmd_name) {
println!(
"Unknown command '{}'. Use Tab for auto-completion or type 'help' to list all commands.",
cmd_name
);
} else {
println!("Command `{line}` failed: {err}");
}
},
Ok(command) => {
self.watch_task = command;
},
}
}
}
async fn execute_command(&mut self) {
tokio::select! {
res = self.reader.next_command() => {
if let Some(event) = res {
match event {
Ok(line) => {
self.handle_line(line).await;
}
Err(ReadlineError::Interrupted) => {
// If `Ctrl-C` is pressed
if self.first_signal {
self.done = true;
} else {
println!("Are you leaving already? Press Ctrl-C again (or Ctrl-D) to terminate the node.");
self.first_signal = true;
}
}
Err(ReadlineError::Eof) => {
// If `Ctrl-D` is pressed
self.done = true;
}
Err(err) => {
log::debug!(target: LOG_TARGET, "Could not read line from rustyline:{err}");
self.done = true;
}
}
} else {
self.done = true;
}
if self.done && !self.shutdown_signal.is_triggered() {
self.context.shutdown.trigger();
}
},
_ = self.shutdown_signal.wait() => {
self.done = true;
}
}
}
}