/*** 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 util = require("util");
















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

var WebsitesClient = require("./websites/websitesclient");
var $ = utils.getLocaleString;

var linkedRevisionControl = require("../../util/git/linkedrevisioncontrol");

exports.init = function(cli) {
  var log = cli.output;
  var site = cli.category("site");

  var repository = site.category("repository").description($("Commands to manage your Web Site repositories"));


  repository.command("branch <branch> [name]>").usage("[options] <branch> [name]").description($("Set your repository branch")).option("--slot <slot>", $("the name of the slot")).option("-s, --subscription <id>", $("the subscription id")).execute(function __1(branch, name, options, _) { var parsedSiteName, context, repositoryUri; var __frame = { name: "__1", line: 38 }; return __func(_, this, arguments, __1, 3, __frame, function __$__1() {





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



      return site.ensureRepositoryUri(context, __cb(_, __frame, 12, 31, function ___(__0, __1) { repositoryUri = __1; return (function __$__1(__then) {
          if (repositoryUri) {
            return doSettingsSet(context, __cb(_, __frame, 14, 8, __then, true)); } else {

            log.error($("Repository is not setup")); __then(); } ; })(_); }, true)); }); });



  repository.command("delete [name]").usage("[options] [name]").description($("Delete your repository")).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 __2(name, options, _) { var parsedSiteName, context, repositoryUri; var __frame = { name: "__2", line: 64 }; return __func(_, this, arguments, __2, 2, __frame, function __$__2() {






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



      return site.ensureRepositoryUri(context, __cb(_, __frame, 10, 31, function ___(__0, __2) { repositoryUri = __2; return (function __$__2(__then) {
          if (repositoryUri) { return (function __$__2(_) {
              var __1 = !options.quiet; if (!__1) { return _(null, __1); } ; return cli.interaction.confirm($("Delete repository? [y/n] "), __cb(_, __frame, 12, 47, function ___(__0, __3) { var __2 = !__3; return _(null, __2); }, true)); })(__cb(_, __frame, -63, 18, function ___(__0, __3) { return (function __$__2(__then) { if (__3) { return _(null); } else { __then(); } ; })(function __$__2() {



                return doDeleteRepository(context, __cb(_, __frame, 16, 8, function __$__2() {
                  return site.doRepositoryDelete(context, __cb(_, __frame, 17, 13, __then, true)); }, true)); }); }, true)); } else {

            log.error($("Repository is not setup")); __then(); } ; })(_); }, true)); }); });



  repository.command("sync [name]").usage("[options] [name]").description($("Sync your repository")).option("--slot <slot>", $("the name of the slot")).option("-s, --subscription <id>", $("the subscription id")).execute(function __3(name, options, _) { var parsedSiteName, context, repositoryUri; var __frame = { name: "__3", line: 92 }; return __func(_, this, arguments, __3, 2, __frame, function __$__3() {





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



      return site.ensureRepositoryUri(context, __cb(_, __frame, 10, 31, function ___(__0, __1) { repositoryUri = __1; return (function __$__3(__then) {
          if (repositoryUri) {
            return site.doRepositorySync(context, __cb(_, __frame, 12, 13, __then, true)); } else {

            log.error($("Repository is not setup")); __then(); } ; })(_); }, true)); }); });



  repository.command("update [name]").usage("[options] [name]").description($("Link a website to a github account for deployment")).option("-s, --subscription <id>", $("the subscription id")).execute(function __4(name, options, _) { var parsedSiteName, context, websiteClient, sites, lvcClient, i, currentSite, siteNameParts; var __frame = { name: "__4", line: 114 }; return __func(_, this, arguments, __4, 2, __frame, function __$__4() {




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



      websiteClient = new WebsitesClient(cli, context.subscription);
      return websiteClient.lookupSiteName(context, __cb(_, __frame, 11, 20, function __$__4() {

        return site.doSitesGet(context, __cb(_, __frame, 13, 23, function ___(__0, __1) { sites = __1.filter(function(s) {
            var currentSiteName = WebsitesClient.parseSiteName(s.name);
            return utils.ignoreCaseEquals(currentSiteName.name, context.site.name); });


          lvcClient = linkedRevisionControl.createClient(cli, "git", websiteClient);

          i = 0; var __5 = false; return (function ___(__break) { var __more; var __loop = __cb(_, __frame, 0, 0, function __$__4() { __more = false; if (__5) { i++; } else { __5 = true; } ; var __4 = (i < sites.length); if (__4) {
                currentSite = sites[i];

                siteNameParts = WebsitesClient.parseSiteName(currentSite.name);
                options = {
                  subscription: context.subscription,
                  site: {
                    name: siteNameParts.name,
                    slot: siteNameParts.slot,
                    webspace: currentSite.webSpace },

                  flags: { siteExists: true } };


                return lvcClient.init(options, __cb(_, __frame, 34, 18, function __$__4() {
                  return lvcClient.deploy(options, __cb(_, __frame, 35, 18, function __$__4() { while (__more) { __loop(); }; __more = true; }, true)); }, true)); } else { __break(); } ; }); do { __loop(); } while (__more); __more = true; })(_); }, true)); }, true)); }); });



  function doSettingsGet(context, _) { var service, progress; var __frame = { name: "doSettingsGet", line: 153 }; return __func(_, this, arguments, doSettingsGet, 1, __frame, function __$doSettingsGet() {
      return createExtensionsClient(context, __cb(_, __frame, 1, 18, function ___(__0, __1) { service = __1;

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

                if ((context.settingKey !== undefined)) {
                  return service.settings.get(context.settingKey, __cb(_, __frame, 6, 32, function ___(__0, __3) { var __2 = __3.value; return _(null, __2); }, true)); } else {

                  return service.settings.list(__cb(_, __frame, 8, 32, function ___(__0, __5) { var __4 = __5.settings; return _(null, __4); }, true)); } ; })(function __$doSettingsGet() { _(null, null, true); }); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$doSettingsGet() {


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



  repository.doSettingsGet = doSettingsGet;

  function doSettingsSet(context, _) { var service, progress, settings; var __frame = { name: "doSettingsSet", line: 170 }; return __func(_, this, arguments, doSettingsSet, 1, __frame, function __$doSettingsSet() {
      return createExtensionsClient(context, __cb(_, __frame, 1, 18, function ___(__0, __1) { service = __1;

        progress = cli.interaction.progress(util.format($("Setting %s to %s"), context.settingKey, context.settingValue)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$doSettingsSet() {

              settings = { };
              settings[context.settingKey] = context.settingValue;
              return service.settings.update(settings, __cb(_, __frame, 7, 30, _, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$doSettingsSet() {

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



  function doDeleteRepository(context, _) { var service, progress; var __frame = { name: "doDeleteRepository", line: 183 }; return __func(_, this, arguments, doDeleteRepository, 1, __frame, function __$doDeleteRepository() {
      return createExtensionsClient(context, __cb(_, __frame, 1, 18, function ___(__0, __1) { service = __1;

        progress = cli.interaction.progress($("Deleting repository files")); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$doDeleteRepository() {

              return service.repository.deleteMethod(__cb(_, __frame, 5, 32, _, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$doDeleteRepository() {

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



  function createExtensionsClient(context, _) { var webSiteClient, suffix, service; var __frame = { name: "createExtensionsClient", line: 194 }; return __func(_, this, arguments, createExtensionsClient, 1, __frame, function __$createExtensionsClient() {
      webSiteClient = new WebsitesClient(cli, context.subscription);

      return webSiteClient.getHostNameSuffix(context.subscription, __cb(_, __frame, 3, 31, function ___(__0, __1) { suffix = __1;

        service = utils.createWebSiteExtensionsClient(WebsitesClient.getSiteHostName(context.site.name, context.site.slot), suffix, context.repositoryAuth.split(":")[0], context.repositoryAuth.split(":")[1]);





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