Skip to content

Commit

Permalink
feat(sdk): introducing sim.Container (#6022)
Browse files Browse the repository at this point in the history
The `sim.Container` resource allows running containers in the Wing Simulator.

```js
let c = new sim.Container(
  name: "http-echo",
  image: "hashicorp/http-echo",
  containerPort: 5678,
  args: ["-text=bang"],
);

test "send request" {
  http.get("http://localhost:{c.hostPort}");
}
```

There is also support for building containers from a local `Dockerfile`:

```js
new sim.Container(
  name: "my-service",
  image: "./my-service",
  containerPort: 8080,
);
```

Migrated the `redis` and `dynamodb` to use this new resource instead of starting/stopping their containers by hand.

### Motivation

Standardize how we manage containers in the Wing Simulator so we can improve the DX across the board and offer a nice UI in the Wing Console for this.

### Misc

Added a couple of utilities:

* `util.md5(dir)` - calculates the md5 hash on all the files in a directory (recursively).
* `util.glob(pattern)` - returns the files matching a glob pattern.

## Checklist

- [x] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted)
- [x] Description explains motivation and solution
- [x] Tests added (always)
- [x] Docs updated (only required for features)
- [x] Added `pr/e2e-full` label if this feature requires end-to-end testing

*By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
  • Loading branch information
eladb authored Mar 21, 2024
1 parent be50618 commit 885fb3a
Show file tree
Hide file tree
Showing 52 changed files with 1,704 additions and 135 deletions.
185 changes: 185 additions & 0 deletions docs/docs/04-standard-library/fs/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ new fs.Util();
| <code><a href="#@winglang/sdk.fs.Util.dirname">dirname</a></code> | Retrieve the name of the directory from a given file path. |
| <code><a href="#@winglang/sdk.fs.Util.exists">exists</a></code> | Check if the path exists. |
| <code><a href="#@winglang/sdk.fs.Util.extension">extension</a></code> | Extracts the extension (without the leading dot) from the path, if possible. |
| <code><a href="#@winglang/sdk.fs.Util.glob">glob</a></code> | Match files using the patterns the shell uses. |
| <code><a href="#@winglang/sdk.fs.Util.isDir">isDir</a></code> | Checks if the given path is a directory and exists. |
| <code><a href="#@winglang/sdk.fs.Util.join">join</a></code> | Join all arguments together and normalize the resulting path. |
| <code><a href="#@winglang/sdk.fs.Util.md5">md5</a></code> | Calculate an MD5 content hash of all the files that match a glob pattern. |
| <code><a href="#@winglang/sdk.fs.Util.metadata">metadata</a></code> | Gets the stats of the given path. |
| <code><a href="#@winglang/sdk.fs.Util.mkdir">mkdir</a></code> | Create a directory. |
| <code><a href="#@winglang/sdk.fs.Util.mkdtemp">mkdtemp</a></code> | Create a temporary directory. |
Expand Down Expand Up @@ -202,6 +204,34 @@ The path to get extension for.

---

##### `glob` <a name="glob" id="@winglang/sdk.fs.Util.glob"></a>

```wing
bring fs;
fs.glob(pattern: str, options?: GlobOptions);
```

Match files using the patterns the shell uses.

Built with the great `glob` package, based on https://www.npmjs.com/package/glob

###### `pattern`<sup>Required</sup> <a name="pattern" id="@winglang/sdk.fs.Util.glob.parameter.pattern"></a>

- *Type:* str

The pattern to match.

---

###### `options`<sup>Optional</sup> <a name="options" id="@winglang/sdk.fs.Util.glob.parameter.options"></a>

- *Type:* <a href="#@winglang/sdk.fs.GlobOptions">GlobOptions</a>

Glob options.

---

##### `isDir` <a name="isDir" id="@winglang/sdk.fs.Util.isDir"></a>

```wing
Expand Down Expand Up @@ -238,6 +268,32 @@ The array of path need to join.

---

##### `md5` <a name="md5" id="@winglang/sdk.fs.Util.md5"></a>

```wing
bring fs;
fs.md5(dir: str, globPattern?: str);
```

Calculate an MD5 content hash of all the files that match a glob pattern.

###### `dir`<sup>Required</sup> <a name="dir" id="@winglang/sdk.fs.Util.md5.parameter.dir"></a>

- *Type:* str

The root directory.

---

###### `globPattern`<sup>Optional</sup> <a name="globPattern" id="@winglang/sdk.fs.Util.md5.parameter.globPattern"></a>

- *Type:* str

The glob pattern to match (defaults to all files and subdirectories).

---

##### `metadata` <a name="metadata" id="@winglang/sdk.fs.Util.metadata"></a>

```wing
Expand Down Expand Up @@ -693,6 +749,135 @@ The YANL objects to be dumped.

