Skip to content

Commit

Permalink
Clarify docs as requested and make --delete work with recipe json-s a…
Browse files Browse the repository at this point in the history
…s well
  • Loading branch information
martinpaljak committed Oct 31, 2023
1 parent 0bd29b6 commit 2b1ecc6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ abstract class CommandLineInterface {

final static protected OptionSpec<File> OPT_UPLOAD = parser.accepts("upload", "Upload CAP or recipe to Fidesmo").withRequiredArg().ofType(File.class).describedAs(".cap/.json file");
final static protected OptionSpec<Void> OPT_LIST_APPLETS = parser.accepts("list-applets", "List applets at Fidesmo");
final static protected OptionSpec<String> OPT_DELETE = parser.acceptsAll(List.of("delete", "delete-applet"), "Deletes applets and recipes at Fidesmo").withRequiredArg().describedAs("ID");
final static protected OptionSpec<String> OPT_DELETE = parser.acceptsAll(List.of("delete", "delete-applet"), "Deletes applets and recipes at Fidesmo").withRequiredArg().describedAs("file/hash/recipe");

final static protected OptionSpec<Void> OPT_CARD_APPS = parser.accepts("card-apps", "List apps on the card");
final static protected OptionSpec<Void> OPT_CARD_INFO = parser.accepts("card-info", "Show info about the card");
Expand Down
32 changes: 21 additions & 11 deletions tool/src/main/java/com/fidesmo/fdsm/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,23 +126,33 @@ public static void main(String[] argv) {
// Delete a specific applet or recipe
if (args.has(OPT_DELETE)) {
String id = args.valueOf(OPT_DELETE);

Path path = Paths.get(id);
// DWIM: recipe
final URI toDelete;
if (id.toLowerCase().matches("[a-f0-9]{64}")) {
toDelete = client.getURI(FidesmoApiClient.CAPFILES_ID_URL, getAppId(), id);
} else if (Files.exists(Paths.get(id))) {
// DWIM: capfile
CAPFile tmp = CAPFile.fromBytes(Files.readAllBytes(Paths.get(id)));
id = HexUtils.bin2hex(tmp.getLoadFileDataHash("SHA-256"));
toDelete = client.getURI(FidesmoApiClient.CAPFILES_ID_URL, getAppId(), id);
} else if (Files.exists(path)) {
// DWIM: capfile or recipe .json; This makes it the opposite of the upload operation
File f = path.toFile();
String extension = FilenameUtils.getExtension(f.getName());
if (extension.equalsIgnoreCase("json")) {
id = FilenameUtils.getBaseName(f.getName());
toDelete = client.getURI(FidesmoApiClient.SERVICE_RECIPE_URL, getAppId(), id);
} else if (extension.equalsIgnoreCase("cap")) {
CAPFile tmp = CAPFile.fromBytes(Files.readAllBytes(path));
id = HexUtils.bin2hex(tmp.getLoadFileDataHash("SHA-256"));
toDelete = client.getURI(FidesmoApiClient.CAPFILES_ID_URL, getAppId(), id);
} else {
throw new IllegalArgumentException("Only .cap and .json files are supported: " + id);
}
} else {
// Maybe it is a recipe
// Maybe it is a recipe name ?
ArrayNode recipes = (ArrayNode) client.rpc(client.getURI(FidesmoApiClient.RECIPE_SERVICES_URL, getAppId()));
String finalId = id;
id = StreamSupport.stream(recipes.spliterator(), false)
.map(r-> r.asText())
.map(JsonNode::asText)
.filter(r -> r.equals(finalId))
.findFirst().orElseThrow( () -> new IllegalArgumentException("Not a recipe nor SHA-256: " + finalId));
.findFirst().orElseThrow(() -> new IllegalArgumentException("Not a recipe nor SHA-256: " + finalId));
toDelete = client.getURI(FidesmoApiClient.SERVICE_RECIPE_URL, getAppId(), id);
}

Expand Down Expand Up @@ -355,7 +365,7 @@ public static void main(String[] argv) {
}

final ServiceDeliverySession cardSession = ServiceDeliverySession.getInstance(
() -> bibo, fidesmoCard, client, delivery.getAppId().get(), delivery.getService(), formHandler
() -> bibo, fidesmoCard, client, delivery.getAppId().get(), delivery.getService(), formHandler
);

if (args.has(OPT_TIMEOUT))
Expand All @@ -372,7 +382,7 @@ public static void main(String[] argv) {
}
// --run always exists
}

if (args.has(OPT_CARD_APPS)) {
FidesmoCard fidesmoCard = requireDevice(fidesmoMetadata);
fidesmoCard.ensureBatched(bibo, client, optTimeout, ignoreImplicitBatching, getCommandLineFormHandler());
Expand Down

0 comments on commit 2b1ecc6

Please sign in to comment.