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

Print out namespaces list as json by default #541

Merged
merged 3 commits into from
Sep 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions docs/content/kv_commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,20 @@ yes
Outputs a list of all KV namespaces associated with your account id.

#### Usage
The example below uses the `jq` command line tool to pretty-print output.

```sh
$ wrangler kv:namespace list
🌀 Retrieving namespaces
✨ Success:
+---------------+----------------------------------+
| TITLE | ID |
+---------------+----------------------------------+
| New Namespace | f7b02e7fc70443149ac906dd81ec1791 |
+---------------+----------------------------------+
$ wrangler kv:namespace list | jq '.'
[
{
"id": "06779da6940b431db6e566b4846d64db",
"title": "TEST_NAMESPACE"
},
{
"id": "32ac1b3c2ed34ed3b397268817dea9ea",
"title": "STATIC_CONTENT"
}
]
```

## `kv:key`
Expand Down Expand Up @@ -168,10 +172,9 @@ Optional params include
1. `--prefix`: A prefix to filter listed keys.

#### Usage
The example below uses Python's JSON pretty-printing command line tool to pretty-print output.

The example below uses the `jq` command line tool to pretty-print output.
```sh
$ wrangler kv:key list --binding=MY_NAMESPACE --prefix="public" | python -m json.tool
$ wrangler kv:key list --binding=MY_NAMESPACE --prefix="public" | jq '.'
[
{
"name": "public_key"
Expand Down
23 changes: 2 additions & 21 deletions src/commands/kv/namespace/list.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,24 @@
use cloudflare::endpoints::workerskv::list_namespaces::ListNamespaces;
use cloudflare::endpoints::workerskv::WorkersKvNamespace;
use cloudflare::framework::apiclient::ApiClient;

use prettytable::{Cell, Row, Table};

use crate::commands::kv;
use crate::settings::global_user::GlobalUser;
use crate::settings::target::Target;
use crate::terminal::message;

pub fn list(target: &Target, user: GlobalUser) -> Result<(), failure::Error> {
let client = kv::api_client(user)?;

message::working("Fetching namespaces...");

let response = client.request(&ListNamespaces {
account_identifier: &target.account_id,
});

match response {
Ok(success) => {
let table = namespace_table(success.result);
message::success(&format!("Success: \n{}", table));
let result = serde_json::to_string(&success.result)?;
println!("{}", result);
}
Err(e) => kv::print_error(e),
}

Ok(())
}

fn namespace_table(namespaces: Vec<WorkersKvNamespace>) -> Table {
let mut table = Table::new();
let table_head = Row::new(vec![Cell::new("TITLE"), Cell::new("ID")]);

table.add_row(table_head);
for ns in namespaces {
let row = Row::new(vec![Cell::new(&ns.title), Cell::new(&ns.id)]);
table.add_row(row);
}

table
}