/*** 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 batchUtil = require("./batch.util");
var batchShowUtil = require("./batch.showUtil");
var utils = require("../../util/utils");
var startProgress = batchUtil.startProgress;
var endProgress = batchUtil.endProgress;

var fs = require("fs");

var $ = utils.getLocaleString;




exports.init = function(cli) {


  batchUtil.init(cli);




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


  var logger = cli.output;

  var interaction = cli.interaction;

  var certificate = batch.category("certificate").description($("Commands to manage your Batch certificate"));


  certificate.command("create [thumbprint] [cert-file]").description($("Add a certificate")).option("-t, --thumbprint <thumbprint>", $("the certificate thumbprint")).option("-f, --cert-file <cert-file>", $("the certificate file: cer file or pfx file")).option("--thumbprint-algorithm <thumbprint-algorithm>", $("the certificate thumbprint algorithm")).option("-p, --password <password>", $("the password to access the certificate's private key")).appendBatchAccountOption().execute(addCertificate);








  certificate.command("delete [thumbprint]").description($("Delete the specified Batch certificate")).option("-t, --thumbprint <thumbprint>", $("the certificate thumbprint")).option("--thumbprint-algorithm <thumbprint-algorithm>", $("the certificate thumbprint algorithm")).option("--abort", $("cancel the deletion")).option("-q, --quiet", $("delete the specified certificate without confirmation")).appendBatchAccountOption().execute(deleteCertificate);








  certificate.command("show [thumbprint]").description($("Show details of the Batch certificate")).option("-t, --thumbprint <thumbprint>", $("the certificate thumbprint")).option("--thumbprint-algorithm <thumbprint-algorithm>", $("the certificate thumbprint algorithm")).appendODataFilterOption(true, false, false).appendBatchAccountOption().execute(showCertificate);







  certificate.command("list").description($("List Batch certificates")).appendODataFilterOption(true, true, false).appendBatchAccountOption().execute(listCertificate);
















  function addCertificate(thumbprint, certFile, options, _) { var thumbprintAlgorithm, readBuffer, data, client, tips, batchOptions, addParam; var __frame = { name: "addCertificate", line: 92 }; return __func(_, this, arguments, addCertificate, 3, __frame, function __$addCertificate() {
      if (!thumbprint) {
        thumbprint = options.thumbprint; } ;

      return interaction.promptIfNotGiven($("Certificate thumbprint: "), thumbprint, __cb(_, __frame, 4, 29, function ___(__0, __1) { thumbprint = __1;

        if (!certFile) {
          certFile = options.certFile; } ;

        return interaction.promptIfNotGiven($("Certificate file: "), certFile, __cb(_, __frame, 9, 27, function ___(__0, __2) { certFile = __2;

          thumbprintAlgorithm = options.thumbprintAlgorithm;
          if (!thumbprintAlgorithm) {
            thumbprintAlgorithm = "sha1"; } ;


          readBuffer = fs.readFileSync(certFile);
          data = readBuffer.toString("base64");

          client = batchUtil.createBatchServiceClient(options);
          tips = util.format($("Adding certificate file %s with thumbprint %s"), certFile, thumbprint);
          batchOptions = { };

          batchOptions.certificateAddOptions = batchUtil.getBatchOperationDefaultOption();

          addParam = { };
          addParam.thumbprint = thumbprint;
          addParam.thumbprintAlgorithm = thumbprintAlgorithm;
          addParam.data = data;
          if (options.password) {
            addParam.certificateFormat = "pfx";
            addParam.password = options.password; }
           else {
            addParam.certificateFormat = "cer"; } ;


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


                      return client.certificateOperations.add(addParam, batchOptions, __cb(_, __frame, 39, 35, __then, true)); }); })(function ___(err, __result) { __catch(function __$addCertificate() { if (err) {

                        if (err.message) {
                          if ((typeof err.message === "object")) {
                            err.message = err.message.value; } ;


                          return _(err); } ; __then(); } else { _(null, __result); } ; }, _); }); })(function ___() { __tryCatch(_, function __$addCertificate() { _(null, null, true); }); }); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$addCertificate() {


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


              logger.info(util.format($("Certificate %s has been added"), thumbprint));
              return showCertificate(thumbprint, options, __cb(_, __frame, 53, 4, function __$addCertificate() { _(); }, true)); }); }); }, true)); }, true)); }); };








  function deleteCertificate(thumbprint, options, _) { var thumbprintAlgorithm, client, tips, batchOptions; var __frame = { name: "deleteCertificate", line: 154 }; return __func(_, this, arguments, deleteCertificate, 2, __frame, function __$deleteCertificate() {
      if (!thumbprint) {
        thumbprint = options.thumbprint; } ;

      return interaction.promptIfNotGiven($("Certificate thumbprint: "), thumbprint, __cb(_, __frame, 4, 29, function ___(__0, __1) { thumbprint = __1;
        thumbprintAlgorithm = options.thumbprintAlgorithm;
        if (!thumbprintAlgorithm) {
          thumbprintAlgorithm = "sha1"; } ;


        client = batchUtil.createBatchServiceClient(options);

        batchOptions = { }; return (function __$deleteCertificate(__then) {

          if (options.abort) {
            tips = util.format($("Cancelling certificate %s deletion"), thumbprint);

            batchOptions.certificateCancelDeletionOptions = batchUtil.getBatchOperationDefaultOption();

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


                        return client.certificateOperations.cancelDeletion(thumbprintAlgorithm, thumbprint, batchOptions, __cb(_, __frame, 22, 37, __then, true)); }); })(function ___(err, __result) { __catch(function __$deleteCertificate() { if (err) {

                          if (batchUtil.isNotFoundException(err)) {
                            return _(new Error(util.format($("Certificate %s doesn't exist"), thumbprint))); }
                           else {
                            if (err.message) {
                              if ((typeof err.message === "object")) {
                                err.message = err.message.value; } ; } ;



                            return _(err); } ; __then(); } else { _(null, __result); } ; }, _); }); })(function ___() { __tryCatch(_, function __$deleteCertificate() { _(null, null, true); }); }); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$deleteCertificate() {


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


                logger.info(util.format($("Certificate %s won't be deleted"), thumbprint)); __then(); }); }); } else { return (function __$deleteCertificate(__then) {

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




              tips = util.format($("Deleting certificate %s"), thumbprint);
              batchOptions.certificateDeleteMethodOptions = batchUtil.getBatchOperationDefaultOption();

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


                          return client.certificateOperations.deleteMethod(thumbprintAlgorithm, thumbprint, batchOptions, __cb(_, __frame, 53, 37, __then, true)); }); })(function ___(err, __result) { __catch(function __$deleteCertificate() { if (err) {

                            if (batchUtil.isNotFoundException(err)) {
                              return _(new Error(util.format($("Certificate %s doesn't exist"), thumbprint))); }
                             else {
                              if (err.message) {
                                if ((typeof err.message === "object")) {
                                  err.message = err.message.value; } ; } ;



                              return _(err); } ; __then(); } else { _(null, __result); } ; }, _); }); })(function ___() { __tryCatch(_, function __$deleteCertificate() { _(null, null, true); }); }); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$deleteCertificate() {


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


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









  function showCertificate(thumbprint, options, _) { var thumbprintAlgorithm, client, tips, batchOptions, certificate; var __frame = { name: "showCertificate", line: 234 }; return __func(_, this, arguments, showCertificate, 2, __frame, function __$showCertificate() {
      if (!thumbprint) {
        thumbprint = options.thumbprint; } ;

      return interaction.promptIfNotGiven($("Certificate thumbprint: "), thumbprint, __cb(_, __frame, 4, 29, function ___(__0, __1) { thumbprint = __1;
        thumbprintAlgorithm = options.thumbprintAlgorithm;
        if (!thumbprintAlgorithm) {
          thumbprintAlgorithm = "sha1"; } ;


        client = batchUtil.createBatchServiceClient(options);
        tips = $("Getting Batch certificate information");
        batchOptions = { };
        batchOptions.certificateGetOptions = batchUtil.getBatchOperationDefaultOption();

        if (options.selectClause) {
          batchOptions.certificateGetOptions.select = options.selectClause; } ;


        certificate = null;
        startProgress(tips); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$showCertificate() { return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$showCertificate() {


                    return client.certificateOperations.get(thumbprintAlgorithm, thumbprint, batchOptions, __cb(_, __frame, 23, 49, function ___(__0, __2) { certificate = __2; __then(); }, true)); }); })(function ___(err, __result) { __catch(function __$showCertificate() { if (err) {

                      if (batchUtil.isNotFoundException(err)) {
                        return _(new Error(util.format($("Certificate %s doesn't exist"), thumbprint))); }
                       else {
                        if (err.message) {
                          if ((typeof err.message === "object")) {
                            err.message = err.message.value; } ; } ;



                        return _(err); } ; __then(); } else { _(null, __result); } ; }, _); }); })(function ___() { __tryCatch(_, function __$showCertificate() { _(null, null, true); }); }); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$showCertificate() {


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


            batchShowUtil.showCertificate(certificate, cli.output); _(); }); }); }, true)); }); };







  function listCertificate(options, _) { var client, tips, batchOptions, certficates, result, nextLink; var __frame = { name: "listCertificate", line: 282 }; return __func(_, this, arguments, listCertificate, 1, __frame, function __$listCertificate() {
      client = batchUtil.createBatchServiceClient(options);
      tips = $("Listing Batch certficates");
      batchOptions = { };
      batchOptions.certificateListOptions = batchUtil.getBatchOperationDefaultOption();

      if (options.selectClause) {
        batchOptions.certificateListOptions.select = options.selectClause; } ;

      if (options.filterClause) {
        batchOptions.certificateListOptions.filter = options.filterClause; } ;


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


                  return client.certificateOperations.list(batchOptions, __cb(_, __frame, 17, 48, function ___(__0, __1) { result = __1;
                    result.forEach(function(cert) {
                      certficates.push(cert); });

                    nextLink = result.odatanextLink; return (function ___(__break) { var __more; var __loop = __cb(_, __frame, 0, 0, function __$listCertificate() { __more = false;

                        var __4 = nextLink; if (__4) {
                          batchOptions.certificateListOptions = batchUtil.getBatchOperationDefaultOption();
                          return client.certificateOperations.listNext(nextLink, batchOptions, __cb(_, __frame, 25, 46, function ___(__0, __2) { result = __2;
                            result.forEach(function(cert) {
                              certficates.push(cert); });

                            nextLink = result.odatanextLink; while (__more) { __loop(); }; __more = true; }, true)); } else { __break(); } ; }); do { __loop(); } while (__more); __more = true; })(__then); }, true)); }); })(function ___(err, __result) { __catch(function __$listCertificate() { if (err) {


                    if (err.message) {
                      if ((typeof err.message === "object")) {
                        err.message = err.message.value; } ; } ;



                    return _(err); } else { _(null, __result); } ; }, _); }); })(function ___() { __tryCatch(_, function __$listCertificate() { _(null, null, true); }); }); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$listCertificate() {

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


          cli.interaction.formatOutput(certficates, function(outputData) {
            if ((outputData.length === 0)) {
              logger.info($("No certificate found")); }
             else {
              logger.table(outputData, function(row, item) {
                row.cell($("Thumbprint"), item.thumbprint);
                row.cell($("State"), item.state);
                row.cell($("Previous State"), item.previousState);
                row.cell($("Deletion Error"), !!item.deleteCertificateError); }); } ; }); _(); }); }); }); };};
