Sha256: b58711d71bef69a40e995021cbad5afcba557a2c2ffe547c69cfdd00c323007f

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

require 'yaml'
require 'singleton'
require 'fileutils'

module WhatsupGithub
  # Creates readable objects from confirurarion files
  class Config
    attr_reader :config

    include Singleton

    @@filename = ''

    def self.filename=(filename)
      @@filename = filename
    end

    def initialize
      @file = @@filename
      @config = {}
    end

    def read
      unless File.exist?(@file)
        dist_file = File.expand_path("../template/#{@file}", __dir__)
        FileUtils.cp dist_file, @file
      end
      @config = YAML.load_file @file
      return {} unless @config

      @config
    end

    def repos
      read['repos']
    end

    def base_branch
      read['base_branch']
    end

    def output_format
      read['output_format']
    end

    def labels
      required_labels + optional_labels
    end

    def required_labels
      res = read.dig 'labels', 'required'
      return [] unless res

      res
    end

    def optional_labels
      res = read.dig 'labels', 'optional'
      return [] unless res

      res
    end

    def membership
      read['membership']
    end

    def magic_word
      read['magic_word']
    end

    def enterprise
      read['enterprise']
    end
  end
end

if $PROGRAM_NAME == __FILE__
  config = WhatsupGithub::Config.instance
  p config.repos
  p config.base_branch
  p config.output_format
  p config.labels
  p config.required_labels
  p config.optional_labels
  p config.magic_word
  p config.membership
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
whatsup_github-1.0.1 lib/whatsup_github/config_reader.rb
whatsup_github-1.0.0 lib/whatsup_github/config_reader.rb