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

















var path = require("path");
var request = require("request");
var util = require("util");

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

var groupUtils = require("./groupUtils");

var $ = utils.getLocaleString;

exports.init = function(cli) {

  var log = cli.output;
  var group = cli.category("group");

  var groupTemplate = group.category("template").description($("Commands to manage your local or gallery resource group template"));


  groupTemplate.command("list").description($("Lists gallery resource group templates")).option("-c --category [category]", $("the category of the templates to list")).option("-p --publisher [publisher]", $("the publisher of the templates to list")).execute(function __1(options, _) { var client, filters, result; var __frame = { name: "__1", line: 42 }; return __func(_, this, arguments, __1, 1, __frame, function __$__1() {




      client = createGalleryClient(profile.current.getSubscription(options.subscription));

      filters = [];
      if (options.publisher) {
        filters.push(util.format("Publisher eq '%s'", options.publisher)); } ;


      if (options.category) {
        filters.push(util.format("CategoryIds/any(c: c eq '%s')", options.category)); } ;


      return cli.interaction.withProgress($("Listing gallery resource group templates"), function __1(log, _) { var __frame = { name: "__1", line: 55 }; return __func(_, this, arguments, __1, 1, __frame, function __$__1() {

          return client.items.list(((filters.length === 0) ? null : { filter: filters.join(" and ") }), __cb(_, __frame, 1, 30, _, true)); }); }, __cb(_, __frame, 12, 35, function ___(__0, __2) { result = __2;


        cli.interaction.formatOutput(result.items, function(data) {
          if ((data.length === 0)) {
            log.info($("No gallery resource group templates")); }
           else {
            var validItems = data.filter(function(c) {
              return !utils.stringEndsWith(c.version, "-placeholder", true); });

            var sortedItems = validItems.sort(function(left, right) {
              return left.publisher.localeCompare(right.publisher); });

            log.table(sortedItems, function(row, item) {
              row.cell($("Publisher"), item.publisher);
              row.cell($("Name"), item.identity); }); } ; }); _(); }, true)); }); });





  groupTemplate.command("show [name]").description($("Shows a gallery resource group template")).usage("[options] <name>").option("-n --name <name>", $("the name of template to show")).execute(function __2(name, options, _) { var client, result; var __frame = { name: "__2", line: 81 }; return __func(_, this, arguments, __2, 2, __frame, function __$__2() {




      if (!name) {
        return _(null, cli.missingArgument("name")); } ;


      client = createGalleryClient(profile.current.getSubscription(options.subscription));

      return cli.interaction.withProgress($("Showing a gallery resource group template"), function __1(log, _) { var __frame = { name: "__1", line: 89 }; return __func(_, this, arguments, __1, 1, __frame, function __$__1() {

          return client.items.get(name, __cb(_, __frame, 1, 30, _, true)); }); }, __cb(_, __frame, 7, 35, function ___(__0, __2) { result = __2;


        cli.interaction.formatOutput(result.item, function(data) {
          log.data($("Name:         "), data.identity);
          log.data($("Publisher:    "), data.publisher);
          log.data($("Version:      "), data.version);
          log.data($("Summary:      "), (data.summary || data.longSummary));
          log.data($("Description:  "), data.description);
          log.data("");
          log.data($("Download uris:"));
          var downloaduris = groupUtils.getTemplateDownloadUrl(data);
          for (var i = 0; (i < downloaduris.length); i++) {
            log.data("");
            log.data($("  Name :"), downloaduris[i].name);
            log.data($("  Uri  :"), downloaduris[i].uri);
            log.data(""); }; }); _(); }, true)); }); });




  groupTemplate.command("download [name] [directory]").description($("Downloads a gallery resource group template")).usage("[options] [name] [directory]").option("-n --name <name>", $("the name of the template to download")).option("-d --directory <directory>", $("the name of the destination directory")).option("-q --quiet", $("quiet mode (do not prompt for overwrite if output file exists)")).execute(function __3(name, directory, options, _) { var confirm, client, result, uris, waitForDownloadEnd, i, fileName; var __frame = { name: "__3", line: 117 }; return __func(_, this, arguments, __3, 3, __frame, function __$__3() {






      if (!name) {
        return _(null, cli.missingArgument("name")); } ;


      confirm = cli.interaction.confirm.bind(cli.interaction);
      client = createGalleryClient(profile.current.getSubscription(options.subscription));
      return cli.interaction.withProgress(util.format($("Getting gallery resource group template %s"), name), function __1(log, _) { var __frame = { name: "__1", line: 126 }; return __func(_, this, arguments, __1, 1, __frame, function __$__1() {


          return client.items.get(name, __cb(_, __frame, 1, 30, _, true)); }); }, __cb(_, __frame, 7, 35, function ___(__0, __2) { result = __2;


        uris = groupUtils.getTemplateDownloadUrl(result.item);

        waitForDownloadEnd = function(stream, callback) {
          stream.on("close", function() {
            callback(null); });

          stream.on("error", function(ex) {
            callback(ex); }); };



        i = 0; var __6 = false; return (function ___(__break) { var __more; var __loop = __cb(_, __frame, 0, 0, function __$__3() { __more = false; if (__6) { i++; } else { __6 = true; } ; var __5 = (i < uris.length); if (__5) {
              fileName = path.join(directory, (uris[i].name + ".json"));
              return groupUtils.normalizeDownloadFileName(fileName, options.quiet, confirm, __cb(_, __frame, 26, 30, function ___(__0, __3) { fileName = __3; return (function __$__3(__then) {
                  if (fileName) {
                    log.info(("Downloading " + fileName));
                    return waitForDownloadEnd(request(uris[i].uri).pipe(fs.createWriteStream(fileName)), __cb(_, __frame, 29, 10, __then, true)); } else { __then(); } ; })(function __$__3() { while (__more) { __loop(); }; __more = true; }); }, true)); } else { __break(); } ; }); do { __loop(); } while (__more); __more = true; })(_); }, true)); }); });




  groupTemplate.command("validate [resource-group]").description($("Validates a template to see whether it's using the right syntax, resource providers, resource types, etc.")).usage("[options] <resource-group>").option("-g --resource-group <resource-group>", $("the name of the resource group")).fileRelatedOption("-f --template-file <template-file>", $("the path to the template file in the file system")).option("--template-uri <template-uri>", $("the uri to the remote template file")).option("--template-version <template-version>", $("the content version of the template")).option("-p --parameters <parameters>", $("a JSON-formatted string containing parameters")).fileRelatedOption("-e --parameters-file <parametersFile>", $("a file containing parameters")).option("--subscription <subscription>", $("the subscription identifier")).execute(function __4(resourceGroup, options, _) { var __frame = { name: "__4", line: 161 }; return __func(_, this, arguments, __4, 2, __frame, function __$__4() {










      if (!resourceGroup) {
        return _(null, cli.missingArgument("resourceGroup")); } ;

      return groupUtils.validateTemplate(cli, resourceGroup, options, log, __cb(_, __frame, 4, 17, function __$__4() { _(); }, true)); }); });};



function createGalleryClient(subscription) {
  return utils.createGalleryClient(subscription);};
