Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Commit

Permalink
Merge pull request #509 from cloudflare/feat-kv-additions
Browse files Browse the repository at this point in the history
feat: kv additions
  • Loading branch information
ashleymichal authored Sep 20, 2019
2 parents f5a3b86 + d6f0140 commit b7ff500
Show file tree
Hide file tree
Showing 32 changed files with 1,057 additions and 216 deletions.
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ walkdir = "2.2.9"
percent-encoding = "1.0.1"
http = "0.1.1"
regex = "1"
sha2 = "0.8.0"
data-encoding = "2.1.2"

[dev-dependencies]
assert_cmd = "0.11.1"
Expand Down
67 changes: 51 additions & 16 deletions docs/content/kv_commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ the `kv:namespace` subcommand.

The `kv:namespace` subcommand takes as a new binding name as an argument. It will create a Worker KV namespace
whose title is a concatenation of your Worker's name (from `wrangler.toml`) and the binding name you provide:

```console
$ wrangler kv:namespace create "MY_KV"
🌀 Creating namespace with title "worker-MY_KV"
Expand All @@ -28,19 +29,25 @@ kv-namespaces = [
{ binding = "MY_KV", id = "e29b263ab50e42ce9b637fa8370175e8" }
]
```

Make sure to add the `kv-namespaces` output above to your `wrangler.toml`. You can now
access it from a Worker with code like:

```js
let value = await MY_KV.get("my-key");
```

