Skip to content

Commit 0ac81eb

Browse files
authored
Display an error message when apply or diff with a non-existing file (#300)
* Display an error message when applying or diffing a non-existing file * Handle not found in delete cmd * update * update message * fix test * add comment
1 parent eb30da1 commit 0ac81eb

File tree

7 files changed

+57
-18
lines changed

7 files changed

+57
-18
lines changed

src/main/java/com/michelin/kafkactl/Kafkactl.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,11 @@ public class Kafkactl implements Callable<Integer> {
7878
public CommandSpec commandSpec;
7979

8080
/**
81-
* Main Micronaut method There are 3 ways to configure kafkactl: - Setup config file in $HOME/.kafkactl/config.yml -
82-
* Setup config file anywhere and set KAFKACTL_CONFIG=/path/to/config.yml - No file but environment variables
83-
* instead
81+
* Main Micronaut method.
82+
*
83+
* <p>There are three ways to configure kafkactl: - Set up a config file in $HOME/.kafkactl/config.yml - Set up a
84+
* config file anywhere and set KAFKACTL_CONFIG=/path/to/config.yml - Use environment variables instead of a config
85+
* file
8486
*
8587
* @param args Input arguments
8688
*/

src/main/java/com/michelin/kafkactl/command/Delete.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ public Integer onAuthSuccess() {
130130
*/
131131
private List<Resource> parseResources(String namespace) {
132132
if (config.fileConfig != null && config.fileConfig.file.isPresent()) {
133+
if (!config.fileConfig.file.get().exists()) {
134+
throw new ParameterException(
135+
commandSpec.commandLine(),
136+
"File or directory \"" + config.fileConfig.file.get().getAbsolutePath() + "\" not found.");
137+
}
138+
133139
// List all files to process
134140
List<File> yamlFiles =
135141
fileService.computeYamlFileList(config.fileConfig.file.get(), config.fileConfig.recursive);

src/main/java/com/michelin/kafkactl/hook/HelpHook.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
import picocli.CommandLine.Command;
2222
import picocli.CommandLine.Option;
2323

24-
/** Help abstract command. */
24+
/**
25+
* Help abstract command. This is preferred over the mixinStandardHelpOptions property of {@link Command}, which
26+
* displays the "-V" version option that is only available on the main Kafkactl command.
27+
*/
2528
@Command
2629
public abstract class HelpHook {
2730
@Option(

src/main/java/com/michelin/kafkactl/service/ResourceService.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,13 +496,20 @@ public int vaultsOnConnectClusters(
496496
*/
497497
public List<Resource> parseResources(Optional<File> file, boolean recursive, CommandSpec commandSpec) {
498498
if (file.isPresent()) {
499+
if (!file.get().exists()) {
500+
throw new ParameterException(
501+
commandSpec.commandLine(),
502+
"File or directory \"" + file.get().getAbsolutePath() + "\" not found.");
503+
}
504+
499505
// List all files to process
500506
List<File> yamlFiles = fileService.computeYamlFileList(file.get(), recursive);
501507
if (yamlFiles.isEmpty()) {
502508
throw new ParameterException(
503509
commandSpec.commandLine(),
504510
"Could not find YAML or YML files in " + file.get().getName() + " directory.");
505511
}
512+
506513
// Load each files
507514
return fileService.parseResourceListFromFiles(yamlFiles);
508515
}
@@ -619,12 +626,10 @@ private List<Resource> prepareSchemaResources(List<Resource> resources, CommandS
619626
String name =
620627
new AvroSchema(spec.get(SCHEMA_FIELD).toString(), references, resolvedReferences, null).name();
621628

622-
// String subjectName = resource.getMetadata().getName();
623629
Stream<Resource> schemasList =
624630
Stream.concat(schemasByName.getOrDefault(name, Stream.of()), Stream.of(resource));
625631
schemasByName.put(name, schemasList);
626632
referencesByParentName.put(name, references);
627-
628633
} else {
629634
finalResources.add(resource);
630635
}

src/test/java/com/michelin/kafkactl/command/DeleteTest.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,28 @@ void shouldReturnInvalidCurrentContext() {
9898

9999
@Test
100100
void shouldNotDeleteByNameWhenNotAuthenticated() {
101+
CommandLine cmd = new CommandLine(delete);
102+
StringWriter sw = new StringWriter();
103+
cmd.setErr(new PrintWriter(sw));
104+
101105
when(configService.isCurrentContextValid()).thenReturn(true);
102106
when(loginService.doAuthenticate(any(), anyBoolean())).thenReturn(false);
103107

108+
int code = cmd.execute("topic", "myTopic");
109+
assertEquals(1, code);
110+
}
111+
112+
@Test
113+
void shouldNotDeleteWhenFileNotFound() {
104114
CommandLine cmd = new CommandLine(delete);
105115
StringWriter sw = new StringWriter();
106116
cmd.setErr(new PrintWriter(sw));
107117

108-
int code = cmd.execute("topic", "myTopic");
109-
assertEquals(1, code);
118+
when(configService.isCurrentContextValid()).thenReturn(true);
119+
when(loginService.doAuthenticate(any(), anyBoolean())).thenReturn(true);
120+
121+
int code = cmd.execute("-f", "src/test/resources/topics/unknown.yml");
122+
assertEquals(2, code);
110123
}
111124

112125
@Test
@@ -120,9 +133,9 @@ void shouldNotDeleteByFileWhenYmlFileNotFound() {
120133
StringWriter sw = new StringWriter();
121134
cmd.setErr(new PrintWriter(sw));
122135

123-
int code = cmd.execute("-f", "topic");
136+
int code = cmd.execute("-f", "src/test/resources/topics-empty");
124137
assertEquals(2, code);
125-
assertTrue(sw.toString().contains("Could not find YAML or YML files in topic directory."));
138+
assertTrue(sw.toString().contains("Could not find YAML or YML files in topics-empty directory."));
126139
}
127140

128141
@Test
@@ -152,7 +165,7 @@ void shouldNotDeleteByFileWhenInvalidResources() {
152165
.when(resourceService)
153166
.validateAllowedResources(any(), any());
154167

155-
int code = cmd.execute("-f", "topic", "-n", "namespace");
168+
int code = cmd.execute("-f", "src/test/resources/topics/topic.yml", "-n", "namespace");
156169
assertEquals(2, code);
157170
assertTrue(sw.toString().contains("The server does not have resource type(s) Topic."));
158171
}
@@ -195,7 +208,7 @@ void shouldNotDeleteByFileWhenNamespaceMismatch() {
195208
StringWriter sw = new StringWriter();
196209
cmd.setErr(new PrintWriter(sw));
197210

198-
int code = cmd.execute("-f", "topic", "-n", "namespaceMismatch");
211+
int code = cmd.execute("-f", "src/test/resources/topics/topic.yml", "-n", "namespaceMismatch");
199212
assertEquals(2, code);
200213
assertTrue(sw.toString()
201214
.contains("Namespace mismatch between Kafkactl configuration and YAML resource(s): "
@@ -237,7 +250,7 @@ void shouldDeleteByFile() {
237250
StringWriter sw = new StringWriter();
238251
cmd.setOut(new PrintWriter(sw));
239252

240-
int code = cmd.execute("-f", "topic", "-n", "namespace");
253+
int code = cmd.execute("-f", "src/test/resources/topics/topic.yml", "-n", "namespace");
241254
assertEquals(0, code);
242255
}
243256

@@ -276,7 +289,7 @@ void shouldDeleteOneVersionByFile() {
276289
StringWriter sw = new StringWriter();
277290
cmd.setOut(new PrintWriter(sw));
278291

279-
int code = cmd.execute("-f", "topic", "-n", "namespace");
292+
int code = cmd.execute("-f", "src/test/resources/topics/topic.yml", "-n", "namespace");
280293
assertEquals(0, code);
281294
}
282295

@@ -364,7 +377,7 @@ void shouldNotDeleteByFileWhenInDryRunMode() {
364377
StringWriter sw = new StringWriter();
365378
cmd.setOut(new PrintWriter(sw));
366379

367-
int code = cmd.execute("-f", "topic", "--dry-run", "-n", "namespace");
380+
int code = cmd.execute("-f", "src/test/resources/topics/topic.yml", "--dry-run", "-n", "namespace");
368381
assertEquals(0, code);
369382
assertTrue(sw.toString().contains("Dry run execution."));
370383
}
@@ -402,7 +415,7 @@ void shouldNotDeleteByFileWhenFail() {
402415

403416
CommandLine cmd = new CommandLine(delete);
404417

405-
int code = cmd.execute("-f", "topic", "-n", "namespace");
418+
int code = cmd.execute("-f", "src/test/resources/topics/topic.yml", "-n", "namespace");
406419
assertEquals(1, code);
407420
}
408421

@@ -431,7 +444,7 @@ void shouldNotDeleteByFileWhenHttpClientResponseException() {
431444

432445
CommandLine cmd = new CommandLine(delete);
433446

434-
int code = cmd.execute("-f", "topic", "-n", "namespace");
447+
int code = cmd.execute("-f", "src/test/resources/topics/topic.yml", "-n", "namespace");
435448
assertEquals(1, code);
436449
verify(formatService).displayError(exception, cmd.getCommandSpec());
437450
}

src/test/java/com/michelin/kafkactl/service/ResourceServiceTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1435,7 +1435,17 @@ void shouldParse() {
14351435
}
14361436

14371437
@Test
1438-
void shouldNotParseNotFound() {
1438+
void shouldNotParseWhenFileDoesNotExist() {
1439+
CommandLine cmd = new CommandLine(new Kafkactl());
1440+
1441+
Optional<File> file = Optional.of(new File("src/test/resources/topics/unknown.yml"));
1442+
CommandLine.Model.CommandSpec spec = cmd.getCommandSpec();
1443+
1444+
assertThrows(ParameterException.class, () -> resourceService.parseResources(file, false, spec));
1445+
}
1446+
1447+
@Test
1448+
void shouldNotParseWhenNoYamlFilesInDirectory() {
14391449
CommandLine cmd = new CommandLine(new Kafkactl());
14401450

14411451
when(fileService.computeYamlFileList(any(), anyBoolean())).thenReturn(Collections.emptyList());

src/test/resources/topics-empty/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)