diff --git a/src/app/document_database.rs b/src/app/document_database.rs index 674a4bd..f155027 100644 --- a/src/app/document_database.rs +++ b/src/app/document_database.rs @@ -44,10 +44,6 @@ impl InMemoryDocumentDatabase { self.read_document(uri).await.map(|e| e.text) } - pub async fn remove_document(&self, uri: &str) { - self.documents.write().await.remove(uri); - } - pub async fn append_document_diagnostics( &self, uri: impl Into, @@ -164,17 +160,6 @@ mod tests { assert_eq!(document.text, "updated"); } - #[tokio::test] - async fn test_remove_document() { - let db = InMemoryDocumentDatabase::default(); - - db.write_document_text("file://main.rs", "contents").await; - assert!(db.read_document("file://main.rs").await.is_some()); - - db.remove_document("file://main.rs").await; - assert!(db.read_document("file://main.rs").await.is_none()); - } - #[tokio::test] async fn test_add_diagnostics() { let db = InMemoryDocumentDatabase::default(); @@ -200,13 +185,13 @@ mod tests { db.append_document_diagnostics( "file://mod1.rs", - &vec![create_diagnostic((0, 0), (0, 6), "Incorrect module name")], + &[create_diagnostic((0, 0), (0, 6), "Incorrect module name")], ) .await; db.append_document_diagnostics( "file://mod2.rs", - &vec![ + &[ create_diagnostic((0, 0), (0, 6), "Incorrect module name"), create_diagnostic((0, 7), (0, 8), "Unexpected token"), ], diff --git a/src/app/image_builder.rs b/src/app/image_builder.rs index 5162c6e..e0b1ec0 100644 --- a/src/app/image_builder.rs +++ b/src/app/image_builder.rs @@ -8,6 +8,8 @@ pub trait ImageBuilder { } pub struct ImageBuildResult { + // FIXME(fede): Eventually we will need to check if this dead code is actually needed for our use case + #[allow(dead_code)] pub image_id: String, pub image_name: String, } diff --git a/src/app/image_scanner.rs b/src/app/image_scanner.rs index 117d632..dfbf657 100644 --- a/src/app/image_scanner.rs +++ b/src/app/image_scanner.rs @@ -1,7 +1,6 @@ use std::error::Error; use thiserror::Error; -use tracing::info; use crate::domain::scanresult::scan_result::ScanResult; @@ -10,37 +9,6 @@ pub trait ImageScanner { async fn scan_image(&self, image_pull_string: &str) -> Result; } -#[derive(Clone, Copy, Debug, Default)] -pub struct Vulnerabilities { - pub critical: usize, - pub high: usize, - pub medium: usize, - pub low: usize, - pub negligible: usize, -} - -impl From for Vulnerabilities { - fn from(value: ScanResult) -> Self { - value - .vulnerabilities() - .into_iter() - .fold(Self::default(), |mut acc, v| { - use crate::domain::scanresult::severity::Severity; - match v.severity() { - Severity::Critical => acc.critical += 1, - Severity::High => acc.high += 1, - Severity::Medium => acc.medium += 1, - Severity::Low => acc.low += 1, - Severity::Negligible => acc.negligible += 1, - Severity::Unknown => { - info!("unknown severity {:?}", v) - } - } - acc - }) - } -} - #[derive(Error, Debug)] pub enum ImageScanError { #[error("error in the internal scanner execution: {0}")] diff --git a/src/app/lsp_server/command_generator.rs b/src/app/lsp_server/command_generator.rs index d6fc497..1f0aa65 100644 --- a/src/app/lsp_server/command_generator.rs +++ b/src/app/lsp_server/command_generator.rs @@ -55,7 +55,7 @@ impl From for CodeLens { } } -pub fn generate_commands_for_uri(uri: &Url, content: &str) -> Vec { +pub fn generate_commands_for_uri(uri: &Url, content: &str) -> Result, String> { let file_uri = uri.as_str(); if file_uri.contains("docker-compose.yml") @@ -65,24 +65,28 @@ pub fn generate_commands_for_uri(uri: &Url, content: &str) -> Vec { { generate_compose_commands(uri, content) } else { - generate_dockerfile_commands(uri, content) + Ok(generate_dockerfile_commands(uri, content)) } } -fn generate_compose_commands(url: &Url, content: &str) -> Vec { +fn generate_compose_commands(url: &Url, content: &str) -> Result, String> { let mut commands = vec![]; - if let Ok(instructions) = parse_compose_file(content) { - for instruction in instructions { - commands.push( - SupportedCommands::ExecuteBaseImageScan { - location: Location::new(url.clone(), instruction.range), - image: instruction.image_name, - } - .into(), - ); + match parse_compose_file(content) { + Ok(instructions) => { + for instruction in instructions { + commands.push( + SupportedCommands::ExecuteBaseImageScan { + location: Location::new(url.clone(), instruction.range), + image: instruction.image_name, + } + .into(), + ); + } } + Err(err) => return Err(format!("{}", err)), } - commands + + Ok(commands) } fn generate_dockerfile_commands(uri: &Url, content: &str) -> Vec { diff --git a/src/app/lsp_server/lsp_server_inner.rs b/src/app/lsp_server/lsp_server_inner.rs index f3106df..91ef495 100644 --- a/src/app/lsp_server/lsp_server_inner.rs +++ b/src/app/lsp_server/lsp_server_inner.rs @@ -1,5 +1,5 @@ use serde_json::Value; -use tower_lsp::jsonrpc::{Error, ErrorCode, Result}; +use tower_lsp::jsonrpc::{self, Error, ErrorCode, Result}; use tower_lsp::lsp_types::HoverContents::Markup; use tower_lsp::lsp_types::MarkupKind::Markdown; use tower_lsp::lsp_types::{ @@ -78,7 +78,7 @@ where }; let commands = command_generator::generate_commands_for_uri(uri, &content); - Ok(commands) + commands.map_err(|e| jsonrpc::Error::internal_error().with_message(e)) } pub async fn initialize( diff --git a/src/app/lsp_server/mod.rs b/src/app/lsp_server/mod.rs index f68a970..07eb3ca 100644 --- a/src/app/lsp_server/mod.rs +++ b/src/app/lsp_server/mod.rs @@ -6,7 +6,7 @@ use tower_lsp::jsonrpc::{Error, Result}; use tower_lsp::lsp_types::{ CodeActionParams, CodeActionResponse, CodeLens, CodeLensParams, DidChangeConfigurationParams, DidChangeTextDocumentParams, DidOpenTextDocumentParams, ExecuteCommandParams, Hover, - HoverParams, InitializeParams, InitializeResult, InitializedParams, Range, + HoverParams, InitializeParams, InitializeResult, InitializedParams, }; use super::{InMemoryDocumentDatabase, LSPClient}; @@ -41,13 +41,6 @@ impl LSPServer { } } -struct CommandInfo { - title: String, - command: String, - arguments: Option>, - range: Range, -} - #[async_trait::async_trait] impl LanguageServer for LSPServer where diff --git a/src/app/mod.rs b/src/app/mod.rs index 6a1c776..c53d7c2 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -10,7 +10,7 @@ mod queries; pub use document_database::*; pub use image_builder::{ImageBuildError, ImageBuildResult, ImageBuilder}; -pub use image_scanner::{ImageScanError, ImageScanner, Vulnerabilities}; +pub use image_scanner::{ImageScanError, ImageScanner}; pub use lsp_client::LSPClient; pub use lsp_interactor::LspInteractor; pub use lsp_server::LSPServer; diff --git a/src/domain/mod.rs b/src/domain/mod.rs index 97f4ef3..dd65c0e 100644 --- a/src/domain/mod.rs +++ b/src/domain/mod.rs @@ -1 +1,2 @@ +#![allow(dead_code)] pub mod scanresult; diff --git a/src/infra/compose_ast_parser.rs b/src/infra/compose_ast_parser.rs index deddb1a..f10abc1 100644 --- a/src/infra/compose_ast_parser.rs +++ b/src/infra/compose_ast_parser.rs @@ -1,3 +1,4 @@ +use thiserror::Error; use tower_lsp::lsp_types::{Position, Range}; #[derive(Debug, PartialEq)] @@ -6,8 +7,9 @@ pub struct ImageInstruction { pub range: Range, } -#[derive(Debug)] +#[derive(Debug, Error)] pub enum ParseError { + #[error("Invalid yaml: {0}")] InvalidYaml(marked_yaml::LoadError), } diff --git a/src/infra/mod.rs b/src/infra/mod.rs index a1e0c31..f3e32bd 100644 --- a/src/infra/mod.rs +++ b/src/infra/mod.rs @@ -9,6 +9,6 @@ mod sysdig_image_scanner_json_scan_result_v1; pub use sysdig_image_scanner::{SysdigAPIToken, SysdigImageScanner}; pub mod lsp_logger; pub use component_factory_impl::ConcreteComponentFactory; -pub use compose_ast_parser::{ImageInstruction, parse_compose_file}; +pub use compose_ast_parser::parse_compose_file; pub use docker_image_builder::DockerImageBuilder; -pub use dockerfile_ast_parser::{Instruction, parse_dockerfile}; +pub use dockerfile_ast_parser::parse_dockerfile; diff --git a/src/lib.rs b/src/lib.rs index 5b8e0af..cc87de3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,3 @@ -#![allow(dead_code)] #![cfg_attr(not(test), deny(clippy::unwrap_used))] #![cfg_attr(not(test), deny(clippy::expect_used))] diff --git a/src/main.rs b/src/main.rs index d30d08f..1838284 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,15 @@ -use clap::Parser; -use sysdig_lsp::{ +use crate::{ app::LSPServer, infra::{ConcreteComponentFactory, lsp_logger::LSPLogger}, }; +use clap::Parser; use tower_lsp::{LspService, Server}; use tracing_subscriber::layer::SubscriberExt; +mod app; +mod domain; +mod infra; + #[derive(Parser, Debug)] #[command(version, author, about, long_about)] struct Args {}