-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (cli): Add fetch CLI command (with documentation)
- Loading branch information
Showing
11 changed files
with
177 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<!-- | ||
SPDX-License-Identifier: Apache-2.0 | ||
Copyright 2024 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. | ||
--> | ||
|
||
# Fetch | ||
|
||
`fetch` fetches a _Resource_ from an _URL_ and outputs its content. You can therefore use this similarly to [curl](https://curl.se/) or [httpie](https://httpie.io/cli) or [wget](https://en.wikipedia.org/wiki/Wget). (If you want to see the _Media Type,_ use [`info detect`](../info/index.md#detect).) | ||
|
||
This is different from [`get`](../get/index.md), which shows _Things_ given an _IRI. | ||
(However the `--load` option of `get` internally does a `fetch`, and supports the same schemes.) | ||
|
||
Enola supports the [URI schemes](https://en.wikipedia.org/wiki/List_of_URI_schemes) which are documented below. | ||
These are supported everywhere; including in `fetch`, `--load`, and elsewhere. | ||
|
||
## HTTP | ||
|
||
This will fetch <https://www.vorburger.ch/hello.md>: _(Note how for security reasons we have to explicitly permit it.)_ | ||
|
||
```bash cd ../.././.. | ||
$ ./enola fetch --http-scheme https://www.vorburger.ch/hello.md | ||
... | ||
``` | ||
|
||
Enola locally caches HTTP responses on the filesystem. | ||
|
||
## Files | ||
|
||
We can do `cat`-like equivalent of local files using [the `file:` scheme](https://en.wikipedia.org/wiki/File_URI_scheme): | ||
|
||
```bash cd ../.././.. | ||
$ echo "hello" >/tmp/hi.txt && ./enola fetch file:///tmp/hi.txt | ||
... | ||
``` | ||
|
||
We can omit the `file:` scheme and use absolute or relative paths, | ||
because (in the CLI) the current working directory is implicitly [the _base URI_ | ||
used to resolve URI references](https://en.wikipedia.org/wiki/Uniform_Resource_Identifier#URI_references): | ||
|
||
```bash cd ../.././.. | ||
$ ./enola fetch /tmp/hi.txt | ||
... | ||
``` | ||
|
||
## Classpath | ||
|
||
```bash cd ../.././.. | ||
$ ./enola fetch classpath:/VERSION | ||
... | ||
``` | ||
|
||
## Data | ||
|
||
Enola [supports (RFC 2397) `data:` URLs](https://en.m.wikipedia.org/wiki/Data_URI_scheme): | ||
|
||
```bash cd ../.././.. | ||
$ ./enola fetch "data:application/json;charset=UTF-8,%7B%22key%22%3A+%22value%22%7D" | ||
... | ||
``` | ||
|
||
## Empty | ||
|
||
`empty:` is a (non-standard) URL scheme in Enola for "no content" (as an alternative to `data:,`): | ||
|
||
```bash cd ../.././.. | ||
$ ./enola fetch empty:/ | ||
... | ||
``` | ||
|
||
## Screencast | ||
|
||
![Demo](script.svg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Copyright 2024 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.iri.URIs; | ||
|
||
import picocli.CommandLine; | ||
|
||
import java.net.URI; | ||
import java.nio.file.Paths; | ||
|
||
@CommandLine.Command( | ||
name = "fetch", | ||
description = {"Fetches (I/O) a Resource", "See also the related 'info detect' command."}) | ||
public class FetchCommand extends CommandWithResourceProvider { | ||
|
||
@CommandLine.Parameters(index = "0", paramLabel = "URL", description = "URL to fetch") | ||
String url; | ||
|
||
@Override | ||
public Integer call() throws Exception { | ||
super.run(); | ||
var uri = URI.create(url); | ||
try (var ctx = TLC.open().push(URIs.ContextKeys.BASE, Paths.get("").toUri())) { | ||
var resource = rp.getResource(uri); | ||
if (resource == null) { | ||
System.err.println(uri.getScheme() + " scheme unknown; try: enola fetch --help"); | ||
return 1; | ||
|
||
} else { | ||
resource.byteSource().copyTo(System.out); | ||
return 0; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters