Compare commits
9 commits
6e3a0a582f
...
049b036e65
| Author | SHA1 | Date | |
|---|---|---|---|
| 049b036e65 | |||
| 135ef44feb | |||
| 33774da89c | |||
| d906232bf9 | |||
| 6878e539c8 | |||
| 44543e0f2e | |||
| 259ed9886d | |||
| aba28dc475 | |||
| 105ed229c8 |
14 changed files with 2685 additions and 765 deletions
5
.gitignore
vendored
5
.gitignore
vendored
|
|
@ -1,3 +1,4 @@
|
|||
/target
|
||||
/*/target
|
||||
.data/*.log
|
||||
/*/target
|
||||
/pw
|
||||
/target
|
||||
|
|
|
|||
2875
Cargo.lock
generated
2875
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -45,6 +45,12 @@
|
|||
"<Ctrl-c>": "Quit",
|
||||
"<Ctrl-z>": "Suspend"
|
||||
},
|
||||
"SelectTicket": {
|
||||
"<q>": "Quit",
|
||||
"<Ctrl-d>": "Quit",
|
||||
"<Ctrl-c>": "Quit",
|
||||
"<Ctrl-z>": "Suspend"
|
||||
},
|
||||
"Confirm": {
|
||||
"<b>": "PrevScreen",
|
||||
"<Enter>": "Process",
|
||||
|
|
@ -227,5 +233,6 @@
|
|||
"network_server": "SERVER",
|
||||
"network_share": "SHARE",
|
||||
"network_user": "USER",
|
||||
"network_pass": "PASS"
|
||||
"network_pass": "PASS",
|
||||
"ost_server": "ost.1201.com"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -48,6 +48,7 @@ regex = "1.11.1"
|
|||
serde = { version = "1.0.217", features = ["derive"] }
|
||||
serde_json = "1.0.125"
|
||||
signal-hook = "0.3.17"
|
||||
sqlx = { version = "0.8", features = ["mysql", "runtime-tokio"] }
|
||||
strip-ansi-escapes = "0.2.0"
|
||||
strum = { version = "0.26.3", features = ["derive"] }
|
||||
tempfile = "3.13.0"
|
||||
|
|
@ -56,8 +57,12 @@ tokio-util = "0.7.11"
|
|||
tracing = "0.1.41"
|
||||
tracing-error = "0.2.0"
|
||||
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "serde"] }
|
||||
tui-input = "*"
|
||||
walkdir = "2.5.0"
|
||||
|
||||
[dependencies.ost-db-connector]
|
||||
git = "ssh://git@git.1201.com:2222/1201/ost-db-connector.git"
|
||||
|
||||
[build-dependencies]
|
||||
anyhow = "1.0.86"
|
||||
vergen-gix = { version = "1.0.0", features = ["build", "cargo"] }
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@
|
|||
// You should have received a copy of the GNU General Public License
|
||||
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||
//
|
||||
use ost_db_connector::ResponseColor;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use strum::Display;
|
||||
|
||||
|
|
@ -59,6 +60,12 @@ pub enum Action {
|
|||
FindWimBackups,
|
||||
FindWimNetwork,
|
||||
SetUserName(String),
|
||||
// osTicket
|
||||
PostResponse { color: ResponseColor, text: String },
|
||||
ResetTicket,
|
||||
SelectTicket(u16),
|
||||
UserNo,
|
||||
UserYes,
|
||||
// Screens
|
||||
DismissPopup,
|
||||
DisplayPopup(PopupType, String),
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ pub mod footer;
|
|||
pub mod left;
|
||||
pub mod popup;
|
||||
pub mod right;
|
||||
pub mod select_ticket;
|
||||
pub mod state;
|
||||
pub mod title;
|
||||
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@
|
|||
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||
//
|
||||
use color_eyre::Result;
|
||||
use crossterm::event::KeyCode;
|
||||
use rand::random;
|
||||
use ratatui::{
|
||||
prelude::*,
|
||||
|
|
@ -25,13 +26,14 @@ use tokio::sync::mpsc::UnboundedSender;
|
|||
use tracing::info;
|
||||
|
||||
use super::Component;
|
||||
use crate::{action::Action, config::Config};
|
||||
use crate::{action::Action, config::Config, tui::Event};
|
||||
|
||||
#[derive(Default, Debug, Clone, PartialEq, Eq, Display, Serialize, Deserialize)]
|
||||
pub enum Type {
|
||||
#[default]
|
||||
Info,
|
||||
Error,
|
||||
Question,
|
||||
Success,
|
||||
}
|
||||
|
||||
|
|
@ -39,6 +41,7 @@ pub enum Type {
|
|||
pub struct Popup {
|
||||
command_tx: Option<UnboundedSender<Action>>,
|
||||
config: Config,
|
||||
get_input: bool,
|
||||
popup_type: Type,
|
||||
popup_text: String,
|
||||
}
|
||||
|
|
@ -46,7 +49,10 @@ pub struct Popup {
|
|||
impl Popup {
|
||||
#[must_use]
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
Self {
|
||||
get_input: false,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -61,11 +67,34 @@ impl Component for Popup {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_events(&mut self, event: Option<Event>) -> Result<Option<Action>> {
|
||||
if !self.get_input {
|
||||
return Ok(None);
|
||||
}
|
||||
let action = match event {
|
||||
Some(Event::Key(key_event)) => match key_event.code {
|
||||
KeyCode::Char(c) => {
|
||||
let response = c.to_ascii_lowercase();
|
||||
match response {
|
||||
'n' => Some(Action::UserNo),
|
||||
'y' => Some(Action::UserYes),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
},
|
||||
Some(Event::Mouse(_)) => None,
|
||||
_ => None,
|
||||
};
|
||||
Ok(action)
|
||||
}
|
||||
|
||||
fn update(&mut self, action: Action) -> Result<Option<Action>> {
|
||||
match action {
|
||||
Action::DismissPopup => self.popup_text.clear(),
|
||||
Action::DisplayPopup(new_type, new_text) => {
|
||||
info!("Show Popup ({new_type}): {new_text}");
|
||||
self.get_input = new_type == Type::Question;
|
||||
self.popup_type = new_type;
|
||||
self.popup_text = format!("\n{new_text}");
|
||||
}
|
||||
|
|
@ -85,6 +114,7 @@ impl Component for Popup {
|
|||
match self.popup_type {
|
||||
Type::Info => style = style.cyan(),
|
||||
Type::Error => style = style.red(),
|
||||
Type::Question => style = style.yellow(),
|
||||
Type::Success => style = style.green(),
|
||||
}
|
||||
|
||||
|
|
|
|||
197
core/src/components/select_ticket.rs
Normal file
197
core/src/components/select_ticket.rs
Normal file
|
|
@ -0,0 +1,197 @@
|
|||
// This file is part of Deja-Vu.
|
||||
//
|
||||
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||
// under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
// See the GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||
use color_eyre::Result;
|
||||
use crossterm::event::KeyCode;
|
||||
use ratatui::{
|
||||
prelude::*,
|
||||
widgets::{Block, Borders, Clear, Paragraph},
|
||||
};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use tokio::sync::mpsc::UnboundedSender;
|
||||
use tui_input::{Input, InputRequest};
|
||||
|
||||
use super::Component;
|
||||
use crate::{
|
||||
action::Action, components::popup::Type as PopupType, config::Config, osticket::OsTicket,
|
||||
state::Mode, tui::Event,
|
||||
};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct TicketSelection {
|
||||
command_tx: Option<UnboundedSender<Action>>,
|
||||
config: Config,
|
||||
input: Input,
|
||||
mode: Mode,
|
||||
osticket_arc: Arc<Mutex<OsTicket>>,
|
||||
}
|
||||
|
||||
impl TicketSelection {
|
||||
#[must_use]
|
||||
pub fn new(osticket_arc: Arc<Mutex<OsTicket>>) -> Self {
|
||||
Self {
|
||||
osticket_arc,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Component for TicketSelection {
|
||||
fn register_action_handler(&mut self, tx: UnboundedSender<Action>) -> Result<()> {
|
||||
self.command_tx = Some(tx);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn register_config_handler(&mut self, config: Config) -> Result<()> {
|
||||
self.config = config;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_events(&mut self, event: Option<Event>) -> Result<Option<Action>> {
|
||||
if self.mode != Mode::SelectTicket {
|
||||
return Ok(None);
|
||||
}
|
||||
let action = match event {
|
||||
Some(Event::Key(key_event)) => match key_event.code {
|
||||
KeyCode::Backspace => {
|
||||
self.input.handle(InputRequest::DeletePrevChar);
|
||||
if let Ok(id) = self.input.value().parse::<u16>() {
|
||||
Some(Action::SelectTicket(id))
|
||||
} else {
|
||||
Some(Action::ResetTicket)
|
||||
}
|
||||
}
|
||||
KeyCode::Char(c) => {
|
||||
let mut result = None;
|
||||
if c.is_ascii_digit() {
|
||||
self.input.handle(InputRequest::InsertChar(c));
|
||||
if let Ok(id) = self.input.value().parse::<u16>() {
|
||||
result = Some(Action::SelectTicket(id));
|
||||
} else {
|
||||
result = Some(Action::ResetTicket);
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
KeyCode::Enter => Some(Action::SetMode(Mode::ScanDisks)),
|
||||
KeyCode::Esc => Some(Action::DisplayPopup(
|
||||
PopupType::Question,
|
||||
String::from("Disable osTicket integration?\n\n\nY / N"),
|
||||
)),
|
||||
_ => None,
|
||||
},
|
||||
Some(Event::Mouse(_)) => None,
|
||||
_ => None,
|
||||
};
|
||||
Ok(action)
|
||||
}
|
||||
|
||||
fn update(&mut self, action: Action) -> Result<Option<Action>> {
|
||||
if let Action::SetMode(mode) = action {
|
||||
self.mode = mode;
|
||||
self.input.reset();
|
||||
if self.mode == Mode::SelectTicket
|
||||
&& let Ok(osticket) = self.osticket_arc.lock()
|
||||
&& let Some(id) = &osticket.ticket.id
|
||||
{
|
||||
id.to_string().chars().for_each(|c| {
|
||||
let _ = self.input.handle(InputRequest::InsertChar(c));
|
||||
});
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
fn draw(&mut self, frame: &mut Frame, area: Rect) -> Result<()> {
|
||||
if self.mode != Mode::SelectTicket {
|
||||
// Bail early
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let [_, center_area, _] = Layout::horizontal([
|
||||
Constraint::Length(1),
|
||||
Constraint::Min(1),
|
||||
Constraint::Length(1),
|
||||
])
|
||||
.areas(area);
|
||||
let [_, input_area, info_area, _] = Layout::vertical([
|
||||
Constraint::Length(1),
|
||||
Constraint::Length(3),
|
||||
Constraint::Min(1),
|
||||
Constraint::Length(1),
|
||||
])
|
||||
.areas(center_area);
|
||||
|
||||
frame.render_widget(Clear, area);
|
||||
let outer_block = Block::bordered().cyan().bold();
|
||||
frame.render_widget(outer_block, area);
|
||||
|
||||
// Input Box
|
||||
let width = input_area.width.max(3) - 3; // keep 2 for borders and 1 for cursor
|
||||
let scroll = self.input.visual_scroll(width as usize);
|
||||
let input = Paragraph::new(self.input.value())
|
||||
.scroll((0, scroll as u16))
|
||||
.white()
|
||||
.block(Block::new().borders(Borders::BOTTOM).title("Enter ID"));
|
||||
frame.render_widget(input, input_area);
|
||||
|
||||
// Info Box
|
||||
let mut lines = Vec::with_capacity(4);
|
||||
if let Ok(osticket) = self.osticket_arc.lock() {
|
||||
if !osticket.connected {
|
||||
lines.push(Line::from("Connecting...").style(Style::new().yellow()));
|
||||
} else {
|
||||
let ticket_id = if let Some(id) = &osticket.ticket.id {
|
||||
id.to_string()
|
||||
} else {
|
||||
String::from("...")
|
||||
};
|
||||
let ticket_name = if let Some(name) = &osticket.ticket.name {
|
||||
name.clone()
|
||||
} else {
|
||||
String::from("...")
|
||||
};
|
||||
let ticket_subject = if let Some(subject) = &osticket.ticket.subject {
|
||||
subject.clone()
|
||||
} else {
|
||||
String::from("...")
|
||||
};
|
||||
lines.push(Line::from(vec![
|
||||
Span::from("Ticket ID: ").bold(),
|
||||
Span::from(ticket_id),
|
||||
]));
|
||||
lines.push(Line::from(vec![
|
||||
Span::from("Customer Name: ").bold(),
|
||||
Span::from(ticket_name),
|
||||
]));
|
||||
lines.push(Line::from(vec![
|
||||
Span::from("Subject: ").bold(),
|
||||
Span::from(ticket_subject),
|
||||
]));
|
||||
}
|
||||
}
|
||||
let info = Paragraph::new(lines)
|
||||
.white()
|
||||
.block(Block::new().borders(Borders::NONE).title("Ticket Info"));
|
||||
frame.render_widget(info, info_area);
|
||||
|
||||
// Ratatui hides the cursor unless it's explicitly set. Position the cursor past the
|
||||
// end of the input text and one line down from the border to the input line
|
||||
let x = self.input.visual_cursor().max(scroll) - scroll;
|
||||
frame.set_cursor_position((input_area.x + x as u16, input_area.y + 1));
|
||||
|
||||
// Done
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
@ -62,6 +62,8 @@ pub struct Config {
|
|||
pub network_user: String,
|
||||
#[serde(default)]
|
||||
pub network_pass: String,
|
||||
#[serde(default)]
|
||||
pub ost_server: String,
|
||||
}
|
||||
|
||||
pub static PROJECT_NAME: &str = "DEJA-VU";
|
||||
|
|
@ -84,14 +86,15 @@ impl Config {
|
|||
let config_dir = get_config_dir();
|
||||
let mut builder = config::Config::builder()
|
||||
.set_default("app_title", default_config.app_title.as_str())?
|
||||
.set_default("clone_app_path", default_config.app_title.as_str())?
|
||||
.set_default("conemu_path", default_config.app_title.as_str())?
|
||||
.set_default("clone_app_path", default_config.clone_app_path.to_str())?
|
||||
.set_default("conemu_path", default_config.conemu_path.to_str())?
|
||||
.set_default("config_dir", config_dir.to_str().unwrap())?
|
||||
.set_default("data_dir", data_dir.to_str().unwrap())?
|
||||
.set_default("network_server", default_config.app_title.as_str())?
|
||||
.set_default("network_share", default_config.app_title.as_str())?
|
||||
.set_default("network_user", default_config.app_title.as_str())?
|
||||
.set_default("network_pass", default_config.app_title.as_str())?;
|
||||
.set_default("network_server", default_config.network_server.as_str())?
|
||||
.set_default("network_share", default_config.network_share.as_str())?
|
||||
.set_default("network_user", default_config.network_user.as_str())?
|
||||
.set_default("network_pass", default_config.network_pass.as_str())?
|
||||
.set_default("ost_server", default_config.ost_server.as_str())?;
|
||||
|
||||
let config_files = [
|
||||
("config.json5", config::FileFormat::Json5),
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ pub mod config;
|
|||
pub mod errors;
|
||||
pub mod line;
|
||||
pub mod logging;
|
||||
pub mod osticket;
|
||||
pub mod state;
|
||||
pub mod system;
|
||||
pub mod tasks;
|
||||
|
|
|
|||
127
core/src/osticket.rs
Normal file
127
core/src/osticket.rs
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
// This file is part of Deja-Vu.
|
||||
//
|
||||
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||
// under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
// See the GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||
//
|
||||
|
||||
use ost_db_connector::Ticket;
|
||||
use sqlx::{MySql, Pool};
|
||||
|
||||
use crate::system::disk::Disk;
|
||||
|
||||
pub enum ReportType {
|
||||
Source,
|
||||
Destination { label: String },
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct OsTicket {
|
||||
pub connected: bool,
|
||||
pub disabled: bool,
|
||||
pub error: String,
|
||||
pub server_url: String,
|
||||
pub report: Vec<String>,
|
||||
pub tech_note: String,
|
||||
pub ticket: Ticket,
|
||||
}
|
||||
|
||||
impl OsTicket {
|
||||
pub fn new(ost_server: &str) -> Self {
|
||||
Self {
|
||||
connected: false,
|
||||
disabled: false,
|
||||
server_url: ost_server.to_string(),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_error(&mut self, text: &str) {
|
||||
let lines: Vec<&str> = text
|
||||
.lines()
|
||||
.filter(|line| {
|
||||
let line = line.trim();
|
||||
!line.is_empty() && !line.eq_ignore_ascii_case("ERROR")
|
||||
})
|
||||
.collect();
|
||||
self.error = lines.join("\n");
|
||||
}
|
||||
|
||||
pub fn disable(&mut self) {
|
||||
self.connected = false;
|
||||
self.disabled = true;
|
||||
}
|
||||
|
||||
pub fn get_report(&mut self) -> String {
|
||||
if !self.error.is_empty() {
|
||||
self.report.push(format!("\n\nERROR: {}", self.error));
|
||||
}
|
||||
self.report.join("\n")
|
||||
}
|
||||
|
||||
pub fn set_db_pool(&mut self, pool: Option<Pool<MySql>>) {
|
||||
if self.disabled {
|
||||
return;
|
||||
}
|
||||
match pool {
|
||||
Some(pool) => {
|
||||
self.connected = true;
|
||||
self.ticket.pool = Some(pool);
|
||||
}
|
||||
None => {
|
||||
self.disable();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_report(&mut self, disk: &Disk, report_type: ReportType) {
|
||||
// Header
|
||||
if self.report.is_empty() {
|
||||
self.report.push(String::from("[Deja-Vu Report]"));
|
||||
if !self.tech_note.is_empty() {
|
||||
self.report.push(format!("Tech note: {}", self.tech_note));
|
||||
}
|
||||
}
|
||||
|
||||
// Disk Label
|
||||
match report_type {
|
||||
ReportType::Source => {
|
||||
self.report.push(String::new());
|
||||
self.report.push(String::from("Source:"));
|
||||
}
|
||||
ReportType::Destination { label } => {
|
||||
self.report.push(String::new());
|
||||
self.report.push(String::from("---"));
|
||||
self.report.push(String::new());
|
||||
self.report.push(format!("Destination ({label}):"));
|
||||
}
|
||||
}
|
||||
|
||||
// Disk Details
|
||||
self.report.push(format!(
|
||||
"... {:<8} {:>11} {:<4} {:<4} {}",
|
||||
"Disk ID", "Size", "Conn", "Type", "Model (Serial)"
|
||||
));
|
||||
self.report.push(format!("... {}", disk.description));
|
||||
self.report.push(String::new());
|
||||
self.report.push(format!(
|
||||
"... {:<8} {:>11} {:<7} {}",
|
||||
"Part ID", "Size", "(FS)", "\"Label\""
|
||||
));
|
||||
disk.parts_description.iter().for_each(|desc| {
|
||||
self.report.push(format!("... {desc}"));
|
||||
});
|
||||
if disk.parts_description.is_empty() {
|
||||
self.report.push(String::from("... -None-"));
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -42,6 +42,8 @@ pub enum Mode {
|
|||
Clone,
|
||||
SelectParts,
|
||||
PostClone,
|
||||
// osTicket
|
||||
SelectTicket,
|
||||
// Windows Installer
|
||||
ScanWinSources,
|
||||
SelectWinSource,
|
||||
|
|
|
|||
|
|
@ -41,6 +41,9 @@ tracing-error = "0.2.0"
|
|||
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "serde"] }
|
||||
check_elevation = "0.2.4"
|
||||
|
||||
[dependencies.ost-db-connector]
|
||||
git = "ssh://git@git.1201.com:2222/1201/ost-db-connector.git"
|
||||
|
||||
[build-dependencies]
|
||||
anyhow = "1.0.86"
|
||||
vergen-gix = { version = "1.0.0", features = ["build", "cargo"] }
|
||||
|
|
|
|||
|
|
@ -21,11 +21,13 @@ use core::{
|
|||
left::{Left, SelectionType},
|
||||
popup,
|
||||
right::Right,
|
||||
select_ticket::TicketSelection,
|
||||
state::StatefulList,
|
||||
title::Title,
|
||||
},
|
||||
config::Config,
|
||||
line::{DVLine, get_disk_description_right, get_part_description},
|
||||
osticket::{OsTicket, ReportType},
|
||||
state::Mode,
|
||||
system::{
|
||||
boot,
|
||||
|
|
@ -45,6 +47,7 @@ use std::{
|
|||
};
|
||||
|
||||
use color_eyre::Result;
|
||||
use ost_db_connector::ResponseColor;
|
||||
use ratatui::{
|
||||
crossterm::event::KeyEvent,
|
||||
layout::{Constraint, Direction, Layout},
|
||||
|
|
@ -52,7 +55,7 @@ use ratatui::{
|
|||
style::Color,
|
||||
};
|
||||
use tokio::sync::mpsc;
|
||||
use tracing::{debug, info};
|
||||
use tracing::{debug, info, warn};
|
||||
|
||||
use crate::state::State;
|
||||
|
||||
|
|
@ -67,6 +70,8 @@ pub struct App {
|
|||
should_quit: bool,
|
||||
should_suspend: bool,
|
||||
tick_rate: f64,
|
||||
// osTicket
|
||||
osticket_arc: Arc<Mutex<OsTicket>>,
|
||||
// App
|
||||
cur_mode: Mode,
|
||||
list: StatefulList<usize>,
|
||||
|
|
@ -79,7 +84,10 @@ pub struct App {
|
|||
impl App {
|
||||
pub fn new(tick_rate: f64, frame_rate: f64) -> Result<Self> {
|
||||
let (action_tx, action_rx) = mpsc::unbounded_channel();
|
||||
let config = Config::new()?;
|
||||
let disk_list_arc = Arc::new(Mutex::new(Vec::new()));
|
||||
let osticket = OsTicket::new(&config.ost_server);
|
||||
let osticket_arc = Arc::new(Mutex::new(osticket));
|
||||
let tasks = Tasks::new(action_tx.clone(), disk_list_arc.clone());
|
||||
Ok(Self {
|
||||
// TUI
|
||||
|
|
@ -90,6 +98,7 @@ impl App {
|
|||
Box::new(Left::new()),
|
||||
Box::new(Right::new()),
|
||||
Box::new(Footer::new()),
|
||||
Box::new(TicketSelection::new(osticket_arc.clone())),
|
||||
Box::new(popup::Popup::new()),
|
||||
],
|
||||
config: Config::new()?,
|
||||
|
|
@ -98,6 +107,8 @@ impl App {
|
|||
should_quit: false,
|
||||
should_suspend: false,
|
||||
tick_rate,
|
||||
// osTicket
|
||||
osticket_arc,
|
||||
// App
|
||||
state: State::new(disk_list_arc),
|
||||
cur_mode: Mode::default(),
|
||||
|
|
@ -110,7 +121,7 @@ impl App {
|
|||
|
||||
pub fn prev_mode(&mut self) -> Option<Mode> {
|
||||
match self.cur_mode {
|
||||
Mode::Home => Some(Mode::Home),
|
||||
Mode::Home | Mode::SelectTicket => Some(Mode::Home),
|
||||
Mode::Failed => Some(Mode::Failed),
|
||||
Mode::Done => Some(Mode::Done),
|
||||
Mode::SelectParts => Some(Mode::SelectParts),
|
||||
|
|
@ -142,7 +153,8 @@ impl App {
|
|||
|
||||
pub fn next_mode(&mut self) -> Option<Mode> {
|
||||
let new_mode = match self.cur_mode {
|
||||
Mode::Home | Mode::InstallDrivers => Mode::ScanDisks,
|
||||
Mode::Home => Mode::SelectTicket,
|
||||
Mode::SelectTicket | Mode::InstallDrivers => Mode::ScanDisks,
|
||||
Mode::ScanDisks => Mode::SelectDisks,
|
||||
Mode::SelectDisks => Mode::SelectTableType,
|
||||
Mode::SelectTableType => Mode::Confirm,
|
||||
|
|
@ -306,11 +318,21 @@ impl App {
|
|||
component.init(tui.size()?)?;
|
||||
}
|
||||
|
||||
// Start background task to connect to osTicket
|
||||
let osticket_arc = self.osticket_arc.clone();
|
||||
let ost_server = self.config.ost_server.clone();
|
||||
tokio::spawn(async move {
|
||||
let pool = ost_db_connector::connect(Some(ost_server), true).await;
|
||||
if let Ok(mut osticket) = osticket_arc.lock() {
|
||||
osticket.set_db_pool(pool);
|
||||
}
|
||||
});
|
||||
|
||||
let action_tx = self.action_tx.clone();
|
||||
action_tx.send(Action::SetMode(Mode::ScanDisks))?;
|
||||
action_tx.send(Action::SetMode(Mode::SelectTicket))?;
|
||||
loop {
|
||||
self.handle_events(&mut tui).await?;
|
||||
self.handle_actions(&mut tui)?;
|
||||
self.handle_actions(&mut tui).await?;
|
||||
if self.should_suspend {
|
||||
tui.suspend()?;
|
||||
action_tx.send(Action::Resume)?;
|
||||
|
|
@ -369,7 +391,7 @@ impl App {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_actions(&mut self, tui: &mut Tui) -> Result<()> {
|
||||
async fn handle_actions(&mut self, tui: &mut Tui) -> Result<()> {
|
||||
while let Ok(action) = self.action_rx.try_recv() {
|
||||
if action != Action::Tick && action != Action::Render {
|
||||
debug!("{action:?}");
|
||||
|
|
@ -389,6 +411,9 @@ impl App {
|
|||
Action::KeyUp => self.list.previous(),
|
||||
Action::KeyDown => self.list.next(),
|
||||
Action::Error(ref msg) => {
|
||||
if let Ok(mut osticket) = self.osticket_arc.lock() {
|
||||
osticket.add_error(msg);
|
||||
}
|
||||
self.action_tx
|
||||
.send(Action::DisplayPopup(popup::Type::Error, msg.clone()))?;
|
||||
self.action_tx.send(Action::SetMode(Mode::Failed))?;
|
||||
|
|
@ -407,6 +432,22 @@ impl App {
|
|||
},
|
||||
Action::Resize(w, h) => self.handle_resize(tui, w, h)?,
|
||||
Action::Render => self.render(tui)?,
|
||||
Action::PostResponse { color, ref text } => {
|
||||
if let Ok(osticket) = self.osticket_arc.lock()
|
||||
&& osticket.connected
|
||||
&& !osticket.disabled
|
||||
{
|
||||
let result = osticket.ticket.post_response(color, &text).await;
|
||||
match result {
|
||||
Ok(_) => {
|
||||
info!("Posted results to osTicket!");
|
||||
}
|
||||
Err(err) => {
|
||||
warn!("Failed to post results to osTicket: {err}");
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Action::PrevScreen => {
|
||||
if let Some(new_mode) = self.prev_mode() {
|
||||
self.prev_mode = new_mode;
|
||||
|
|
@ -462,6 +503,10 @@ impl App {
|
|||
self.selections[0] = one;
|
||||
self.selections[1] = two;
|
||||
}
|
||||
Action::SelectTicket(id) => {
|
||||
let mut osticket = self.osticket_arc.lock().unwrap();
|
||||
let _ = osticket.ticket.select_id(id).await;
|
||||
}
|
||||
Action::SetMode(new_mode) => {
|
||||
// Clear TableType selection
|
||||
match new_mode {
|
||||
|
|
@ -471,10 +516,15 @@ impl App {
|
|||
_ => {}
|
||||
}
|
||||
self.set_mode(new_mode)?;
|
||||
|
||||
// Update components
|
||||
self.action_tx
|
||||
.send(Action::UpdateFooter(build_footer_string(self.cur_mode)))?;
|
||||
self.action_tx.send(build_left_items(self))?;
|
||||
self.action_tx.send(build_right_items(self))?;
|
||||
|
||||
// Update state
|
||||
// TODO: Verify this shouldn't be before update components?
|
||||
match new_mode {
|
||||
Mode::SelectTableType | Mode::Confirm => {
|
||||
// Select source/dest disks
|
||||
|
|
@ -500,8 +550,74 @@ impl App {
|
|||
}
|
||||
_ => {}
|
||||
};
|
||||
|
||||
// Update osTicket
|
||||
match new_mode {
|
||||
Mode::Confirm => {
|
||||
if let Ok(mut osticket) = self.osticket_arc.lock()
|
||||
&& osticket.connected
|
||||
&& !osticket.disabled
|
||||
{
|
||||
info!("Updating OST Report (Before)...");
|
||||
let disk_list = self.state.disk_list.lock().unwrap();
|
||||
if let Some(source_index) = self.state.disk_index_source
|
||||
&& let Some(source_disk) = disk_list.get(source_index)
|
||||
&& let Some(dest_index) = self.state.disk_index_dest
|
||||
&& let Some(dest_disk) = disk_list.get(dest_index)
|
||||
{
|
||||
osticket.update_report(source_disk, ReportType::Source);
|
||||
osticket.update_report(
|
||||
dest_disk,
|
||||
ReportType::Destination {
|
||||
label: String::from("Before"),
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Mode::Done | Mode::Failed => {
|
||||
if let Ok(mut osticket) = self.osticket_arc.lock()
|
||||
&& osticket.connected
|
||||
&& !osticket.disabled
|
||||
{
|
||||
info!("Updating OST Report (After)...");
|
||||
let disk_list = self.state.disk_list.lock().unwrap();
|
||||
if let Some(dest_index) = self.state.disk_index_dest
|
||||
&& let Some(dest_disk) = disk_list.get(dest_index)
|
||||
{
|
||||
osticket.update_report(
|
||||
dest_disk,
|
||||
ReportType::Destination {
|
||||
label: String::from("After"),
|
||||
},
|
||||
);
|
||||
|
||||
// Set color
|
||||
let color = if new_mode == Mode::Failed {
|
||||
ResponseColor::DiagFail
|
||||
} else {
|
||||
ResponseColor::Normal
|
||||
};
|
||||
|
||||
// Upload osTicket report
|
||||
self.action_tx.send(Action::PostResponse {
|
||||
color,
|
||||
text: osticket.get_report(),
|
||||
})?;
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Action::TasksComplete => self.action_tx.send(Action::NextScreen)?,
|
||||
Action::UserNo => self.action_tx.send(Action::DismissPopup)?,
|
||||
Action::UserYes => {
|
||||
if let Ok(mut osticket) = self.osticket_arc.lock() {
|
||||
osticket.disable();
|
||||
}
|
||||
self.action_tx.send(Action::NextScreen)?;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
for component in &mut self.components {
|
||||
|
|
@ -554,8 +670,10 @@ impl App {
|
|||
|
||||
fn render(&mut self, tui: &mut Tui) -> Result<()> {
|
||||
tui.draw(|frame| {
|
||||
if let [header, _body, footer, left, right, popup] = get_chunks(frame.area())[..] {
|
||||
let component_areas = vec![header, left, right, footer, popup];
|
||||
if let [header, _body, footer, left, right, select_ticket, popup] =
|
||||
get_chunks(frame.area())[..]
|
||||
{
|
||||
let component_areas = vec![header, left, right, footer, select_ticket, popup];
|
||||
for (component, area) in zip(self.components.iter_mut(), component_areas) {
|
||||
if let Err(err) = component.draw(frame, area) {
|
||||
let _ = self
|
||||
|
|
@ -594,7 +712,7 @@ fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
|
|||
fn get_chunks(r: Rect) -> Vec<Rect> {
|
||||
let mut chunks: Vec<Rect> = Vec::with_capacity(6);
|
||||
|
||||
// Main sections
|
||||
// Main sections (Title, body, footer)
|
||||
chunks.extend(
|
||||
Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
|
|
@ -616,6 +734,9 @@ fn get_chunks(r: Rect) -> Vec<Rect> {
|
|||
.to_vec(),
|
||||
);
|
||||
|
||||
// Ticket Selection
|
||||
chunks.push(centered_rect(60, 40, r));
|
||||
|
||||
// Popup
|
||||
chunks.push(centered_rect(60, 25, r));
|
||||
|
||||
|
|
@ -635,7 +756,7 @@ fn build_footer_string(cur_mode: Mode) -> String {
|
|||
),
|
||||
Mode::SelectTableType => String::from("(Enter) to select / (b) to go back / (q) to quit"),
|
||||
Mode::Confirm => String::from("(Enter) to confirm / (b) to go back / (q) to quit"),
|
||||
Mode::Done | Mode::Failed => String::from("(Enter) or (q) to quit"),
|
||||
Mode::Done | Mode::Failed | Mode::SelectTicket => String::from("(Enter) or (q) to quit"),
|
||||
// Invalid states
|
||||
Mode::BootDiags
|
||||
| Mode::BootScan
|
||||
|
|
@ -709,6 +830,10 @@ fn build_left_items(app: &App) -> Action {
|
|||
});
|
||||
}
|
||||
}
|
||||
Mode::SelectTicket => {
|
||||
title = String::new();
|
||||
select_type = SelectionType::Loop;
|
||||
}
|
||||
Mode::Done | Mode::Failed => {
|
||||
select_type = SelectionType::Loop;
|
||||
title = String::from("Done");
|
||||
|
|
@ -735,6 +860,21 @@ fn build_right_items(app: &App) -> Action {
|
|||
let mut items = Vec::new();
|
||||
let mut labels: Vec<Vec<DVLine>> = Vec::new();
|
||||
let mut start_index = 0;
|
||||
if let Ok(osticket) = app.osticket_arc.lock()
|
||||
&& !osticket.disabled
|
||||
&& let Some(name) = &osticket.ticket.name
|
||||
{
|
||||
items.push(vec![DVLine {
|
||||
line_parts: vec![
|
||||
format!("osTicket: #{} ", osticket.ticket.id.unwrap()),
|
||||
name.clone(),
|
||||
String::from(" // "),
|
||||
osticket.ticket.subject.clone().unwrap(),
|
||||
],
|
||||
line_colors: vec![Color::Reset, Color::Cyan, Color::Reset, Color::Reset],
|
||||
}]);
|
||||
start_index += 1;
|
||||
}
|
||||
match app.cur_mode {
|
||||
Mode::InstallDrivers => {
|
||||
items.push(vec![DVLine {
|
||||
|
|
@ -745,7 +885,7 @@ fn build_right_items(app: &App) -> Action {
|
|||
line_parts: vec![get_cpu_name()],
|
||||
line_colors: vec![Color::Reset],
|
||||
}]);
|
||||
start_index = 2;
|
||||
start_index += 2;
|
||||
}
|
||||
Mode::SelectDisks | Mode::SelectTableType | Mode::Confirm => {
|
||||
// Labels
|
||||
|
|
@ -788,8 +928,13 @@ fn build_right_items(app: &App) -> Action {
|
|||
line_colors: vec![Color::Cyan],
|
||||
}]);
|
||||
}
|
||||
start_index += 1;
|
||||
items.push(vec![DVLine {
|
||||
line_parts: vec![String::new()],
|
||||
line_colors: vec![Color::Reset],
|
||||
}]);
|
||||
if let Some(index) = app.state.disk_index_dest {
|
||||
start_index = 1;
|
||||
start_index += 1;
|
||||
let disk_list = app.state.disk_list.lock().unwrap();
|
||||
if let Some(disk) = disk_list.get(index) {
|
||||
// Disk Details
|
||||
|
|
|
|||
Loading…
Reference in a new issue