#!/usr/bin/env node // -*- mode: js -*- /* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ /* * Copyright (c) 2018, Joyent, Inc. */ var cmdutil = require('cmdutil'); var moray = require('../lib'); var moraycli = require('../lib/cmd'); var clientOptions, parser, client; var findOptions, bucket, filter, p; var prettyPrint = true; cmdutil.configure({ 'usageMessage': 'fetch objects from Moray matching a filter', 'synopses': [ moraycli.commonUsage + '[-Hins] [-a attr] [-d attr] [-l limit] ' + '[-o offset] BUCKET FILTER' ] }); clientOptions = {}; findOptions = {}; parser = moraycli.parseCliOptions({ 'argv': process.argv, 'env': process.env, 'errstream': process.stderr, 'extraOptStr': 'a:d:Hil:no:s', 'clientOptions': clientOptions, 'onUsage': cmdutil.usage, 'onOption': function (option) { switch (option.option) { case 'a': findOptions.sort = { 'order': 'ASC', 'attribute': option.optarg }; break; case 'd': findOptions.sort = { 'order': 'DESC', 'attribute': option.optarg }; break; case 'H': prettyPrint = false; break; case 'i': findOptions.requireIndexes = true; break; case 'l': p = parseInt(option.optarg, 10); if (isNaN(p) || p < 0) { cmdutil.usage('unsupported value for -l: "%s"', option.optarg); } findOptions.limit = p; break; case 'n': findOptions.no_count = true; break; case 'o': p = parseInt(option.optarg, 10); if (isNaN(p) || p < 0) { cmdutil.usage('unsupported value for -o / --offset: "%s"', option.optarg); } findOptions.offset = p; break; case 's': findOptions.sql_only = true; break; default: cmdutil.usage(); break; } } }); if (parser.optind() >= process.argv.length) cmdutil.usage('missing required arguments: "bucket"'); bucket = process.argv[parser.optind()]; if ((parser.optind() + 1) >= process.argv.length) cmdutil.usage('missing required arguments: "filter"'); filter = process.argv[parser.optind() + 1]; client = moray.createClient(clientOptions); client.on('error', cmdutil.fail); client.on('connect', function onConnect() { var req; req = client.findObjects(bucket, filter, findOptions); req.on('error', cmdutil.fail); req.on('record', function (obj) { if (prettyPrint) console.log(JSON.stringify(obj, null, 2)); else console.log(JSON.stringify(obj)); }); req.on('end', function () { moraycli.cliFinish(cmdutil, client); client.close(); }); });