Skip to content

Commit c969d2b

Browse files
committed
refactor: make the server send the command with image name to scan
1 parent c8de783 commit c969d2b

File tree

2 files changed

+58
-33
lines changed

2 files changed

+58
-33
lines changed

src/app/lsp_server.rs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use super::commands::CommandExecutor;
1919
use super::component_factory::{ComponentFactory, Config};
2020
use super::queries::QueryExecutor;
2121
use super::{InMemoryDocumentDatabase, LSPClient};
22-
use crate::infra::parse_compose_file;
22+
use crate::infra::{parse_compose_file, parse_dockerfile};
2323

2424
pub struct LSPServer<C> {
2525
command_executor: CommandExecutor<C>,
@@ -124,7 +124,11 @@ where
124124
commands.push(CommandInfo {
125125
title: "Scan base image".to_string(),
126126
command: SupportedCommands::ExecuteBaseImageScan.to_string(),
127-
arguments: Some(vec![json!(uri), json!(instruction.range.start.line)]),
127+
arguments: Some(vec![
128+
json!(uri),
129+
json!(instruction.range.start.line),
130+
json!(instruction.image_name),
131+
]),
128132
range: instruction.range,
129133
});
130134
}
@@ -138,35 +142,32 @@ where
138142
content: &str,
139143
) -> Vec<CommandInfo> {
140144
let mut commands = vec![];
141-
if let Some(last_line_starting_with_from_statement) = content
142-
.lines()
143-
.enumerate()
144-
.filter(|(_, line)| line.trim_start().starts_with("FROM "))
145-
.map(|(line_num, _)| line_num)
146-
.last()
145+
let instructions = parse_dockerfile(content);
146+
if let Some(last_from_instruction) = instructions
147+
.iter()
148+
.filter(|instruction| instruction.keyword == "FROM")
149+
.next_back()
147150
{
148151
let range = Range::new(
149-
Position::new(last_line_starting_with_from_statement as u32, 0),
150-
Position::new(last_line_starting_with_from_statement as u32, 0),
152+
Position::new(last_from_instruction.range.start.line, 0),
153+
Position::new(last_from_instruction.range.start.line, 0),
151154
);
155+
let line = last_from_instruction.range.start.line;
152156
commands.push(CommandInfo {
153157
title: "Build and scan".to_string(),
154158
command: SupportedCommands::ExecuteBuildAndScan.to_string(),
155-
arguments: Some(vec![
156-
json!(uri),
157-
json!(last_line_starting_with_from_statement),
158-
]),
159-
range,
160-
});
161-
commands.push(CommandInfo {
162-
title: "Scan base image".to_string(),
163-
command: SupportedCommands::ExecuteBaseImageScan.to_string(),
164-
arguments: Some(vec![
165-
json!(uri),
166-
json!(last_line_starting_with_from_statement),
167-
]),
159+
arguments: Some(vec![json!(uri), json!(line)]),
168160
range,
169161
});
162+
163+
if let Some(image_name) = last_from_instruction.arguments.first() {
164+
commands.push(CommandInfo {
165+
title: "Scan base image".to_string(),
166+
command: SupportedCommands::ExecuteBaseImageScan.to_string(),
167+
arguments: Some(vec![json!(uri), json!(line), json!(image_name)]),
168+
range,
169+
});
170+
}
170171
}
171172
commands
172173
}

