/*** 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,__tryCatch=__rt.__tryCatch; var path = require("path");
















var fs = require("fs");
var util = require("util");

var profile = require("../../util/profile");
var utils = require("../../util/utils");
var WebsitesClient = require("./websites/websitesclient");

var validation = require("../../util/validation");

var $ = utils.getLocaleString;

exports.init = function(cli) {
  var log = cli.output;
  var site = cli.category("site");
  var siteJobs = site.category("job").description($("Commands to manage your Web Site WebJobs"));


  siteJobs.command("list [name]").description($("List all the WebJobs under a web site")).option("--job-type <job-type>", $("optional. The type of the webjob. Valid value is \"triggered\" or \"continuous\". By default return webjobs of all types.")).option("--slot <slot>", $("the name of the slot")).option("-s, --subscription <id>", $("the subscription id")).execute(function __1(name, options, _) { var parsedSiteName, context, service, continuousJobs, triggeredJobs, webJobs, progress; var __frame = { name: "__1", line: 40 }; return __func(_, this, arguments, __1, 2, __frame, function __$__1() {





      parsedSiteName = WebsitesClient.parseSiteName(name);
      context = {
        subscription: profile.current.getSubscription(options.subscription).id,
        site: {
          name: parsedSiteName.name,
          slot: (options.slot ? options.slot : parsedSiteName.slot) } };



      if (options.jobType) {
        validation.isValidEnumValue(options.jobType, ["continuous","triggered",]); } ;


      return createWebsiteExtensionsClient(context, __cb(_, __frame, 14, 20, function ___(__0, __1) { service = __1;


        webJobs = [];

        progress = cli.interaction.progress($("Getting WebJobs")); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__1() { return (function __$__1(__then) {

                if ((!options.jobType || (options.jobType === "continuous"))) {
                  return service.continuousWebJobs.list(__cb(_, __frame, 22, 53, function ___(__0, __2) { continuousJobs = __2; __then(); }, true)); } else { __then(); } ; })(function __$__1() { return (function __$__1(__then) {


                  if ((!options.jobType || (options.jobType === "triggered"))) {
                    return service.triggeredWebJobs.list(__cb(_, __frame, 26, 51, function ___(__0, __3) { triggeredJobs = __3; __then(); }, true)); } else { __then(); } ; })(function __$__1() { _(null, null, true); }); }); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$__1() {


                progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$__1() {


            if (continuousJobs) {
              webJobs = webJobs.concat(continuousJobs.continuousWebJobs); } ;


            if (triggeredJobs) {
              webJobs = webJobs.concat(triggeredJobs.triggeredWebJobs); } ;


            cli.interaction.formatOutput(webJobs, function(data) {
              if ((data.length > 0)) {
                log.table(data, function(row, item) {
                  row.cell($("Name"), item.name);
                  row.cell($("Type"), item.type);
                  row.cell($("Run Command"), item.runCommand);
                  row.cell($("Status"), item.status); }); }

               else {
                log.info($("No jobs exist.")); } ; }); _(); }); }); }, true)); }); });




  siteJobs.command("show [jobName] [jobType] [name]").usage("[options] <jobName> <jobType> [name]").description($("Show details of a specific webjob")).option("--job-name <job-name>", $("required. The name of the webjob.")).option("--job-type <job-type>", $("required. The type of the webjob. Valid value is \"triggered\" or \"continuous\".")).option("--slot <slot>", $("the name of the slot")).option("-s, --subscription <id>", $("the subscription id")).execute(function __2(jobName, jobType, name, options, _) { var parsedSiteName, context, service, operation, webJob, progress, jobTypeKey; var __frame = { name: "__2", line: 101 }; return __func(_, this, arguments, __2, 4, __frame, function __$__2() {







      if (jobType) {
        validation.isValidEnumValue(jobType, ["continuous","triggered",]); }
       else {
        return _(new Error($("--job-type is required"))); } ;


      if (!jobName) {
        return _(new Error($("--job-name is required"))); } ;


      parsedSiteName = WebsitesClient.parseSiteName(name);
      context = {
        subscription: profile.current.getSubscription(options.subscription).id,
        site: {
          name: parsedSiteName.name,
          slot: (options.slot ? options.slot : parsedSiteName.slot) } };



      return createWebsiteExtensionsClient(context, __cb(_, __frame, 20, 20, function ___(__0, __1) { service = __1;
        operation = service[(jobType.toLowerCase() + "WebJobs")];


        progress = cli.interaction.progress($("Getting WebJob")); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__2() {

              return operation.get(jobName, __cb(_, __frame, 26, 27, function ___(__0, __2) { webJob = __2; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$__2() {

                progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$__2() {


            jobTypeKey = (jobType.toLowerCase() + "WebJob");

            if (log.format().json) {
              log.json(webJob[jobTypeKey]); }
             else {
              cli.interaction.logEachData($("WebJob"), webJob[jobTypeKey]); } ; _(); }); }); }, true)); }); });



  siteJobs.command("delete [jobName] [jobType] [name]").usage("[options] <jobName> <jobType> [name]").description($("Delete a WebJob")).option("--job-name <job-name>", $("required. The name of the webjob.")).option("--job-type <job-type>", $("required. The type of the webjob. Valid value is \"triggered\" or \"continuous\".")).option("-q, --quiet", $("quiet mode, do not ask for delete confirmation")).option("--slot <slot>", $("the name of the slot")).option("-s, --subscription <id>", $("the subscription id")).execute(function __3(jobName, jobType, name, options, _) { var parsedSiteName, context, service, operation, progress; var __frame = { name: "__3", line: 149 }; return __func(_, this, arguments, __3, 4, __frame, function __$__3() {








      if (jobType) {
        validation.isValidEnumValue(jobType, ["continuous","triggered",]); }
       else {
        return _(new Error($("--job-type is required"))); } ;


      if (!jobName) {
        return _(new Error($("--job-name is required"))); } ;


      parsedSiteName = WebsitesClient.parseSiteName(name);
      context = {
        subscription: profile.current.getSubscription(options.subscription).id,
        site: {
          name: parsedSiteName.name,
          slot: (options.slot ? options.slot : parsedSiteName.slot) } }; return (function __$__3(_) {



        var __1 = !options.quiet; if (!__1) { return _(null, __1); } ; return cli.interaction.confirm(util.format("Delete WebJob %s? [y/n] ", jobName), __cb(_, __frame, 20, 45, function ___(__0, __3) { var __2 = !__3; return _(null, __2); }, true)); })(__cb(_, __frame, -148, 18, function ___(__0, __2) { return (function __$__3(__then) { if (__2) { return _(null); } else { __then(); } ; })(function __$__3() {



          return createWebsiteExtensionsClient(context, __cb(_, __frame, 24, 20, function ___(__0, __3) { service = __3;
            operation = service[(jobType.toLowerCase() + "WebJobs")];
            progress = cli.interaction.progress($("Deleting WebJob")); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__3() {

                  return operation.deleteMethod(jobName, __cb(_, __frame, 28, 18, function __$__3() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$__3() {

                    progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$__3() {


                log.info(util.format($("WebJob %s has been deleted"), jobName)); _(); }); }); }, true)); }); }, true)); }); });


  siteJobs.command("upload [jobName] [jobType] [jobFile] [name]").usage("[options] <jobName> <jobType> <jobFile> [name]").description($("Upload a new WebJob")).option("--job-name <job-name>", $("required. The name of the webjob.")).option("--job-type <job-type>", $("required. The type of the webjob. Valid values are \"triggered\" or \"continuous\".")).option("--job-file <job-file>", $("required. The job file.")).option("--slot <slot>", $("the name of the slot")).option("-s, --subscription <id>", $("the subscription id")).execute(function __4(jobName, jobType, jobFile, name, options, _) { var parsedSiteName, context, service, progress, fileContent, operation; var __frame = { name: "__4", line: 193 }; return __func(_, this, arguments, __4, 5, __frame, function __$__4() {








      if (jobType) {
        validation.isValidEnumValue(jobType, ["continuous","triggered",]); }
       else {
        return _(new Error($("--job-type is required"))); } ;


      if (!jobName) {
        return _(new Error($("--job-name is required"))); } ;


      if (!jobFile) {
        return _(new Error($("--job-file is required"))); } else {
        if (!fs.existsSync(jobFile)) {
          return _(new Error($("Specified file does not exist"))); } else {
          if ((path.extname(jobFile) !== ".zip")) {
            return _(new Error($("WebJobs need to be zip files"))); } ; } ; } ;


      if ((options.singleton && !utils.ignoreCaseEquals(jobType, "continuous"))) {

        return _(new Error($("Only continuous jobs can be set to singleton"))); } ;


      parsedSiteName = WebsitesClient.parseSiteName(name);
      context = {
        subscription: profile.current.getSubscription(options.subscription).id,
        site: {
          name: parsedSiteName.name,
          slot: (options.slot ? options.slot : parsedSiteName.slot) } };



      return createWebsiteExtensionsClient(context, __cb(_, __frame, 33, 20, function ___(__0, __1) { service = __1;
        progress = cli.interaction.progress($("Uploading new WebJob")); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__4() {

              return fs.readFile(jobFile, __cb(_, __frame, 36, 29, function ___(__0, __2) { fileContent = __2;
                operation = service[(jobType.toLowerCase() + "WebJobs")];
                return operation.uploadZip(jobName, path.basename(jobFile), fileContent, __cb(_, __frame, 38, 18, function __$__4() { _(null, null, true); }, true)); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$__4() {

                progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$__4() {


            return checkIfValid(service, jobName, jobType, __cb(_, __frame, 43, 6, function __$__4() {

              log.info(util.format($("WebJob %s has been uploaded"), jobName)); _(); }, true)); }); }); }, true)); }); });


  function checkIfValid(service, jobName, jobType, callback) {
    setTimeout(function() {
      var progress = cli.interaction.progress($("Getting WebJob"));
      var operation = service[(jobType.toLowerCase() + "WebJobs")];
      operation.get(jobName, function(err) {
        progress.end();

        if (err) {
          callback(new Error($("WebJob is not valid"))); }
         else {
          callback(); } ; });


    }, 2000); };


  siteJobs.command("start [jobName] [jobType] [name]").usage("[options] <jobName> <jobType> [name]").description($("Start a WebJob")).option("--job-name <job-name>", $("required. The name of the webjob.")).option("--job-type <job-type>", $("required. The type of the webjob. Valid value is \"triggered\" or \"continuous\".")).option("--slot <slot>", $("the name of the slot")).option("-s, --subscription <id>", $("the subscription id")).execute(function __5(jobName, jobType, name, options, _) { var parsedSiteName, context, service, progress; var __frame = { name: "__5", line: 264 }; return __func(_, this, arguments, __5, 4, __frame, function __$__5() {







      if (jobType) {
        validation.isValidEnumValue(jobType, ["continuous","triggered",]); }
       else {
        return _(new Error($("--job-type is required"))); } ;


      if (!jobName) {
        return _(new Error($("--job-name is required"))); } ;


      parsedSiteName = WebsitesClient.parseSiteName(name);
      context = {
        subscription: profile.current.getSubscription(options.subscription).id,
        site: {
          name: parsedSiteName.name,
          slot: (options.slot ? options.slot : parsedSiteName.slot) } };



      return createWebsiteExtensionsClient(context, __cb(_, __frame, 20, 20, function ___(__0, __1) { service = __1;
        progress = cli.interaction.progress($("Starting WebJob")); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__5() { return (function __$__5(__then) {

                if (utils.ignoreCaseEquals(jobType, "continuous")) {
                  return service.continuousWebJobs.start(jobName, __cb(_, __frame, 24, 36, __then, true)); } else { return (function __$__5(__then) {
                    if (utils.ignoreCaseEquals(jobType, "triggered")) {
                      return service.triggeredWebJobs.run(jobName, __cb(_, __frame, 26, 35, __then, true)); } else { __then(); } ; })(__then); } ; })(function __$__5() { _(null, null, true); }); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$__5() {


                progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$__5() {


            log.info(util.format($("WebJob %s has been started"), jobName)); _(); }); }); }, true)); }); });


  siteJobs.command("stop [jobName] [name]").usage("[options] <jobName> <jobType> [name]").description($("Stops a WebJob. Only continuous jobs can  be stopped")).option("--job-name <job-name>", $("required. The name of the webjob.")).option("--slot <slot>", $("the name of the slot")).option("-s, --subscription <id>", $("the subscription id")).execute(function __6(jobName, name, options, _) { var parsedSiteName, context, service, progress; var __frame = { name: "__6", line: 305 }; return __func(_, this, arguments, __6, 3, __frame, function __$__6() {






      if (!jobName) {
        return _(new Error($("--job-name is required"))); } ;


      parsedSiteName = WebsitesClient.parseSiteName(name);
      context = {
        subscription: profile.current.getSubscription(options.subscription).id,
        site: {
          name: parsedSiteName.name,
          slot: (options.slot ? options.slot : parsedSiteName.slot) } };



      return createWebsiteExtensionsClient(context, __cb(_, __frame, 14, 20, function ___(__0, __1) { service = __1;
        progress = cli.interaction.progress($("Stopping WebJob")); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__6() {

              return service.continuousWebJobs.stop(jobName, __cb(_, __frame, 17, 34, function __$__6() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$__6() {

                progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$__6() {


            log.info(util.format($("WebJob %s has been stopped"), jobName)); _(); }); }); }, true)); }); });


  var siteJobHistory = siteJobs.category("history").description($("Commands to manage your Web Site WebJobs History"));


  siteJobHistory.command("list [jobName] [name]").description($("List all the triggered WebJobs runs under a web site")).option("--job-name <job-name>", $("required. The name of the webjob.")).option("--slot <slot>", $("the name of the slot")).option("-s, --subscription <id>", $("the subscription id")).execute(function __7(jobName, name, options, _) { var parsedSiteName, context, service, webJobRuns, progress; var __frame = { name: "__7", line: 338 }; return __func(_, this, arguments, __7, 3, __frame, function __$__7() {





      if (!jobName) {
        return _(new Error($("--job-name is required"))); } ;


      parsedSiteName = WebsitesClient.parseSiteName(name);
      context = {
        subscription: profile.current.getSubscription(options.subscription).id,
        site: {
          name: parsedSiteName.name,
          slot: (options.slot ? options.slot : parsedSiteName.slot) } };



      return createWebsiteExtensionsClient(context, __cb(_, __frame, 14, 20, function ___(__0, __1) { service = __1;


        progress = cli.interaction.progress($("Getting WebJob runs")); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__7() {

              return service.triggeredWebJobs.listRuns(jobName, __cb(_, __frame, 19, 46, function ___(__0, __2) { webJobRuns = __2; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$__7() {

                progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$__7() {


            cli.interaction.formatOutput(webJobRuns.triggeredWebJobRuns, function(data) {
              if ((data.length > 0)) {
                log.table(data, function(row, item) {
                  row.cell($("Id"), item.id);
                  row.cell($("Status"), item.status);

                  var duration = item.duration;
                  row.cell($("Duration"), util.format("%d:%d:%d.%d", duration.hours(), duration.minutes(), duration.seconds(), duration.milliseconds));


                  row.cell($("Start Time"), item.startTime);
                  row.cell($("End Time"), item.endTime); }); }

               else {
                log.info($("No job runs exist.")); } ; }); _(); }); }); }, true)); }); });




  siteJobHistory.command("show [jobName] [runId] [name]").description($("Get the detaisl for a triggered WebJobs run under a web site")).option("--job-name <job-name>", $("required. The name of the webjob.")).option("--run-id <run-id>", $("optional. The id of the run history. If not specified, show the latest run.")).option("--slot <slot>", $("the name of the slot")).option("-s, --subscription <id>", $("the subscription id")).execute(function __8(jobName, runId, name, options, _) { var parsedSiteName, context, service, webJobRun, progress; var __frame = { name: "__8", line: 387 }; return __func(_, this, arguments, __8, 4, __frame, function __$__8() {






      if (!jobName) {
        return _(new Error($("--job-name is required"))); } ;


      parsedSiteName = WebsitesClient.parseSiteName(name);
      context = {
        subscription: profile.current.getSubscription(options.subscription).id,
        site: {
          name: parsedSiteName.name,
          slot: (options.slot ? options.slot : parsedSiteName.slot) } };



      return createWebsiteExtensionsClient(context, __cb(_, __frame, 14, 20, function ___(__0, __1) { service = __1;


        progress = cli.interaction.progress($("Getting WebJob run")); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__8() {

              return service.triggeredWebJobs.getRun(jobName, runId, __cb(_, __frame, 19, 45, function ___(__0, __2) { webJobRun = __2; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$__8() {

                progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$__8() {


            cli.interaction.logEachData($("WebJob run"), webJobRun.triggeredJobRun); _(); }); }); }, true)); }); });


  function createWebsiteExtensionsClient(context, _) { var websiteClient, siteData, authData, siteName, hostNameSuffix, service; var __frame = { name: "createWebsiteExtensionsClient", line: 414 }; return __func(_, this, arguments, createWebsiteExtensionsClient, 1, __frame, function __$createWebsiteExtensionsClient() {
      websiteClient = new WebsitesClient(cli, context.subscription);
      return websiteClient.lookupSiteNameAndWebSpace(context, __cb(_, __frame, 2, 18, function __$createWebsiteExtensionsClient() {
        return websiteClient.getSite(context, __cb(_, __frame, 3, 33, function ___(__0, __1) { siteData = __1;
          authData = websiteClient.getRepositoryAuthData(siteData);
          siteName = WebsitesClient.getSiteHostName(context.site.name, context.site.slot);
          return websiteClient.getHostNameSuffix(context.subscription, __cb(_, __frame, 6, 39, function ___(__0, __2) { hostNameSuffix = __2;
            service = utils.createWebSiteExtensionsClient(siteName, hostNameSuffix, authData.username, authData.password);

            return _(null, service); }, true)); }, true)); }, true)); }); };};
