/*** 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 table = storage.category("table").description($("Commands to manage your Storage tables"));


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





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





  table.command("show [table]").description($("Show details of the storage able")).option("--table <table>", $("the storage table name")).addStorageAccountOption().execute(showTable);





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






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


  tableSas.command("create [table] [permissions] [expiry]").description($("Generate a shared access signature of storage table")).option("--table <table>", $("the storage table name")).option("--permissions <permissions>", $("the operation permissions combining symbols of r(Query)/a(Add)/u(Update)/d(Delete)")).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")).option("--start-pk <startPk>", $("the inclusive starting partition key")).option("--end-pk <endPk>", $("the inclusive ending partition key")).option("--start-rk <startRk>", $("the inclusive starting row key")).option("--end-rk <endRk>", $("the inclusive ending row key")).addStorageAccountOption().execute(createTableSAS);















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


  policy.command("create [table] [name]").usage("[options] [table] [name]").description($("Create a stored access policy on the table")).option("--table <table>", $("the storage table 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(Query)/a(Add)/u(Update)/d(Delete)")).addStorageAccountOption().execute(createTablePolicy);










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







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






  policy.command("set [table] [name]").usage("[options] [table] [name]").description($("Set a stored access policy on the table")).option("--table <table>", $("the storage table 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(Query)/a(Add)/u(Update)/d(Delete) and passing two spaces means to remove the existing setting")).addStorageAccountOption().execute(setTablePolicy);










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















  function getTableServiceClient(options) {
    var serviceClient = StorageUtil.getServiceClient(StorageUtil.getTableService, options);
    applyTableServicePatch(serviceClient);
    return serviceClient; };







  function getStorageTableOperation(serviceClient, operationName) {
    return StorageUtil.getStorageOperation(serviceClient, StorageUtil.OperationType.Table, operationName); };





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



    return option; };





  function createTablePolicySetting(options) {
    var policySettings = { };
    policySettings.accessType = StorageUtil.AccessType.Table;
    policySettings.serviceClient = getTableServiceClient(options);
    policySettings.getAclOperation = getStorageTableOperation(policySettings.serviceClient, "getTableAcl");
    policySettings.setAclOperation = getStorageTableOperation(policySettings.serviceClient, "setTableAcl");
    policySettings.storageOptions = getStorageTableOperationDefaultOption();
    policySettings.policyOptions = options;
    return policySettings; };








  function createTable(table, options, _) { var tableService, operation, tips, storageOptions, created; var __frame = { name: "createTable", line: 206 }; return __func(_, this, arguments, createTable, 2, __frame, function __$createTable() {
      tableService = getTableServiceClient(options);
      return interaction.promptIfNotGiven($("Table name: "), table, __cb(_, __frame, 2, 24, function ___(__0, __1) { table = __1;
        operation = getStorageTableOperation(tableService, "createTable");
        tips = util.format($("Creating storage table %s"), table);
        storageOptions = getStorageTableOperationDefaultOption();
        startProgress(tips); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$createTable() {

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



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


            logger.verbose(util.format($("Table %s has been created successfully"), table));
            return showTable(table, StorageUtil.getStorageAccountOptions(options), __cb(_, __frame, 18, 4, function __$createTable() { _(); }, true)); }); }); }, true)); }); };








  function deleteTable(table, options, _) { var tableService, operation, tips, storageOptions; var __frame = { name: "deleteTable", line: 233 }; return __func(_, this, arguments, deleteTable, 2, __frame, function __$deleteTable() {
      tableService = getTableServiceClient(options);
      return interaction.promptIfNotGiven($("Table name: "), table, __cb(_, __frame, 2, 24, function ___(__0, __1) { table = __1;
        operation = getStorageTableOperation(tableService, "deleteTable");
        tips = util.format($("Deleting storagetable %s"), table);
        storageOptions = getStorageTableOperationDefaultOption(); return (function __$deleteTable(__then) {

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




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


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

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


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


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








  function showTable(table, options, _) { var tableService, operation, tips, storageOptions, output, exist, info, acl; var __frame = { name: "showTable", line: 269 }; return __func(_, this, arguments, showTable, 2, __frame, function __$showTable() {
      tableService = getTableServiceClient(options);
      return interaction.promptIfNotGiven($("Table name: "), table, __cb(_, __frame, 2, 24, function ___(__0, __1) { table = __1;
        operation = getStorageTableOperation(tableService, "doesTableExist");
        tips = $("Getting Storage table information");
        storageOptions = getStorageTableOperationDefaultOption();
        output = [];

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

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

                          operation = getStorageTableOperation(tableService, "getTableAcl");
                          info = { name: table }; return (function __$showTable(__then) {
                            if (!options.sas) {
                              return performStorageOperation(operation, __cb(_, __frame, 17, 20, function ___(__0, __3) { acl = __3;
                                info.policies = acl.signedIdentifiers; __then(); }, true), table, storageOptions); } else {

                              info.policies = { }; __then(); } ; })(function __$showTable() {

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


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


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


            cli.interaction.formatOutput(output[0], function(outputData) {
              var names = Object.keys(outputData.policies);
              if ((names.length === 0)) {
                logger.info(util.format($("No information is found for table %s"), table)); }
               else {
                logger.table(names, function(row, item) {
                  row.cell($("Policy"), item);

                  item = outputData.policies[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 listTable(prefix, options, _) { var tableService, listOperation, tips, tableOptions, useWildcard, tables; var __frame = { name: "listTable", line: 327 }; return __func(_, this, arguments, listTable, 2, __frame, function __$listTable() {
      tableService = getTableServiceClient(options);
      listOperation = getStorageTableOperation(tableService, "listAllTables");
      tips = $("Getting storage tables");
      tableOptions = getStorageTableOperationDefaultOption();
      useWildcard = false;

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


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


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


                  info = { name: table };
                  tables.push(info); _(); }); }); }, true), tableOptions); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$listTable() {


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


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








  function createTableSAS(table, permissions, expiry, options, _) { var tableService, start, tableField, output, sharedAccessPolicy, tips; var __frame = { name: "createTableSAS", line: 370 }; return __func(_, this, arguments, createTableSAS, 4, __frame, function __$createTableSAS() {
      tableService = getTableServiceClient(options);
      return interaction.promptIfNotGiven($("Table name: "), table, __cb(_, __frame, 2, 24, function ___(__0, __1) { table = __1; return (function __$createTableSAS(__then) {

          if (!options.policy) {
            return interaction.promptIfNotGiven($("Permissions: "), permissions, __cb(_, __frame, 5, 32, function ___(__0, __2) { permissions = __2;
              StorageUtil.validatePermissions(StorageUtil.AccessType.Table, 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 __$createTableSAS() {



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


          tableField = {
            startPk: options.startPk,
            endPk: options.endPk,
            startRk: options.startRk,
            endRk: options.endRk };


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

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


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


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






  function applyTableServicePatch(tableService) {




    tableService.listAllTables = function(options, callback) {
      StorageUtil.listWithContinuation(tableService.listTablesSegmentedWithPrefix, tableService, StorageUtil.ListContinuationTokenArgIndex.Table, options.prefix, null, options, callback); }; };






  function createTablePolicy(table, name, options, _) { var createPolicySettings, policies; var __frame = { name: "createTablePolicy", line: 426 }; return __func(_, this, arguments, createTablePolicy, 3, __frame, function __$createTablePolicy() {
      createPolicySettings = createTablePolicySetting(options);
      return interaction.promptIfNotGiven($("Table name: "), table, __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 table %s"), createPolicySettings.policyName, createPolicySettings.resourceName);

          if (options.permissions) {
            StorageUtil.validatePermissions(StorageUtil.AccessType.Table, 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 table %s are: "), createPolicySettings.resourceName));
              StorageUtil.showPolicyResults(outputData); }); _(); }, true)); }, true)); }, true)); }); };






  function listTablePolicy(table, options, _) { var listPolicySettings, policies; var __frame = { name: "listTablePolicy", line: 446 }; return __func(_, this, arguments, listTablePolicy, 2, __frame, function __$listTablePolicy() {
      listPolicySettings = createTablePolicySetting(options);
      return interaction.promptIfNotGiven($("Table name: "), table, __cb(_, __frame, 2, 50, function ___(__0, __1) { listPolicySettings.resourceName = __1;
        listPolicySettings.tips = util.format($("Listing the stored access policies on the table %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 table %s."), listPolicySettings.resourceName)); } ; }); _(); }, true)); }, true)); }); };







  function showTablePolicy(table, name, options, _) { var showPolicySettings, policy; var __frame = { name: "showTablePolicy", line: 464 }; return __func(_, this, arguments, showTablePolicy, 3, __frame, function __$showTablePolicy() {
      showPolicySettings = createTablePolicySetting(options);
      return interaction.promptIfNotGiven($("Table name: "), table, __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 table %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 setTablePolicy(table, name, options, _) { var setPolicySettings, policies; var __frame = { name: "setTablePolicy", line: 479 }; return __func(_, this, arguments, setTablePolicy, 3, __frame, function __$setTablePolicy() {
      setPolicySettings = createTablePolicySetting(options);
      return interaction.promptIfNotGiven($("Table name: "), table, __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 table %s"), setPolicySettings.policyName, setPolicySettings.resourceName);

          if (options.permissions) {
            StorageUtil.validatePermissions(StorageUtil.AccessType.Table, 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 table %s are: "), setPolicySettings.resourceName));
              StorageUtil.showPolicyResults(outputData); }); _(); }, true)); }, true)); }, true)); }); };






  function deleteTablePolicy(table, name, options, _) { var deletePolicySettings, policies; var __frame = { name: "deleteTablePolicy", line: 499 }; return __func(_, this, arguments, deleteTablePolicy, 3, __frame, function __$deleteTablePolicy() {
      deletePolicySettings = createTablePolicySetting(options);
      return interaction.promptIfNotGiven($("Table name: "), table, __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 table %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 table %s are: "), deletePolicySettings.resourceName));
                StorageUtil.showPolicyResults(outputData); }
               else {
                logger.info(util.format($("There is no stored access policy on the table %s"), deletePolicySettings.resourceName)); } ; }); _(); }, true)); }, true)); }, true)); }); };};
