Skip to content

Commit

Permalink
feat(core): Introduce new CLI: enola info digest
Browse files Browse the repository at this point in the history
  • Loading branch information
vorburger committed Jan 4, 2025
1 parent cf2305d commit c2c5487
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/use/fetch/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ $ ./enola fetch --http-scheme "https://www.vorburger.ch/hello.md?integrity=z8Vsn
...
```

[`enola info digest`](../info/index.md#digest) is an alternative for obtaining the `?integrity=...` value.

Note that while [Multihash](https://www.multiformats.io/multihash/) defines codes for [various hash functions](https://github.com/multiformats/multicodec/blob/master/table.csv),
Enola (currently) [intentionally](https://github.com/google/guava/issues/5990#issuecomment-2571350434) only actually supports `sha2-256` & `sha2-512`.

Expand Down
18 changes: 17 additions & 1 deletion docs/use/info/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ $ ./enola info detect "picasso.thing.yaml?mediaType=application/json"
...
```

[`fetch`](../fetch/index.md) is another command to "get the bytes at" an URL.
BTW: [`fetch`](../fetch/index.md) is the command to "get the bytes at" an URL.

## Metadata

Expand All @@ -77,6 +77,22 @@ $ ./enola info metadata --load=test/metadata-label-property.ttl https://example.

[Metadata](../../concepts/metadata.md) explains what this is about.

## Digest

```bash cd ../.././..
$ ./enola info digest --help
...
```

So for example:

```bash cd ../.././..
$ ./enola info digest --http-scheme https://www.google.com
...
```

This _digest_ can be used e.g. in [`?integrity=...` of `fetch`](../fetch/index.md#integrity).

## Screencast

![Demo](script.svg)
2 changes: 2 additions & 0 deletions java/dev/enola/cli/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ java_binary(
),
main_class = "dev.enola.cli.EnolaCLI",
deps = [
"@enola_maven//:com_github_multiformats_java_multibase",
"@enola_maven//:com_github_multiformats_java_multihash",
"//java/dev/enola",
# TODO Remove most of these direct deps, through new Enola API...
"//java/dev/enola/common",
Expand Down
6 changes: 3 additions & 3 deletions java/dev/enola/cli/DetectCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ public class DetectCommand extends CommandWithResourceProvider {

@Spec CommandSpec spec;

@CommandLine.Parameters(index = "0", paramLabel = "iri", description = "IRI")
String iri;
@CommandLine.Parameters(index = "0", paramLabel = "url", description = "URL")
String url;

@Override
public Integer call() throws Exception {
super.run();
var uri = URIs.parse(iri);
var uri = URIs.parse(url);
try (var ctx = TLC.open().push(URIs.ContextKeys.BASE, Paths.get("").toUri())) {
var resource = rp.getResource(uri);
if (resource == null) {
Expand Down
59 changes: 59 additions & 0 deletions java/dev/enola/cli/DigestCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2025 The Enola <https://enola.dev> Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dev.enola.cli;

import dev.enola.common.context.TLC;
import dev.enola.common.io.hashbrown.Multihashes;
import dev.enola.common.io.hashbrown.ResourceHasher;
import dev.enola.common.io.iri.URIs;

import io.ipfs.multibase.Multibase;
import io.ipfs.multihash.Multihash;

import picocli.CommandLine;

import java.nio.file.Paths;

@CommandLine.Command(
name = "digest",
description =
"Generates a Message Authentication Code (MAC; also Message Integrity Code, MIC)"
+ " for a given URL.\n"
+ "See also the related ?integrity=... argument of the 'fetch' command.")
public class DigestCommand extends CommandWithResourceProvider {

@CommandLine.Spec CommandLine.Model.CommandSpec spec;

@CommandLine.Parameters(index = "0", paramLabel = "url", description = "URL")
String url;

@Override
public Integer call() throws Exception {
super.run();
var pw = spec.commandLine().getOut();

var uri = URIs.parse(url);
try (var ctx = TLC.open().push(URIs.ContextKeys.BASE, Paths.get("").toUri())) {
var resource = rp.getResource(uri);

var multihash = new ResourceHasher().hash(resource, Multihash.Type.sha2_512);
pw.println(Multihashes.toString(multihash, Multibase.Base.Base64));
}
return 0;
}
}
3 changes: 2 additions & 1 deletion java/dev/enola/cli/InfoCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
name = "info",
description = "Provides various information",
subcommands = {
MetadataCommand.class,
ExtensionsInfoCommand.class,
MediaTypeInfoCommand.class,
DetectCommand.class,
MetadataCommand.class
DigestCommand.class
})
public class InfoCommand {}

0 comments on commit c2c5487

Please sign in to comment.