lib/command/apply_template.rb in cpl-1.3.0 vs lib/command/apply_template.rb in cpl-1.4.0

- old
+ new

@@ -18,14 +18,18 @@ - Templates are ordinary Control Plane templates but with variable preprocessing **Preprocessed template variables:** ``` - APP_GVC - basically GVC or app name - APP_LOCATION - default location - APP_ORG - organization - APP_IMAGE - will use latest app image + {{APP_ORG}} - organization name + {{APP_NAME}} - GVC/app name + {{APP_LOCATION}} - location, per YML file, ENV, or command line arg + {{APP_LOCATION_LINK}} - full link for location, ready to be used for the value of `staticPlacement.locationLinks` in the templates + {{APP_IMAGE}} - latest app image + {{APP_IMAGE_LINK}} - full link for latest app image, ready to be used for the value of `containers[].image` in the templates + {{APP_IDENTITY}} - default identity + {{APP_IDENTITY_LINK}} - full link for identity, ready to be used for the value of `identityLink` in the templates ``` DESC EXAMPLES = <<~EX ```sh # Applies single template. @@ -34,11 +38,11 @@ # Applies several templates (practically creating full app). cpl apply-template gvc postgres redis rails -a $APP_NAME ``` EX - def call # rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity + def call # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity ensure_templates! @created_items = [] @failed_templates = [] @skipped_templates = [] @@ -53,10 +57,12 @@ end end progress.puts if @asked_for_confirmation + @deprecated_variables = [] + pending_templates.each do |template, filename| step("Applying template '#{template}'", abort_on_error: false) do items = apply_template(filename) if items items.each do |item| @@ -68,13 +74,17 @@ $CHILD_STATUS.success? end end + warn_deprecated_variables + print_created_items print_failed_templates print_skipped_templates + + exit(1) if @failed_templates.any? end private def templates @@ -122,18 +132,56 @@ report_skipped(template) false end - def apply_template(filename) + def apply_template(filename) # rubocop:disable Metrics/MethodLength data = File.read(filename) - .gsub("APP_GVC", config.app) - .gsub("APP_LOCATION", config.location) - .gsub("APP_ORG", config.org) - .gsub("APP_IMAGE", latest_image) + .gsub("{{APP_ORG}}", config.org) + .gsub("{{APP_NAME}}", config.app) + .gsub("{{APP_LOCATION}}", config.location) + .gsub("{{APP_LOCATION_LINK}}", app_location_link) + .gsub("{{APP_IMAGE}}", latest_image) + .gsub("{{APP_IMAGE_LINK}}", app_image_link) + .gsub("{{APP_IDENTITY}}", app_identity) + .gsub("{{APP_IDENTITY_LINK}}", app_identity_link) + .gsub("{{APP_SECRETS}}", app_secrets) + .gsub("{{APP_SECRETS_POLICY}}", app_secrets_policy) + find_deprecated_variables(data) + + # Kept for backwards compatibility + data = data + .gsub("APP_ORG", config.org) + .gsub("APP_GVC", config.app) + .gsub("APP_LOCATION", config.location) + .gsub("APP_IMAGE", latest_image) + # Don't read in YAML.safe_load as that doesn't handle multiple documents cp.apply_template(data) + end + + def new_variables + { + "APP_ORG" => "{{APP_ORG}}", + "APP_GVC" => "{{APP_NAME}}", + "APP_LOCATION" => "{{APP_LOCATION}}", + "APP_IMAGE" => "{{APP_IMAGE}}" + } + end + + def find_deprecated_variables(data) + @deprecated_variables.push(*new_variables.keys.select { |old_key| data.include?(old_key) }) + @deprecated_variables = @deprecated_variables.uniq.sort + end + + def warn_deprecated_variables + return unless @deprecated_variables.any? + + message = "Please replace these variables in the templates, " \ + "as support for them will be removed in a future major version bump:" + deprecated = @deprecated_variables.map { |old_key| " - #{old_key} -> #{new_variables[old_key]}" }.join("\n") + progress.puts("\n#{Shell.color("DEPRECATED: #{message}", :yellow)}\n#{deprecated}") end def report_success(item) @created_items.push(item) end