## Structs <a name="Structs" id="Structs"></a>

### GlobOptions <a name="GlobOptions" id="@winglang/sdk.fs.GlobOptions"></a>

Options for `glob`, based on https://www.npmjs.com/package/glob.

#### Initializer <a name="Initializer" id="@winglang/sdk.fs.GlobOptions.Initializer"></a>

```wing
bring fs;
let GlobOptions = fs.GlobOptions{ ... };
```

#### Properties <a name="Properties" id="Properties"></a>

| **Name** | **Type** | **Description** |
| --- | --- | --- |
| <code><a href="#@winglang/sdk.fs.GlobOptions.property.absolute">absolute</a></code> | <code>bool</code> | Set to `true` to always receive absolute paths for matched files. |
| <code><a href="#@winglang/sdk.fs.GlobOptions.property.cwd">cwd</a></code> | <code>str</code> | The current working directory in which to search. |
| <code><a href="#@winglang/sdk.fs.GlobOptions.property.dot">dot</a></code> | <code>bool</code> | Include `.dot` files in normal matches and globstar matches. Note that an explicit dot in a portion of the pattern will always match dot files. |
| <code><a href="#@winglang/sdk.fs.GlobOptions.property.follow">follow</a></code> | <code>bool</code> | Follow symlinked directories when expanding `**` patterns. |
| <code><a href="#@winglang/sdk.fs.GlobOptions.property.ignore">ignore</a></code> | <code>MutArray&lt;str&gt;</code> | An array of glob patterns to exclude from matches. |
| <code><a href="#@winglang/sdk.fs.GlobOptions.property.maxDepth">maxDepth</a></code> | <code>num</code> | Specify a number to limit the depth of the directory traversal to this many levels below the cwd. |
| <code><a href="#@winglang/sdk.fs.GlobOptions.property.nodir">nodir</a></code> | <code>bool</code> | Do not match directories, only files. |

---

##### `absolute`<sup>Optional</sup> <a name="absolute" id="@winglang/sdk.fs.GlobOptions.property.absolute"></a>

```wing
absolute: bool;
```

- *Type:* bool
- *Default:* false

Set to `true` to always receive absolute paths for matched files.

Set to `false` to always
receive relative paths for matched files.

---

##### `cwd`<sup>Optional</sup> <a name="cwd" id="@winglang/sdk.fs.GlobOptions.property.cwd"></a>

```wing
cwd: str;
```

- *Type:* str
- *Default:* process.cwd()

The current working directory in which to search.

---

##### `dot`<sup>Optional</sup> <a name="dot" id="@winglang/sdk.fs.GlobOptions.property.dot"></a>

```wing
dot: bool;
```

- *Type:* bool
- *Default:* false

Include `.dot` files in normal matches and globstar matches. Note that an explicit dot in a portion of the pattern will always match dot files.

---

##### `follow`<sup>Optional</sup> <a name="follow" id="@winglang/sdk.fs.GlobOptions.property.follow"></a>

```wing
follow: bool;
```

- *Type:* bool
- *Default:* false

Follow symlinked directories when expanding `**` patterns.

This can result in a lot of
duplicate references in the presence of cyclic links, and make performance quite bad.

---

##### `ignore`<sup>Optional</sup> <a name="ignore" id="@winglang/sdk.fs.GlobOptions.property.ignore"></a>

```wing
ignore: MutArray<str>;
```

- *Type:* MutArray&lt;str&gt;
- *Default:* []

An array of glob patterns to exclude from matches.

To ignore all children within a directory,
as well as the entry itself, append '/**' to the ignore pattern.

---

##### `maxDepth`<sup>Optional</sup> <a name="maxDepth" id="@winglang/sdk.fs.GlobOptions.property.maxDepth"></a>

```wing
maxDepth: num;
```

- *Type:* num
- *Default:* no limit

Specify a number to limit the depth of the directory traversal to this many levels below the cwd.

---

##### `nodir`<sup>Optional</sup> <a name="nodir" id="@winglang/sdk.fs.GlobOptions.property.nodir"></a>

```wing
nodir: bool;
```

- *Type:* bool
- *Default:* false

Do not match directories, only files.

(Note: to match only directories, put a `/` at the end of
the pattern.)

---

### Metadata <a name="Metadata" id="@winglang/sdk.fs.Metadata"></a>

Metadata of a file system object.
Expand Down
Loading

0 comments on commit 885fb3a

Please sign in to comment.