Add a basic import function to paramcli

This commit is contained in:
2024-09-27 21:25:49 +02:00
parent 6d873bcbcd
commit beb8615abb

View File

@@ -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);