Skip to content

Commit 2f945af

Browse files
committed
refactor(lsp_server): organize commands into modules
1 parent 54c8849 commit 2f945af

File tree

4 files changed

+129
-115
lines changed

4 files changed

+129
-115
lines changed

src/app/lsp_server/commands.rs renamed to src/app/lsp_server/commands/build_and_scan.rs

Lines changed: 9 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,18 @@
1-
use tower_lsp::jsonrpc::Result;
2-
3-
#[async_trait::async_trait]
4-
pub trait LspCommand {
5-
async fn execute(&mut self) -> Result<()>;
6-
}
1+
use std::{path::PathBuf, str::FromStr, sync::Arc};
72

83
use itertools::Itertools;
9-
use tower_lsp::lsp_types::{Diagnostic, DiagnosticSeverity, Location, MessageType};
4+
use tower_lsp::jsonrpc::Result;
5+
use tower_lsp::lsp_types::{
6+
Diagnostic, DiagnosticSeverity, Location, MessageType, Position, Range,
7+
};
108

119
use crate::{
12-
app::{ImageScanner, LSPClient, LspInteractor},
13-
domain::scanresult::{scan_result::ScanResult, severity::Severity},
10+
app::{ImageBuilder, ImageScanner, LSPClient, LspInteractor, lsp_server::WithContext},
11+
domain::scanresult::{layer::Layer, scan_result::ScanResult, severity::Severity},
12+
infra::parse_dockerfile,
1413
};
1514

16-
use super::WithContext;
17-
18-
pub struct ScanBaseImageCommand<'a, C, S>
19-
where
20-
S: ImageScanner,
21-
{
22-
image_scanner: &'a S,
23-
interactor: &'a LspInteractor<C>,
24-
location: Location,
25-
image: String,
26-
}
27-
28-
impl<'a, C, S> ScanBaseImageCommand<'a, C, S>
29-
where
30-
S: ImageScanner,
31-
{
32-
pub fn new(
33-
image_scanner: &'a S,
34-
interactor: &'a LspInteractor<C>,
35-
location: Location,
36-
image: String,
37-
) -> Self {
38-
Self {
39-
image_scanner,
40-
interactor,
41-
location,
42-
image,
43-
}
44-
}
45-
}
46-
47-
#[async_trait::async_trait]
48-
impl<'a, C, S> LspCommand for ScanBaseImageCommand<'a, C, S>
49-
where
50-
C: LSPClient + Sync,
51-
S: ImageScanner + Sync,
52-
{
53-
async fn execute(&mut self) -> tower_lsp::jsonrpc::Result<()> {
54-
let image_name = &self.image;
55-
self.interactor
56-
.show_message(
57-
MessageType::INFO,
58-
format!("Starting scan of {image_name}...").as_str(),
59-
)
60-
.await;
61-
62-
let scan_result = self
63-
.image_scanner
64-
.scan_image(image_name)
65-
.await
66-
.map_err(|e| tower_lsp::jsonrpc::Error::internal_error().with_message(e.to_string()))?;
67-
68-
self.interactor
69-
.show_message(
70-
MessageType::INFO,
71-
format!("Finished scan of {image_name}.").as_str(),
72-
)
73-
.await;
74-
75-
let diagnostic = {
76-
let mut diagnostic = Diagnostic {
77-
range: self.location.range,
78-
severity: Some(DiagnosticSeverity::HINT),
79-
message: "No vulnerabilities found.".to_owned(),
80-
..Default::default()
81-
};
82-
83-
if !scan_result.vulnerabilities().is_empty() {
84-
let vulns = scan_result
85-
.vulnerabilities()
86-
.iter()
87-
.counts_by(|v| v.severity());
88-
diagnostic.message = format!(
89-
"Vulnerabilities found for {}: {} Critical, {} High, {} Medium, {} Low, {} Negligible",
90-
image_name,
91-
vulns.get(&Severity::Critical).unwrap_or(&0_usize),
92-
vulns.get(&Severity::High).unwrap_or(&0_usize),
93-
vulns.get(&Severity::Medium).unwrap_or(&0_usize),
94-
vulns.get(&Severity::Low).unwrap_or(&0_usize),
95-
vulns.get(&Severity::Negligible).unwrap_or(&0_usize),
96-
);
97-
98-
diagnostic.severity = Some(if scan_result.evaluation_result().is_passed() {
99-
DiagnosticSeverity::INFORMATION
100-
} else {
101-
DiagnosticSeverity::ERROR
102-
});
103-
}
104-
105-
diagnostic
106-
};
107-
108-
let uri = self.location.uri.as_str();
109-
self.interactor.remove_diagnostics(uri).await;
110-
self.interactor
111-
.append_document_diagnostics(uri, &[diagnostic])
112-
.await;
113-
self.interactor.publish_all_diagnostics().await
114-
}
115-
}
116-
117-
use std::{path::PathBuf, str::FromStr, sync::Arc};
118-
use tower_lsp::lsp_types::{Position, Range};
119-
120-
use crate::{app::ImageBuilder, domain::scanresult::layer::Layer, infra::parse_dockerfile};
15+
use super::LspCommand;
12116

