From beb8615abb874ee8d4d88a2bc9743c1e7260d8a0 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Fri, 27 Sep 2024 21:25:49 +0200 Subject: [PATCH] Add a basic import function to paramcli --- bin/paramcli | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/bin/paramcli b/bin/paramcli index 0dbdeff..b6fca19 100755 --- a/bin/paramcli +++ b/bin/paramcli @@ -67,6 +67,7 @@ function print_help() { printf(" show {collection}\n Show keys and all values, allowing to delete values.\n"); printf(" delete {collection} {key|id}*\n Delete a key or a value from a collection\n"); printf(" purge {collection}\n Delete an entire collection\n"); + printf(" import {collection} [filename]\n Set values from JSON read from file (or stdin)\n"); } function exit_error(string $msg, int $code=1): never { @@ -214,6 +215,25 @@ function action_set(object $opts) { ); } +function action_import(object $opts): void { + $collection = array_shift($opts->args); + if (!$collection) { + exit_error("Missing collection. Expected: import {collection} [file]"); + } + + $filename = array_shift($opts->args) ?? STDIN; + $data = json_decode(stream_get_contents($filename)); + + http_post($collection, [ 'content-type' => 'application/json' ], json_encode($data))->then( + function ($response) { + echo $response->getBody()->getContents(); + }, + function ($error) { + echo $error->getMessage()."\n"; + } + ); +} + $action = array_shift($opts->args); switch ($action) { @@ -242,6 +262,10 @@ switch ($action) { action_purge($opts); break; + case 'import': + action_import($opts); + break; + default: print_help(); exit(0);