Skip to content

Commit

Permalink
docs: Add WASM pages. (#1291)
Browse files Browse the repository at this point in the history
* Add new wasm guide.

* Add ext docs.

* Add config section.

* Update proto.

* Update completions.
  • Loading branch information
milesj authored Jan 26, 2024
1 parent c273695 commit 2802690
Show file tree
Hide file tree
Showing 9 changed files with 720 additions and 339 deletions.
57 changes: 56 additions & 1 deletion website/docs/commands/completions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,69 @@
title: completions
---

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

The `moon completions` command will generate moon command and argument completions for your current
shell. This command will write to stdout, which can then be redirected to a file of your choice.

```shell
$ moon completions > ~/.bash_completion.d/moon.sh
$ moon completions > ./path/to/write/to
```

### Options

- `--shell` - Shell to explicitly generate for. Accepts "bash", "elvish", "fish", "powershell", or
"zsh".

### Examples

<Tabs
groupId="comp"
defaultValue="bash"
values={[
{ label: 'Bash', value: 'bash' },
{ label: 'Fish', value: 'fish' },
{ label: 'Zsh', value: 'zsh' },
]}
>
<TabItem value="bash">

If using [bash-completion](https://github.com/scop/bash-completion).

```shell
$ moon completions > ~/.bash_completion.d/moon.sh
```

Otherwise write the file to a common location, and source it in your profile.

```shell
$ moon completions > ~/.bash_completions/moon.sh

# In your profile
source ~/.bash_completions/moon.sh
```

</TabItem>
<TabItem value="fish">

Write the file to Fish's completions directory.

```shell
$ moon completions > ~/.config/fish/completions/moon.fish
```

</TabItem>
<TabItem value="zsh">

If using [oh-my-zsh](https://ohmyz.sh/) (the `_` prefix is required),

```shell
$ moon completions > ~/.oh-my-zsh/completions/_moon

# Reload shell (or restart terminal)
$ omz reload
```

</TabItem>
</Tabs>
3 changes: 2 additions & 1 deletion website/docs/config/workspace.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ experiments:

A mapping of extensions that can be downloaded and executed with the [`moon ext`](../commands/ext)
command. An extension is a WASM plugin, and the location of the WASM file must be defined with the
`plugin` field, which supports a relative file path or a secure URL.
`plugin` field, which requires a
[plugin locator string](../guides/wasm-plugins#configuring-plugin-locations).

```yaml title=".moon/workspace.yml" {2-5}
extensions:
Expand Down
110 changes: 108 additions & 2 deletions website/docs/guides/extensions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,113 @@ $ moon ext download --\

## Creating an extension

Coming soon! If you're eager to get started, take a look at our [extensions repository][repo] for
examples.
Refer to our [official WASM guide](./wasm-plugins) for more information on how our WASM plugins
work, critical concepts to know, how to create a plugin, and more. Once you have a good
understanding, you may continue this specific guide.

:::note

Refer to our [moonrepo/moon-extensions][repo] repository for in-depth examples.

:::

### Implementing execution

Extensions support a single plugin function, `execute_extension`, which is called by the
[`moon ext`](../commands/ext) command to execute the extension. This is where all your business
logic will reside.

```rust
use extism_pdk::*;
use moon_pdk::{*, extension::*};
#[host_fn]
extern "ExtismHost" {
fn host_log(input: Json<HostLogInput>);
}
#[plugin_fn]
pub fn execute_extension(Json(input): Json<ExecuteExtensionInput>) -> FnResult<()> {
host_log!(stdout, "Executing extension!");
Ok(())
}
```

### Supporting arguments

Most extensions will require arguments, as it provides a mechanism for users to pass information
into the WASM runtime. To parse arguments, we provide the
[`Args`](https://docs.rs/clap/latest/clap/trait.Args.html) trait/macro from the
[clap](https://crates.io/crates/clap) crate. Refer to their
[official documentation on usage](https://docs.rs/clap/latest/clap/_derive/index.html) (we don't
support everything).

```rust
use moon_pdk::args::*;
#[derive(Args)]
pub struct ExampleExtensionArgs {
// --url, -u
#[arg(long, short = 'u', required = true)]
pub url: String,
}
```

Once your struct has been defined, you can parse the provided input arguments using the
[`parse_args`](https://docs.rs/moon_pdk/latest/moon_pdk/args/fn.parse_args.html) function.

```rust
#[plugin_fn]
pub fn execute_extension(Json(input): Json<ExecuteExtensionInput>) -> FnResult<()> {
let args = parse_args::<ExampleExtensionArgs>(&input.args)?;
args.url; // --url
Ok(())
}
```

### Supporting configuration

Users can configure [extensions](../config/workspace#extensions) with additional settings in
[`.moon/workspace.yml`](../config/workspace). Do note that settings should be in camelCase for them
to be parsed correctly!

```yaml title=".moon/workspace.yml"
extensions:
example:
plugin: 'source:./path/to/example.wasm'
someSetting: 'abc'
anotherSetting: 123
```

In the plugin, we can map these settings (excluding `plugin`) into a struct. The `Default` trait
must be implemented to handle situations where settings were not configured, or some are missing.

```rust
config_struct!(
#[derive(Default)]
pub struct ExampleExtensionConfig {
pub some_setting: String,
pub another_setting: u32,
}
);
```

Once your struct has been defined, you can access the configuration using the
[`get_extension_config`](https://docs.rs/moon_pdk/latest/moon_pdk/extension/fn.get_extension_config.html)
function

```rust
#[plugin_fn]
pub fn execute_extension(Json(input): Json<ExecuteExtensionInput>) -> FnResult<()> {
let config = get_extension_config::<ExampleExtensionConfig>()?;
config.another_setting; // 123
Ok(())
}
```

[repo]: https://github.com/moonrepo/moon-extensions
Loading

0 comments on commit 2802690

Please sign in to comment.