The full KV API for Workers can be found [here](https://developers.cloudflare.com/workers/reference/storage/).

To put a value to your KV namespace via Wrangler, use the `kv:key put` subcommand.

```console
$ wrangler kv:key put --binding=MY_KV "key" "value"
✨ Success
```

You can also specify which namespace to put your key-value pair into using `--namespace-id` instead of `--binding`:

```console
$ wrangler kv:key put --namespace-id=e29b263ab50e42ce9b637fa8370175e8 "key" "value"
✨ Success
Expand Down Expand Up @@ -78,7 +85,7 @@ Most `kv` commands require you to specify a namespace. A namespace can be specif

1. With a `--binding`:
```sh
wrangler kv:key get --binding=KV "my key"
wrangler kv:key get --binding=MY_KV "my key"
```
1. With a `--namespace_id`:
```sh
Expand All @@ -95,22 +102,20 @@ route = "staging.example.com/*"
workers_dev = false
kv-namespaces = [
{ binding = "KV", id = "06779da6940b431db6e566b4846d64db" }
{ binding = "MY_KV", id = "06779da6940b431db6e566b4846d64db" }
]
[env.production]
route = "example.com/*"
kv-namespaces = [
{ binding = "KV", id = "07bc1f3d1f2a4fd8a45a7e026e2681c6" }
{ binding = "MY_KV", id = "07bc1f3d1f2a4fd8a45a7e026e2681c6" }
]
```
With the wrangler.toml above, you can specify `--env production` when you want to perform a KV action on
the namespace `KV` under `env.production`. For example, with the wrangler.toml above, you can get a value
out of a production KV instance with:
With the wrangler.toml above, you can specify `--env production` when you want to perform a KV action on the namespace `MY_KV` under `env.production`. For example, with the wrangler.toml above, you can get a value out of a production KV instance with:
```console
wrangler kv:key get --binding "KV" --env=production "my key"
wrangler kv:key get --binding "MY_KV" --env=production "my key"
```
To learn more about environments, check out the [environments documentation](./environments.md).
Expand Down Expand Up @@ -170,7 +175,7 @@ Takes an optional `--env` [environment](./environments.md) argument.
#### Usage
```console
$ wrangler kv:namespace delete --binding=KV
$ wrangler kv:namespace delete --binding=MY_KV
Are you sure you want to delete namespace f7b02e7fc70443149ac906dd81ec1791? [y/n]
yes
🌀 Deleting namespace f7b02e7fc70443149ac906dd81ec1791
Expand All @@ -195,18 +200,18 @@ Optional params include:
#### Usage
```console
$ wrangler kv:key put --binding=KV "key" "value" --ttl=10000
$ wrangler kv:key put --binding=MY_KV "key" "value" --ttl=10000
✨ Success
```
```console
$ wrangler kv:key put --binding=KV "key" value.txt --path
$ wrangler kv:key put --binding=MY_KV "key" value.txt --path
✨ Success
```
### `list`
Outputs a list of all keys in a given namespace.
Outputs a list of all keys in a given namespace.
Requires `--binding` or `--namespace-id` argument.
Expand All @@ -220,7 +225,7 @@ Optional params include:
The example below uses the `jq` command line tool to pretty-print output.
```console
$ wrangler kv:key list --binding=KV --prefix="public" | jq '.'
$ wrangler kv:key list --binding=MY_KV --prefix="public" | jq '.'
[
{
"name": "public_key"
Expand All @@ -243,7 +248,7 @@ Takes an optional `--env` [environment](./environments.md) argument.
#### Usage
```console
$ wrangler kv:key get --binding=KV "key"
$ wrangler kv:key get --binding=MY_KV "key"
value
```
Expand All @@ -258,7 +263,7 @@ Takes an optional `--env` [environment](./environments.md) argument.
#### Usage
```console
$ wrangler kv:key delete --binding=KV "key"
$ wrangler kv:key delete --binding=MY_KV "key"
Are you sure you want to delete key "key"? [y/n]
yes
🌀 Deleting key "key"
Expand Down Expand Up @@ -300,7 +305,7 @@ The `put` command also takes an optional `--env` [environment](./environments.md
#### Usage
```console
$ wrangler kv:bulk put --binding=KV allthethingsupload.json
$ wrangler kv:bulk put --binding=MY_KV allthethingsupload.json
✨ Success
```
Expand All @@ -326,8 +331,38 @@ The `delete` command also takes an optional `--env` [environment](./environments
#### Usage
```console
$ wrangler kv:bulk delete --binding=KV allthethingsdelete.json
$ wrangler kv:bulk delete --binding=MY_KV allthethingsdelete.json
Are you sure you want to delete all keys in allthethingsdelete.json? [y/n]
yes
✨ Success
```
## `kv:bucket`
### `upload`
Walks the given directory and runs a bulk upload, using the path to an asset as its `key` and the asset as its `value`.
#### Usage
```console
$ wrangler kv:bucket upload --binding MY_KV ./public
```
```console
$ wrangler kv:bucket upload --namespace-id f7b02e7fc70443149ac906dd81ec1791 ./public
```
### `delete`
Walks the given directory and runs a bulk delete, using the paths to assets as the `key`s to delete.
#### Usage
```console
$ wrangler kv:bucket delete --binding MY_KV
```
```console
$ wrangler kv:bucket delete --namespace-id f7b02e7fc70443149ac906dd81ec1791
```
16 changes: 8 additions & 8 deletions src/commands/build/wranglerjs/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ use crate::commands::build::wranglerjs::output::WranglerjsOutput;

// Directory where we should write the {Bundle}. It represents the built
// artifact.
const BUNDLE_OUT: &str = "./worker";
const BUNDLE_OUT: &str = "worker";
pub struct Bundle {
out: String,
out: PathBuf,
}

// We call a {Bundle} the output of a {Bundler}; representing what {Webpack}
// produces.
impl Bundle {
pub fn new() -> Bundle {
pub fn new(build_dir: &PathBuf) -> Bundle {
Bundle {
out: BUNDLE_OUT.to_string(),
out: build_dir.join(BUNDLE_OUT),
}
}

#[cfg(test)]
fn new_at(out: String) -> Bundle {
fn new_at(out: PathBuf) -> Bundle {
Bundle { out }
}

Expand Down Expand Up @@ -81,15 +81,15 @@ impl Bundle {
mod tests {
use super::*;

fn create_temp_dir(name: &str) -> String {
fn create_temp_dir(name: &str) -> PathBuf {
let mut dir = env::temp_dir();
dir.push(name);
if dir.exists() {
fs::remove_dir_all(&dir).unwrap();
}
fs::create_dir(&dir).expect("could not create temp dir");

dir.to_str().unwrap().to_string()
dir
}

#[test]
Expand Down Expand Up @@ -137,7 +137,7 @@ mod tests {
assert!(wranglerjs_output.get_errors() == "a\nb");
}

fn cleanup(name: String) {
fn cleanup(name: PathBuf) {
let current_dir = env::current_dir().unwrap();
let path = Path::new(&current_dir).join(name);
fs::remove_dir_all(path).unwrap();
Expand Down
44 changes: 35 additions & 9 deletions src/commands/build/wranglerjs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod output;

use crate::commands::build::watch::wait_for_changes;
use crate::commands::build::watch::COOLDOWN_PERIOD;
use crate::commands::generate::run_generate;

use crate::commands::publish::package::Package;
use crate::install;
Expand Down Expand Up @@ -127,8 +128,13 @@ fn setup_build(target: &Target) -> Result<(Command, PathBuf, Bundle), failure::E
env_dep_installed(tool)?;
}

let current_dir = env::current_dir()?;
run_npm_install(current_dir).expect("could not run `npm install`");
let build_dir = target.build_dir()?;

if target.site.is_some() {
scaffold_site_worker(&target)?;
}

run_npm_install(&build_dir).expect("could not run `npm install`");

let node = which::which("node").unwrap();
let mut command = Command::new(node);
Expand All @@ -149,7 +155,7 @@ fn setup_build(target: &Target) -> Result<(Command, PathBuf, Bundle), failure::E
temp_file.clone().to_str().unwrap().to_string()
));

let bundle = Bundle::new();
let bundle = Bundle::new(&build_dir);

command.arg(format!("--wasm-binding={}", bundle.get_wasm_binding()));

Expand All @@ -164,10 +170,9 @@ fn setup_build(target: &Target) -> Result<(Command, PathBuf, Bundle), failure::E
// {package.json} file and pass it to {wranglerjs}.
// https://github.com/cloudflare/wrangler/issues/98
if !bundle.has_webpack_config(&webpack_config_path) {
let package = Package::new("./")?;
let current_dir = env::current_dir()?;
let package_main = current_dir
.join(package.main()?)
let package = Package::new(&build_dir)?;
let package_main = build_dir
.join(package.main(&build_dir)?)
.to_str()
.unwrap()
.to_string();
Expand All @@ -183,9 +188,30 @@ fn setup_build(target: &Target) -> Result<(Command, PathBuf, Bundle), failure::E
Ok((command, temp_file, bundle))
}

pub fn scaffold_site_worker(target: &Target) -> Result<(), failure::Error> {
let build_dir = target.build_dir()?;
// TODO: this is a placeholder template. Replace with The Real Thing on launch.
let template = "https://github.com/ashleymichal/glowing-palm-tree";

if !Path::new(&build_dir).exists() {
// TODO: use site.entry_point instead of build_dir explicitly.
run_generate(
build_dir.file_name().unwrap().to_str().unwrap(),
template,
&target.target_type,
)?;

// This step is to prevent having a git repo within a git repo after
// generating the scaffold into an existing project.
fs::remove_dir_all(&build_dir.join(".git"))?;
}

Ok(())
}

// Run {npm install} in the specified directory. Skips the install if a
// {node_modules} is found in the directory.
fn run_npm_install(dir: PathBuf) -> Result<(), failure::Error> {
fn run_npm_install(dir: &PathBuf) -> Result<(), failure::Error> {
let flock_path = dir.join(&".install.lock");
let flock = File::create(&flock_path)?;
// avoid running multiple {npm install} at the same time (eg. in tests)
Expand Down Expand Up @@ -263,7 +289,7 @@ fn install() -> Result<PathBuf, failure::Error> {
wranglerjs_path.path()
};

run_npm_install(wranglerjs_path.clone()).expect("could not install wranglerjs dependencies");
run_npm_install(&wranglerjs_path.clone()).expect("could not install wranglerjs dependencies");
Ok(wranglerjs_path)
}

Expand Down
Loading

0 comments on commit b7ff500

Please sign in to comment.