Sha256: 02b9055203ccdd86114c41c10d2d50dd1423b356d476676f42de81f402a830a0

Contents?: true

Size: 1.91 KB

Versions: 1

Compression:

Stored size: 1.91 KB

Contents

module Vidar
  class Config
    DEFAULT_MANIFEST_FILE = "vidar.yml".freeze

    DEFAULT_OPTIONS = {
      compose_file:       -> { "docker-compose.ci.yml" },
      default_branch:     -> { "master" },
      current_branch:     -> { `git rev-parse --abbrev-ref HEAD`.strip.tr("/", "-") },
      revision:           -> { `git rev-parse HEAD`.strip },
      revision_name:      -> { `git show --pretty=format:"%s (%h)" -s HEAD`.strip },
      kubectl_context:    -> { `kubectl config current-context`.strip },
      shell_command:      -> { "/bin/sh" },
      console_command:    -> { "bin/console" },
      base_stage_name:    -> { "base" },
      release_stage_name: -> { "release" },
    }.freeze

    class << self
      attr_reader :data
      attr_writer :manifest_file

      def load(file_path = manifest_file)
        @data = YAML.load_file(file_path)
        @loaded = true
      end

      def manifest_file
        @manifest_file || DEFAULT_MANIFEST_FILE
      end

      def loaded?
        @loaded
      end

      def get(key)
        load unless loaded?

        value = @data[key.to_s] || DEFAULT_OPTIONS[key.to_sym]&.call

        return value unless value.is_a?(String)

        Vidar::Interpolation.call(value, self)
      end

      def get!(key)
        get(key) || fail(MissingConfigError, key)
      end

      def deploy_config
        deployments = get(:deployments)
        deployments = {} unless deployments.is_a?(Hash)
        deployment  = deployments[get!(:kubectl_context)]

        if deployment.nil?
          Log.error "ERROR: could not find deployment config for #{get!(:kubectl_context)} context"
          return nil
        end

        deployment.transform_keys!(&:to_sym)
        deployment.transform_values! { |value| Vidar::Interpolation.call(value, self) }

        DeployConfig.new(deployment)
      end

      def default_branch?
        get!(:current_branch) == get!(:default_branch)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vidar-0.13.0 lib/vidar/config.rb