tests/general.rs

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ async fn when_the_client_asks_for_the_existing_code_actions_it_receives_the_avai
4242
CodeActionOrCommand::Command(Command {
4343
title: "Scan base image".to_string(),
4444
command: "sysdig-lsp.execute-scan".to_string(),
45-
arguments: Some(vec![json!("file://dockerfile/"), json!(0)])
45+
arguments: Some(vec![json!("file://dockerfile/"), json!(0), json!("alpine")])
4646
})
4747
]
4848
);
@@ -77,7 +77,7 @@ async fn when_the_client_asks_for_the_existing_code_actions_but_the_dockerfile_c
7777
CodeActionOrCommand::Command(Command {
7878
title: "Scan base image".to_string(),
7979
command: "sysdig-lsp.execute-scan".to_string(),
80-
arguments: Some(vec![json!("file://dockerfile/"), json!(1)])
80+
arguments: Some(vec![json!("file://dockerfile/"), json!(1), json!("ubuntu")])
8181
})
8282
]
8383
);
@@ -115,7 +115,7 @@ async fn when_the_client_asks_for_the_existing_code_lens_it_receives_the_availab
115115
command: Some(Command {
116116
title: "Scan base image".to_string(),
117117
command: "sysdig-lsp.execute-scan".to_string(),
118-
arguments: Some(vec![json!("file://dockerfile/"), json!(0)])
118+
arguments: Some(vec![json!("file://dockerfile/"), json!(0), json!("alpine")])
119119
}),
120120
data: None
121121
}
@@ -152,7 +152,7 @@ async fn when_the_client_asks_for_the_existing_code_lens_but_the_dockerfile_cont
152152
command: Some(Command {
153153
title: "Scan base image".to_string(),
154154
command: "sysdig-lsp.execute-scan".to_string(),
155-
arguments: Some(vec![json!("file://dockerfile/"), json!(1)])
155+
arguments: Some(vec![json!("file://dockerfile/"), json!(1), json!("ubuntu")])
156156
}),
157157
data: None
158158
}
@@ -182,7 +182,11 @@ async fn when_the_client_asks_for_code_lens_in_a_compose_file_it_receives_them()
182182
command: Some(Command {
183183
title: "Scan base image".to_string(),
184184
command: "sysdig-lsp.execute-scan".to_string(),
185-
arguments: Some(vec![json!("file://docker-compose.yml/"), json!(2)])
185+
arguments: Some(vec![
186+
json!("file://docker-compose.yml/"),
187+
json!(2),
188+
json!("nginx:latest")
189+
])
186190
}),
187191
data: None
188192
},
@@ -191,7 +195,11 @@ async fn when_the_client_asks_for_code_lens_in_a_compose_file_it_receives_them()
191195
command: Some(Command {
192196
title: "Scan base image".to_string(),
193197
command: "sysdig-lsp.execute-scan".to_string(),
194-
arguments: Some(vec![json!("file://docker-compose.yml/"), json!(4)])
198+
arguments: Some(vec![
199+
json!("file://docker-compose.yml/"),
200+
json!(4),
201+
json!("postgres:13")
202+
])
195203
}),
196204
data: None
197205
}
@@ -218,7 +226,11 @@ async fn when_the_client_asks_for_code_actions_in_a_compose_file_it_receives_the
218226
vec![CodeActionOrCommand::Command(Command {
219227
title: "Scan base image".to_string(),
220228
command: "sysdig-lsp.execute-scan".to_string(),
221-
arguments: Some(vec![json!("file://docker-compose.yml/"), json!(2)])
229+
arguments: Some(vec![
230+
json!("file://docker-compose.yml/"),
231+
json!(2),
232+
json!("nginx:latest")
233+
])
222234
})]
223235
);
224236
}
@@ -242,7 +254,11 @@ async fn when_the_client_asks_for_code_lens_in_a_complex_compose_yaml_file_it_re
242254
command: Some(Command {
243255
title: "Scan base image".to_string(),
244256
command: "sysdig-lsp.execute-scan".to_string(),
245-
arguments: Some(vec![json!("file://compose.yaml/"), json!(4)])
257+
arguments: Some(vec![
258+
json!("file://compose.yaml/"),
259+
json!(4),
260+
json!("nginx:latest")
261+
])
246262
}),
247263
data: None
248264
},
@@ -251,7 +267,11 @@ async fn when_the_client_asks_for_code_lens_in_a_complex_compose_yaml_file_it_re
251267
command: Some(Command {
252268
title: "Scan base image".to_string(),
253269
command: "sysdig-lsp.execute-scan".to_string(),
254-
arguments: Some(vec![json!("file://compose.yaml/"), json!(9)])
270+
arguments: Some(vec![
271+
json!("file://compose.yaml/"),
272+
json!(9),
273+
json!("postgres:13")
274+
])
255275
}),
256276
data: None
257277
},
@@ -260,7 +280,11 @@ async fn when_the_client_asks_for_code_lens_in_a_complex_compose_yaml_file_it_re
260280
command: Some(Command {
261281
title: "Scan base image".to_string(),
262282
command: "sysdig-lsp.execute-scan".to_string(),
263-
arguments: Some(vec![json!("file://compose.yaml/"), json!(13)])
283+
arguments: Some(vec![
284+
json!("file://compose.yaml/"),
285+
json!(13),
286+
json!("my-api:1.0")
287+
])
264288
}),
265289
data: None
266290
}

0 commit comments

Comments
 (0)