12217
pub struct BuildAndScanCommand<'a, C, B, S>
12318
where

src/app/lsp_server/commands/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
pub mod build_and_scan;
2+
pub mod scan_base_image;
3+
4+
use tower_lsp::jsonrpc::Result;
5+
6+
#[async_trait::async_trait]
7+
pub trait LspCommand {
8+
async fn execute(&mut self) -> Result<()>;
9+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
use itertools::Itertools;
2+
use tower_lsp::lsp_types::{Diagnostic, DiagnosticSeverity, Location, MessageType};
3+
4+
use crate::{
5+
app::{ImageScanner, LSPClient, LspInteractor, lsp_server::WithContext},
6+
domain::scanresult::severity::Severity,
7+
};
8+
9+
use super::LspCommand;
10+
11+
pub struct ScanBaseImageCommand<'a, C, S>
12+
where
13+
S: ImageScanner,
14+
{
15+
image_scanner: &'a S,
16+
interactor: &'a LspInteractor<C>,
17+
location: Location,
18+
image: String,
19+
}
20+
21+
impl<'a, C, S> ScanBaseImageCommand<'a, C, S>
22+
where
23+
S: ImageScanner,
24+
{
25+
pub fn new(
26+
image_scanner: &'a S,
27+
interactor: &'a LspInteractor<C>,
28+
location: Location,
29+
image: String,
30+
) -> Self {
31+
Self {
32+
image_scanner,
33+
interactor,
34+
location,
35+
image,
36+
}
37+
}
38+
}
39+
40+
#[async_trait::async_trait]
41+
impl<'a, C, S> LspCommand for ScanBaseImageCommand<'a, C, S>
42+
where
43+
C: LSPClient + Sync,
44+
S: ImageScanner + Sync,
45+
{
46+
async fn execute(&mut self) -> tower_lsp::jsonrpc::Result<()> {
47+
let image_name = &self.image;
48+
self.interactor
49+
.show_message(
50+
MessageType::INFO,
51+
format!("Starting scan of {image_name}...").as_str(),
52+
)
53+
.await;
54+
55+
let scan_result = self
56+
.image_scanner
57+
.scan_image(image_name)
58+
.await
59+
.map_err(|e| tower_lsp::jsonrpc::Error::internal_error().with_message(e.to_string()))?;
60+
61+
self.interactor
62+
.show_message(
63+
MessageType::INFO,
64+
format!("Finished scan of {image_name}.").as_str(),
65+
)
66+
.await;
67+
68+
let diagnostic = {
69+
let mut diagnostic = Diagnostic {
70+
range: self.location.range,
71+
severity: Some(DiagnosticSeverity::HINT),
72+
message: "No vulnerabilities found.".to_owned(),
73+
..Default::default()
74+
};
75+
76+
if !scan_result.vulnerabilities().is_empty() {
77+
let vulns = scan_result
78+
.vulnerabilities()
79+
.iter()
80+
.counts_by(|v| v.severity());
81+
diagnostic.message = format!(
82+
"Vulnerabilities found for {}: {} Critical, {} High, {} Medium, {} Low, {} Negligible",
83+
image_name,
84+
vulns.get(&Severity::Critical).unwrap_or(&0_usize),
85+
vulns.get(&Severity::High).unwrap_or(&0_usize),
86+
vulns.get(&Severity::Medium).unwrap_or(&0_usize),
87+
vulns.get(&Severity::Low).unwrap_or(&0_usize),
88+
vulns.get(&Severity::Negligible).unwrap_or(&0_usize),
89+
);
90+
91+
diagnostic.severity = Some(if scan_result.evaluation_result().is_passed() {
92+
DiagnosticSeverity::INFORMATION
93+
} else {
94+
DiagnosticSeverity::ERROR
95+
});
96+
}
97+
98+
diagnostic
99+
};
100+
101+
let uri = self.location.uri.as_str();
102+
self.interactor.remove_diagnostics(uri).await;
103+
self.interactor
104+
.append_document_diagnostics(uri, &[diagnostic])
105+
.await;
106+
self.interactor.publish_all_diagnostics().await
107+
}
108+
}

src/app/lsp_server/lsp_server_inner.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ use super::super::LspInteractor;
1313
use super::super::component_factory::{ComponentFactory, Config};
1414
use super::super::queries::QueryExecutor;
1515
use super::command_generator;
16-
use super::commands::{BuildAndScanCommand, LspCommand, ScanBaseImageCommand};
16+
use super::commands::{
17+
LspCommand, build_and_scan::BuildAndScanCommand, scan_base_image::ScanBaseImageCommand,
18+
};
1719
use super::{InMemoryDocumentDatabase, LSPClient, WithContext};
1820

1921
use super::supported_commands::SupportedCommands;

0 commit comments

Comments
 (0)