|
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}; |
7 | 2 |
|
8 | 3 | 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 | +}; |
10 | 8 |
|
11 | 9 | 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, |
14 | 13 | }; |
15 | 14 |
|
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; |
121 | 16 |
|
122 | 17 | pub struct BuildAndScanCommand<'a, C, B, S> |
123 | 18 | where |
|
0 commit comments