/*** Generated by streamline 0.10.17 (callbacks) - DO NOT EDIT ***/ var __rt=require('streamline/lib/callbacks/runtime').runtime(__filename, false),__func=__rt.__func,__cb=__rt.__cb,__catch=__rt.__catch,__tryCatch=__rt.__tryCatch; var util = require("util");
















var commander = require("commander");
var StorageUtil = require("../../util/storage.util");
var utils = require("../../util/utils");
var Wildcard = utils.Wildcard;
var performStorageOperation = StorageUtil.performStorageOperation;
var startProgress = StorageUtil.startProgress;
var endProgress = StorageUtil.endProgress;

var $ = utils.getLocaleString;




commander.Command.prototype.addStorageAccountOption = function() {
  this.option("-a, --account-name <accountName>", $("the storage account name"));
  this.option("-k, --account-key <accountKey>", $("the storage account key"));
  this.option("-c, --connection-string <connectionString>", $("the storage connection string"));
  this.option("-vv", $("run storage command in debug mode"));
  return this;};





exports.init = function(cli) {


  StorageUtil.init(cli);




  var storage = cli.category("storage").description($("Commands to manage your Storage objects"));


  var logger = cli.output;

  var interaction = cli.interaction;

  var queue = storage.category("queue").description($("Commands to manage your Storage queues"));


  queue.command("create [queue]").description($("Create a storage queue")).option("--queue <queue>", $("the storage queue name")).addStorageAccountOption().execute(createQueue);





  queue.command("list [prefix]").description($("List storage queues with wildcard")).option("-p, --prefix <prefix>", $("the storage queue name prefix")).addStorageAccountOption().execute(listQueue);





  queue.command("show [queue]").description($("Show details of the storage able")).option("--queue <queue>", $("the storage queue name")).option("--sas <sas>", $("the shared access signature of the storage queue")).addStorageAccountOption().execute(showQueue);






  queue.command("delete [queue]").description($("Delete the specified storage queue")).option("--queue <queue>", $("the storage queue name")).option("-q, --quiet", $("remove the specified storage queue without confirmation")).addStorageAccountOption().execute(deleteQueue);






  var queueSas = queue.category("sas").description($("Commands to manage shared access signatures of your Storage queue"));


  queueSas.command("create [queue] [permissions] [expiry]").description($("Generate shared access signature of storage queue")).option("--queue <queue>", $("the storage queue name")).option("--permissions <permissions>", $("the operation permissions combining symbols of r(Read)/a(Add)/u(Update)/p(Process)")).option("--protocol <protocol>", $("the protocol permitted for a request made with the SAS. Possible values are HttpsOnly and HttpsOrHttp")).option("--ip-range <ipRange>", $("an IP address or a range of IP addresses from which to accept requests. When specifying a range of IP addresses, note that the range is inclusive. For example, specifying 168.1.5.65 or 168.1.5.60-168.1.5.70 on the SAS restricts the request to those IP addresses.")).option("--start <start>", $("the UTC time at which the SAS becomes valid")).option("--expiry <expiry>", $("the UTC time at which the SAS expires")).option("--policy <policy>", $("the stored access policy identifier")).addStorageAccountOption().execute(createQueueSAS);











  var policy = queue.category("policy").description($("Commands to manage stored access policies of your Storage queue"));


  policy.command("create [queue] [name]").usage("[options] [queue] [name]").description($("Create a stored access policy on the queue")).option("--queue <queue>", $("the storage queue name")).option("--name <name>", $("the policy name")).option("--start <start>", $("the UTC time at which the policy becomes valid")).option("--expiry <expiry>", $("the UTC time at which the policy expires")).option("--permissions <permissions>", $("the operation permissions combining symbols of r(Read)/a(Add)/u(Update)/p(Process)")).addStorageAccountOption().execute(createQueuePolicy);










  policy.command("show [queue] [name]").usage("[options] [queue] [name]").description($("Show a stored access policy on the queue")).option("--queue <queue>", $("the storage queue name")).option("--name <name>", $("the policy name")).addStorageAccountOption().execute(showQueuePolicy);







  policy.command("list [queue]").usage("[options] [queue]").description($("List stored access policies on the queue")).option("--queue <queue>", $("the storage queue name")).addStorageAccountOption().execute(listQueuePolicy);






  policy.command("set [queue] [name]").usage("[options] [queue] [name]").description($("Set a stored access policy on the queue")).option("--queue <queue>", $("the storage queue name")).option("--name <name>", $("the policy name")).option("--start <start>", $("the UTC time at which the policy becomes valid and passing two spaces means to remove the existing setting")).option("--expiry <expiry>", $("the UTC time at which the policy expires and passing two spaces means to remove the existing setting")).option("--permissions <permissions>", $("the operation permissions combining symbols of r(Read)/a(Add)/u(Update)/p(Process) and passing two spaces means to remove the existing setting")).addStorageAccountOption().execute(setQueuePolicy);










  policy.command("delete [queue] [name]").usage("[options] [queue] [name]").description($("Delete a stored access policy on the queue")).option("--queue <queue>", $("the storage queue name")).option("--name <name>", $("the policy name")).addStorageAccountOption().execute(deleteQueuePolicy);















  function getQueueServiceClient(options) {
    var serviceClient = StorageUtil.getServiceClient(StorageUtil.getQueueService, options);
    applyQueueServicePatch(serviceClient);
    return serviceClient; };







  function getStorageQueueOperation(serviceClient, operationName) {
    return StorageUtil.getStorageOperation(serviceClient, StorageUtil.OperationType.Queue, operationName); };





  function getStorageQueueOperationDefaultOption() {
    var option = StorageUtil.getStorageOperationDefaultOption();



    return option; };





  function createQueuePolicySetting(options) {
    var policySettings = { };
    policySettings.accessType = StorageUtil.AccessType.Queue;
    policySettings.serviceClient = getQueueServiceClient(options);
    policySettings.getAclOperation = getStorageQueueOperation(policySettings.serviceClient, "getQueueAcl");
    policySettings.setAclOperation = getStorageQueueOperation(policySettings.serviceClient, "setQueueAcl");
    policySettings.storageOptions = getStorageQueueOperationDefaultOption();
    policySettings.policyOptions = options;
    return policySettings; };








  function createQueue(queue, options, _) { var queueService, operation, tips, storageOptions, created; var __frame = { name: "createQueue", line: 203 }; return __func(_, this, arguments, createQueue, 2, __frame, function __$createQueue() {
      queueService = getQueueServiceClient(options);
      return interaction.promptIfNotGiven($("Queue name: "), queue, __cb(_, __frame, 2, 24, function ___(__0, __1) { queue = __1;
        operation = getStorageQueueOperation(queueService, "createQueue");
        tips = util.format($("Creating storage queue %s"), queue);
        storageOptions = getStorageQueueOperationDefaultOption();
        startProgress(tips); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$createQueue() {

              return performStorageOperation(operation, __cb(_, __frame, 8, 20, function ___(__0, __2) { created = __2;
                if ((created === false)) {
                  return _(new Error(util.format($("Queue '%s' already exists"), queue))); } ; _(null, null, true); }, true), queue, storageOptions); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$createQueue() {


                endProgress(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$createQueue() {


            logger.verbose(util.format($("Queue %s has been created successfully"), queue));
            return showQueue(queue, StorageUtil.getStorageAccountOptions(options), __cb(_, __frame, 17, 4, function __$createQueue() { _(); }, true)); }); }); }, true)); }); };








  function deleteQueue(queue, options, _) { var queueService, operation, tips, storageOptions; var __frame = { name: "deleteQueue", line: 229 }; return __func(_, this, arguments, deleteQueue, 2, __frame, function __$deleteQueue() {
      queueService = getQueueServiceClient(options);
      return interaction.promptIfNotGiven($("Queue name: "), queue, __cb(_, __frame, 2, 24, function ___(__0, __1) { queue = __1;
        operation = getStorageQueueOperation(queueService, "deleteQueue");
        tips = util.format($("Deleting storagequeue %s"), queue);
        storageOptions = getStorageQueueOperationDefaultOption(); return (function __$deleteQueue(__then) {

          if (!options.quiet) {
            return interaction.confirm(util.format($("Do you want to delete queue %s? "), queue), __cb(_, __frame, 8, 23, function ___(__0, __3) { var __2 = !__3; return (function __$deleteQueue(__then) { if (__2) { return _(null); } else { __then(); } ; })(__then); }, true)); } else { __then(); } ; })(function __$deleteQueue() {




          startProgress(tips); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$deleteQueue() { return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$deleteQueue() {


                      return performStorageOperation(operation, __cb(_, __frame, 16, 6, __then, true), queue, storageOptions); }); })(function ___(e, __result) { __catch(function __$deleteQueue() { if (e) {

                        if (StorageUtil.isNotFoundException(e)) {
                          return _(new Error(util.format($("Can not find queue '%s'"), queue))); }
                         else {
                          return _(e); } ; __then(); } else { _(null, __result); } ; }, _); }); })(function ___() { __tryCatch(_, function __$deleteQueue() { _(null, null, true); }); }); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$deleteQueue() {


                  endProgress(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$deleteQueue() {


              logger.info(util.format($("Queue %s has been deleted successfully"), queue)); _(); }); }); }); }, true)); }); };








  function showQueue(queue, options, _) { var queueService, operation, tips, storageOptions, output, exist, info; var __frame = { name: "showQueue", line: 265 }; return __func(_, this, arguments, showQueue, 2, __frame, function __$showQueue() {
      queueService = getQueueServiceClient(options);
      return interaction.promptIfNotGiven($("Queue name: "), queue, __cb(_, __frame, 2, 24, function ___(__0, __1) { queue = __1;
        operation = getStorageQueueOperation(queueService, "doesQueueExist");
        tips = $("Getting Storage queue information");
        storageOptions = getStorageQueueOperationDefaultOption();
        output = [];

        startProgress(tips); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$showQueue() { return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$showQueue() {

                    return performStorageOperation(operation, __cb(_, __frame, 10, 18, function ___(__0, __2) { exist = __2.exists; return (function __$showQueue(__then) {
                        if (!exist) {
                          return _(new Error(util.format($("Queue %s doesn't exist"), queue))); } else {

                          info = { name: queue };
                          operation = getStorageQueueOperation(queueService, "getQueueMetadata");
                          return performStorageOperation(operation, __cb(_, __frame, 16, 24, function ___(__0, __3) { info.metadata = __3; return (function __$showQueue(__then) {

                              if (!options.sas) {
                                operation = getStorageQueueOperation(queueService, "getQueueAcl");
                                return performStorageOperation(operation, __cb(_, __frame, 20, 26, function ___(__0, __4) { info.policies = __4; __then(); }, true), queue, storageOptions); } else {

                                info.policies = { signedIdentifiers: { } }; __then(); } ; })(function __$showQueue() {


                              output.push(info); __then(); }); }, true), queue, storageOptions); } ; })(__then); }, true), queue, storageOptions); }); })(function ___(e, __result) { __catch(function __$showQueue() { if (e) {


                      if (StorageUtil.isNotFoundException(e)) {
                        return _(new Error(util.format($("Queue %s doesn't exist"), queue))); }
                       else {
                        return _(e); } ; __then(); } else { _(null, __result); } ; }, _); }); })(function ___() { __tryCatch(_, function __$showQueue() { _(null, null, true); }); }); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$showQueue() {


                endProgress(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$showQueue() {


            cli.interaction.formatOutput(output[0], function(outputData) {
              logger.info(util.format($("Approximate message count in the queue %s is %s"), queue, outputData.metadata.approximateMessageCount));
              if ((Object.keys(outputData.metadata.metadata).length > 0)) {
                logger.info("");
                logger.table(outputData.metadata.metadata, function(row, item) {
                  row.cell($("Metadata"), item);
                  row.cell($("Value"), outputData.metadata.metadata[item]); }); } ;


              var names = Object.keys(outputData.policies.signedIdentifiers);
              if ((names.length > 0)) {
                logger.info("");
                logger.table(names, function(row, item) {
                  row.cell($("Policy"), item);

                  item = outputData.policies.signedIdentifiers[item];
                  row.cell($("Permissions"), (item.Permissions ? item.Permissions : ""));

                  var UTCFormat = "YYYY-MM-DDTHH:MM:SSZ";
                  row.cell($("Start"), (item.Start ? item.Start.toUTCFormat(UTCFormat) : ""));
                  row.cell($("Expiry"), (item.Expiry ? item.Expiry.toUTCFormat(UTCFormat) : "")); }); } ; }); _(); }); }); }, true)); }); };










  function listQueue(prefix, options, _) { var queueService, listOperation, tips, queueOptions, useWildcard, queues; var __frame = { name: "listQueue", line: 333 }; return __func(_, this, arguments, listQueue, 2, __frame, function __$listQueue() {
      queueService = getQueueServiceClient(options);
      listOperation = getStorageQueueOperation(queueService, "listAllQueues");
      tips = $("Getting storage queues");
      queueOptions = getStorageQueueOperationDefaultOption();
      useWildcard = false;

      if (Wildcard.containWildcards(prefix)) {
        queueOptions.prefix = Wildcard.getNonWildcardPrefix(prefix);
        useWildcard = true; }
       else {
        queueOptions.prefix = prefix; } ;


      queues = [];
      startProgress(tips); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$listQueue() {


            return performStorageOperation(listOperation, __cb(_, __frame, 18, 6, function ___(__0, __2) { return __2.forEach_(__cb(_, __frame, 18, 62, function __$listQueue() { _(null, null, true); }, true), StorageUtil.opConcurrency, function __1(_, queue) { var info; var __frame = { name: "__1", line: 351 }; return __func(_, this, arguments, __1, 0, __frame, function __$__1() {
                  if ((useWildcard && !Wildcard.isMatch(queue.name, prefix))) { return _(null); } ;


                  info = { name: queue.name };
                  queues.push(info); _(); }); }); }, true), queueOptions); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$listQueue() {


              endProgress(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$listQueue() {


          cli.interaction.formatOutput(queues, function(outputData) {
            if ((outputData.length === 0)) {
              logger.info($("No queue found")); }
             else {
              logger.table(outputData, function(row, item) {
                row.cell($("Name"), item.name); }); } ; }); _(); }); }); }); };








  function createQueueSAS(queue, permissions, expiry, options, _) { var queueService, start, output, sharedAccessPolicy, tips; var __frame = { name: "createQueueSAS", line: 376 }; return __func(_, this, arguments, createQueueSAS, 4, __frame, function __$createQueueSAS() {
      queueService = getQueueServiceClient(options);
      return interaction.promptIfNotGiven($("Queue name: "), queue, __cb(_, __frame, 2, 24, function ___(__0, __1) { queue = __1; return (function __$createQueueSAS(__then) {

          if (!options.policy) {
            return interaction.promptIfNotGiven($("Permissions: "), permissions, __cb(_, __frame, 5, 32, function ___(__0, __2) { permissions = __2;
              StorageUtil.validatePermissions(StorageUtil.AccessType.Queue, permissions);

              return interaction.promptIfNotGiven($("Expiry: "), expiry, __cb(_, __frame, 8, 27, function ___(__0, __3) { expiry = __3;
                expiry = utils.parseDateTime(expiry); __then(); }, true)); }, true)); } else { __then(); } ; })(function __$createQueueSAS() {



          if (options.start) {
            start = utils.parseDateTime(options.start); } ;


          output = { sas: "" };
          sharedAccessPolicy = StorageUtil.getSharedAccessPolicy(StorageUtil.AccessType.Queue, permissions, options.protocol, options.ipRange, start, expiry, null, options.policy);
          tips = util.format($("Creating shared access signature for queue %s"), queue);
          startProgress(tips); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$createQueueSAS() {

                output.sas = queueService.generateSharedAccessSignature(queue, sharedAccessPolicy); _(null, null, true); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$createQueueSAS() {

                  endProgress(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$createQueueSAS() {


              cli.interaction.formatOutput(output, function(outputData) {
                logger.data($("Shared Access Signature"), outputData.sas); }); _(); }); }); }); }, true)); }); };






  function applyQueueServicePatch(queueService) {




    queueService.listAllQueues = function(options, callback) {
      StorageUtil.listWithContinuation(queueService.listQueuesSegmentedWithPrefix, queueService, StorageUtil.ListContinuationTokenArgIndex.Queue, options.prefix, null, options, callback); }; };






  function createQueuePolicy(queue, name, options, _) { var createPolicySettings, policies; var __frame = { name: "createQueuePolicy", line: 424 }; return __func(_, this, arguments, createQueuePolicy, 3, __frame, function __$createQueuePolicy() {
      createPolicySettings = createQueuePolicySetting(options);
      return interaction.promptIfNotGiven($("Queue name: "), queue, __cb(_, __frame, 2, 52, function ___(__0, __1) { createPolicySettings.resourceName = __1;
        return interaction.promptIfNotGiven($("Policy name: "), name, __cb(_, __frame, 3, 50, function ___(__0, __2) { createPolicySettings.policyName = __2;
          createPolicySettings.tips = util.format($("Creating the stored access policy %s on the queue %s"), createPolicySettings.policyName, createPolicySettings.resourceName);

          if (options.permissions) {
            StorageUtil.validatePermissions(StorageUtil.AccessType.Queue, options.permissions); } ;


          return StorageUtil.createPolicy(createPolicySettings, __cb(_, __frame, 10, 31, function ___(__0, __3) { policies = __3;
            cli.interaction.formatOutput(policies, function(outputData) {
              logger.info(util.format($("The stored access policies on queue %s are: "), createPolicySettings.resourceName));
              StorageUtil.showPolicyResults(outputData); }); _(); }, true)); }, true)); }, true)); }); };






  function listQueuePolicy(queue, options, _) { var listPolicySettings, policies; var __frame = { name: "listQueuePolicy", line: 444 }; return __func(_, this, arguments, listQueuePolicy, 2, __frame, function __$listQueuePolicy() {
      listPolicySettings = createQueuePolicySetting(options);
      return interaction.promptIfNotGiven($("Queue name: "), queue, __cb(_, __frame, 2, 50, function ___(__0, __1) { listPolicySettings.resourceName = __1;
        listPolicySettings.tips = util.format($("Listing the stored access policies on the queue %s"), listPolicySettings.resourceName);

        return StorageUtil.selectPolicy(listPolicySettings, __cb(_, __frame, 5, 31, function ___(__0, __2) { policies = __2;
          cli.interaction.formatOutput(policies, function(outputData) {
            if (outputData) {
              StorageUtil.showPolicyResults(outputData); }
             else {
              logger.info(util.format($("There is no stored access policy on the queue %s."), listPolicySettings.resourceName)); } ; }); _(); }, true)); }, true)); }); };







  function showQueuePolicy(queue, name, options, _) { var showPolicySettings, policy; var __frame = { name: "showQueuePolicy", line: 462 }; return __func(_, this, arguments, showQueuePolicy, 3, __frame, function __$showQueuePolicy() {
      showPolicySettings = createQueuePolicySetting(options);
      return interaction.promptIfNotGiven($("Queue name: "), queue, __cb(_, __frame, 2, 50, function ___(__0, __1) { showPolicySettings.resourceName = __1;
        return interaction.promptIfNotGiven($("Policy name: "), name, __cb(_, __frame, 3, 48, function ___(__0, __2) { showPolicySettings.policyName = __2;
          showPolicySettings.tips = util.format($("Showing the stored access policy %s on the queue %s"), showPolicySettings.policyName, showPolicySettings.resourceName);

          return StorageUtil.selectPolicy(showPolicySettings, __cb(_, __frame, 6, 29, function ___(__0, __3) { policy = __3;
            cli.interaction.formatOutput(policy, function(outputData) {
              StorageUtil.showPolicyResults(outputData); }); _(); }, true)); }, true)); }, true)); }); };






  function setQueuePolicy(queue, name, options, _) { var setPolicySettings, policies; var __frame = { name: "setQueuePolicy", line: 477 }; return __func(_, this, arguments, setQueuePolicy, 3, __frame, function __$setQueuePolicy() {
      setPolicySettings = createQueuePolicySetting(options);
      return interaction.promptIfNotGiven($("Queue name: "), queue, __cb(_, __frame, 2, 49, function ___(__0, __1) { setPolicySettings.resourceName = __1;
        return interaction.promptIfNotGiven($("Policy name: "), name, __cb(_, __frame, 3, 47, function ___(__0, __2) { setPolicySettings.policyName = __2;
          setPolicySettings.tips = util.format($("Setting the stored access policy %s on the queue %s"), setPolicySettings.policyName, setPolicySettings.resourceName);

          if (options.permissions) {
            StorageUtil.validatePermissions(StorageUtil.AccessType.Queue, options.permissions); } ;


          return StorageUtil.setPolicy(setPolicySettings, __cb(_, __frame, 10, 31, function ___(__0, __3) { policies = __3;
            cli.interaction.formatOutput(policies, function(outputData) {
              logger.info(util.format($("The stored access policies on queue %s are: "), setPolicySettings.resourceName));
              StorageUtil.showPolicyResults(outputData); }); _(); }, true)); }, true)); }, true)); }); };






  function deleteQueuePolicy(queue, name, options, _) { var deletePolicySettings, policies; var __frame = { name: "deleteQueuePolicy", line: 497 }; return __func(_, this, arguments, deleteQueuePolicy, 3, __frame, function __$deleteQueuePolicy() {
      deletePolicySettings = createQueuePolicySetting(options);
      return interaction.promptIfNotGiven($("Queue name: "), queue, __cb(_, __frame, 2, 52, function ___(__0, __1) { deletePolicySettings.resourceName = __1;
        return interaction.promptIfNotGiven($("Policy name: "), name, __cb(_, __frame, 3, 50, function ___(__0, __2) { deletePolicySettings.policyName = __2;
          deletePolicySettings.tips = util.format($("Deleting the stored access policy %s on the queue %s"), deletePolicySettings.policyName, deletePolicySettings.resourceName);

          return StorageUtil.deletePolicy(deletePolicySettings, __cb(_, __frame, 6, 31, function ___(__0, __3) { policies = __3;
            cli.interaction.formatOutput(policies, function(outputData) {
              if (outputData) {
                logger.info(util.format($("The stored access policies on queue %s are: "), deletePolicySettings.resourceName));
                StorageUtil.showPolicyResults(outputData); }
               else {
                logger.info(util.format($("There is no stored access policy on the queue %s."), deletePolicySettings.resourceName)); } ; }); _(); }, true)); }, true)); }, true)); }); };};
