Skip to content

Commit 29e27f3

Browse files
committed
Update pet info command to use the context
1 parent 1b30182 commit 29e27f3

File tree

1 file changed

+6
-4
lines changed
  • spring-shell-samples/spring-shell-sample-petclinic/src/main/java/org/springframework/shell/samples/petclinic/commands

1 file changed

+6
-4
lines changed

spring-shell-samples/spring-shell-sample-petclinic/src/main/java/org/springframework/shell/samples/petclinic/commands/PetCommands.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,20 @@ public void listPets(CommandContext commandContext) {
5050
writer.flush();
5151
}
5252

53-
// TODO inject context and use output writer instead of System.out and System.err
5453
@Command(name = { "pets", "info" }, description = "Show detail about a given pet", group = "Pets",
5554
help = "Show the details about a given pet")
56-
public void showPet(@Option(longName = "petId", description = "The pet ID") int id) {
55+
public void showPet(@Option(longName = "petId", description = "The pet ID") int id, CommandContext commandContext) {
5756
try {
5857
Pet pet = this.jdbcClient.sql("SELECT * FROM PETS where id = " + id)
5958
.query(new DataClassRowMapper<>(Pet.class))
6059
.single();
61-
System.out.println(pet);
60+
commandContext.outputWriter().println(pet);
6261
}
6362
catch (EmptyResultDataAccessException exception) {
64-
System.err.println("No pet found with ID: " + id);
63+
commandContext.outputWriter().println("No pet found with ID: " + id);
64+
}
65+
finally {
66+
commandContext.outputWriter().flush();
6567
}
6668
}
6769

0 commit comments

Comments
 (0)